计算机程序设计(c++)第3周基础练习

1·判断奇偶数

题目内容:

编写程序,输入正整数,判断是奇数还是偶数,是奇数显示“odd”,是偶数显示“even”,输入“1”显示“odd”。

输入:正整数

输出:“odd”或“even”

【提示】使用%运算符,除2的余数为0就是偶数。if...else...判断。

样例1输入:

3

样例1输出:

odd

 

样例2输入:

4

样例2输出:

even

#include
using namespace std;
int main()
{
    int i;
    cin>>i;
    if(i%2 == 0)
    cout<<"even"<

2·判断数的类型

题目内容:

编写程序,输入实数,判断输入的数据是正实数、负实数、正整数、负整数、还是零,分别显示“positive real”, “negative real”, “positive integer”, “negative integer”, “zero”,注意,两个单词的中间有一个空格。

输入:实数

输出:给定的单词或词组之一。

【提示】若int(a)==a结果为true,则可判断为整数。

样例1输入:

10.00

样例1输出:

positive integer

样例2输入:

-10.1

样例2输出:

negative real

#include
#include
using namespace std;
int main()
{
	double a;
	cin >> a;
	if (a > 1e-10)
	{
		if (int(a) == a)
			cout << "positive integer" << endl;
		else cout << "positive real" << endl;
	}
	else if (a < -1e-10)
	{
		if (int(a) == a)
			cout << "negative integer" << endl;
		else cout << "negative real" << endl;
	}
	else
		cout << "zero" << endl;
	system("pause");
	return 0;
}

3·判断点的象限

题目内容:

编写程序,输入平面直角坐标的x,y值,判断点在哪个象限。不考虑在坐标轴上的情况。分别输出1、2、3或4。

输入:两个实数,用空格隔开

输出:1、2、3或4之一,分别表示第1、2、3、4象限。

样例1输入:

1 1

样例1输出:

1

 

样例2输入:

3 -4

样例2输出:

4

#include
using namespace std;
int main(void)
{
    double x,y;
    int d;
    cin>>x>>y;
    if(x>0&&y>0)
        d=1;
    if(x<0&&y>0)
        d=2;
    if(x<0&&y<0)
        d=3;
    if(x>0&&y<0)
        d=4;
    cout<

4·判断字符类型

题目内容:

编写程序,输入一个字符,判断其是数字、大写字母、小写字母还是其他,分别显示0,1,2或-1。

输入:一个ASCII字符

输出:数字-1,0,1或2

样例1输入:

3

样例1输出:

0

 

样例2输入:

#

样例2输出:

-1

 

样例3输入:

A

样例3输出:

1

#include
using namespace std;
int main(void)
{
    char ch;
    cin>>ch;
    if (ch >= 'A'&&ch <= 'Z')
    {
	cout<<"1"<= 'a'&& ch <= 'z')
    {
	cout<<"2"<= '0'&&ch <= '9')
    {
	cout<<"0"<

5·百分制成绩转五分制成绩

题目内容:

编写程序,输入百分制的分数(非负整数),将其转换为5分制成绩,成绩对应关系如下:

90-100: 5

80-89: 4

70-79:  3

60-69:  2

10-59:  1

0-9:  0

 

输入:非负整数

输出:[0,5]之间的整数

 

样例1输入:

80

样例1输出:

4

#include
using namespace std;
int main(void)
{
    int i,j;
    cin>>i;
    j=i/10;
    switch (j)
    {
	case 10:
	case 9:cout<<"5"<

6·显示n个字符

题目内容:

编写程序,输如正整数n和字符c,在一行输出n个字符c。如输入:10 #,显示

##########

输入:一个正整数n和一个字符c,用空格隔开

输出:一行,n个字符c

 

样例1输入:

10 #

样例1输出:

##########

#include
#include
#include
using namespace std;
int main()
{
	string c;
	int n;
	cin >> n;
	cin >> c;
	for (int i = 0; i < n; i++)
	{
		cout << c;
	}
	cout << endl;
	system("pause");
	return 0;
}

7·显示字符组成的矩形

题目内容:

编写程序,输入行数n、列数m和一个字符c,显示由字符c组成的n行m的矩形。

如输入:5 10 #,输出:

##########

##########

##########

##########

##########

输入:两个正整数和一个字符,用空格隔开

输出由字符c组成的矩形

样例1输入:

5 10 #

样例1输出:

##########

##########

##########

##########

##########

#include
#include
#include
using namespace std;
int main()
{
	string c;
	int n,m;
	cin >> n>>m;
	cin >> c;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << c;
		}
		cout << endl;
	}	
	system("pause");
	return 0;
}

8·用循环计算1+2+3+...+n

题目内容:

编写程序,输入非负整数n,计算s=1+2+3+...+n的值。要求使用循环,而不是使用公式。

输入:非负整数n

输出:和

注意,请自己分别使用for和while实现。

样例1输入:

10

样例1输出:

55

#include
#include
#include
using namespace std;
int main()
{
	int n;
	int s = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		s += i;
	}
	cout << s << endl;
	system("pause");
	return 0;
}

9·计算1+1/2+1/3+...+1/n

题目内容:

编写程序,输入非负整数n,计算s=1+1/2+1/3+...+1/n的值。输入0时,输出0。

输入:非负整数n

输出:级数的前n项和。

【提示】1/n应写成1.0/n。和应为double型。请自己分别使用for和while实现。

样例1输入:

3

样例1输出:

1.83333

#include
#include
using namespace std;
int main()
{
	int n;
	double s=0.000000;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		s += 1.0 / i;
	}
	cout << s << endl;
	system("pause");
	return 0;
}

10·计算n!

题目内容:

编写程序,输入非负整数n,计算n!。0!=1。

输入:非负整数n

输出:n!

【提示】阶乘的初始值应设为1。请自己分别使用for和while循环实现。

样例1输入:

6

样例1输出:

720

#include
#include
using namespace std;
int main()
{
	int n;
	int s = 1;
	cin >> n;
	if (n == 0) s = 1;
	for (int i = 1; i <= n; i++)
	{
		s *= i;
	}
	cout << s << endl;
	system("pause");
	return 0;
}

11·交替输出1和-1

题目内容:

编写程序,输入正整数n,从1开始交替输出n个1和-1。如输入3,输出1 -1 1;

输入4,输出1 -1 1 -1,数据间用一个空格隔开。

输入:一个正整数n。

输出:1,-1交替组成的序列,用空格隔开,末尾无空格。

【提示】 (1)k=1;将k=-k放在循环体中产生交替序列。

(2)末尾无空格的实现方法:

i=0;

cout<

while(i

{

cout<<" "<

}

cout<

样例1输入:

3

样例1输出:

1 -1 1

样例2输入:

4

样例2输出:

1 -1 1 -1

#include
#include
using namespace std;
int main()
{
	int  n;
	int k = 1;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		if (i % 2 == 1) cout << k;
		if (i % 2 == 0) cout << -k;
		if (i < n)
		{
			cout << " ";
		}
	}
	cout << endl;
	system("pause");
	return 0;
}

12·判断整数的位数

题目内容:

编写程序,输入非负整数,判断整数的位数。如输入:12,输出:2

输入:一个非负整数

输出:整数的位数

【提示】

样例1输入:

12

样例1输出:

2

 

样例2输入:

1234

样例2输出:

4

#include
#include
using namespace std;
int main()
{
	int n = 0;
	int i = 0;
	cin >> n;
	while (n >= 10)
	{
		n = n / 10;
		++i;
	}
	cout << i+1 << endl;
	system("pause");
	return 0;
}

13·求非负整数的各位数字的

题目内容:

编写程序,输入非负整数,输出其各位数字的和,如输入:1234,输出10.

输入:一个非负整数

输出:整数

【提示】

样例1输入:

1234

样例1输出:

10

#include
#include
using namespace std;
int main()
{
	int n;
	int sum = 0;
	cin >> n;	
	while (n)
	{
		sum =sum+ n % 10;
		n = n / 10;
	}
	cout << sum << endl;
	system("pause");
	return 0;
}

14·九九乘法表

题目内容:

编写程序,显示如下的n行的九九乘法表。如输入5,,显示的乘法表如下:

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

 

输入:[1,9]之间的整数n

输出:n行的乘法表,一行的各项间用一个空格隔开。

【提示】

样例1输入:

5

样例1输出:

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

#include
#include
using namespace std;
int main()
{
	int n;
	int sum = 1;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			sum = i*j;
			cout << i << "*" << j << '=' << sum;
                        if (j < i)
		        {
			    cout << " ";
		        }
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

15·不一样的九九乘法表

题目内容:

编写程序,显示如下的n行的九九乘法表。如输入5,,显示的乘法表如下:

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

4*1=4 4*2=8 4*3=12 4*4=16

3*1=3 3*2=6 3*3=9

2*1=2 2*2=4

1*1=1

 

输入:[1,9]之间的整数n

输出:n行的乘法表,一行的各项间用一个空格隔开。

样例1输入:

5

样例1输出:

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

4*1=4 4*2=8 4*3=12 4*4=16

3*1=3 3*2=6 3*3=9

2*1=2 2*2=4

1*1=1

#include
#include
using namespace std;
int main()
{
	int n;
	int sum = 1;
	cin >> n;
	for (int i = n; i >0; --i)
	{
		for (int j = 1; j <= i; ++j)
		{
			sum = i*j;
			cout << i << "*" << j << '=' << sum ;
			if (j < i)
			{
				cout << " ";
			}
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

16·Fibonacci序列

题目内容:

编写程序,显示Fibonaci序列的前n项(从0开始)。

F(0)=0

F(1)=1

F(n)=F(n-1)+F(n-2)

 

输入:非负整数n

输出:n+1个整数,数据间有一个空格,末尾无空格。

【提示】

样例1输入:

10

样例1输出:

0 1 1 2 3 5 8 13 21 34 55

#include
#include
using namespace std;
int function(int n);
int main()
{
	int i,n;
	cin >> n;
	for (i = 0; i <= n; i++)
	{
		cout << function(i);
		if (i < n)
		{
			cout << " ";
		}
	}
	system("pause");
	cout << endl;
	return 0;
}
int function(int n)
{
	if (n == 0)
	{
		return 0;
	}
	else if (n == 1)
	{
		return 1;
	}
	else return function(n - 1) + function(n - 2);
}

 

你可能感兴趣的:(计算机程序设计(c++)第3周基础练习)