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

计算机程序设计C++ MOOC

测试与作业C++基础练习100题

##第三周基本练习

  1. 判断奇偶数
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int a;
	cin >> a;
	if (a % 2 == 0)
	{
		cout << "even" << endl;
	}
	else
	{
		cout << "odd" << endl; 
	}
	return 0;
}
  1. 判断数的类型
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	double a;
	cin >> a;
	if (a>0&&int(a)!=a)
	{
		cout << "positive real" << endl;
	}
	else if (a>0&&int(a)==a)
	{
		cout << "positive integer" << endl; 
	}
	else if (a<0 && int(a) != a)
	{
		cout << "negative real" << endl;
	}
	else if (a<0 && int(a) == a)
	{
		cout << "negative integer" << endl;
	}
	else if (a == 0)
	{
		cout << "zero" << endl;
	}
	return 0;
}
  1. 判断点的象限
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	double a, b;
	cin >> a >> b;
	if (a > 0 && b > 0)
	{
		cout << "1" << endl;
	}
	else if (a > 0 && b < 0)
	{
		cout << "4" << endl;
	}
	else if (a < 0 && b>0)
	{
		cout << "2" << endl;
	}
	else if (a < 0 && b < 0)
	{
		cout << "3" << endl;
	}
	return 0;
}
  1. 判断字符类型
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	char a;
	cin >> a;
	if (a >= '0'&&a <= '9')
	{
		cout << "0" << endl;
	}
	else if (a >= 'A'&&a <= 'Z')
	{
		cout << "1" << endl;
	}
	else if (a >= 'a'&&a <= 'z')
	{
		cout << "2" << endl;
	}
	else
	{
		cout << "-1" << endl;
	}
	return 0;
}
  1. 百分制成绩转五分制成绩
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int grade;
	cin >> grade;
	switch (grade / 10)
	{
	case 10:
		cout << "5" << endl;
		break;
	case 9:
		cout << "5" << endl;
		break;
	case 8:
		cout << "4" << endl;
		break;
	case 7:
		cout << "3" << endl;
		break;
	case 6:
		cout << "2" << endl;
		break;
	case 0:
		cout << "0" << endl;
		break;
	default:
		cout << "1" << endl;
	}
	return 0;
}
  1. 显示n个字符
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int number,i;
	char a;
	cin >> number >> a;
	for (i = 0; i < number; i++)
	{
		cout << a;
	}
	cout << endl;
	return 0;
}
  1. 显示字符组成的矩形
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int number1,number2,i,j;
	char a;
	cin >> number1>>number2 >> a;
	for (i = 0; i < number1; i++)
	{
		for (j = 0; j < number2; j++)
		{
			cout << a;
		}
		cout << endl;
	}
	return 0;
}
  1. 用循环计算1+2+3+…+n
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n, sum=0,i;
	cin >> n;
	i = 1;
	while (i <= n)
	{
		sum = sum + i;
		i++;
	}
	cout << sum << endl;
	return 0;
}
  1. 计算1+1/2+1/3+…+1/n
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n, i;
	double sum = 0;
	cin >> n;
	if (n != 0)
	{
		for (i = 1; i <= n; i++)
		{
			sum = sum + 1.0 / i;
		}
	}
	cout << sum << endl;
	return 0;
}
  1. 计算n!
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n, mul=1,i;
	cin >> n;
	if (n != 0)
	{
		for (i = 1; i <= n; i++)
		{
			mul = mul*i;
		}
	}
	cout << mul << endl;
	return 0;
}
  1. 交替输出1和-1
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n,i,a=1;
	cin >> n;
	cout << a;
	for (i = 2; i <= n; i++)
	{
		a = a*-1;
		cout << " " << a;
	}
	return 0;
}
  1. 判断整数的位数
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int a, n = 1;
	cin >> a;
	a = a / 10;
	while (a)
	{
		a = a / 10;
		n++;
	}
	cout << n << endl;
	return 0;
}
  1. 求非负整数的各位数字的和
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int a,sum=0;
	cin >> a;
	while (a)
	{
		sum = sum + a % 10;
		a = a / 10;
	}
	cout << sum << endl;
	return 0;
}
  1. 九九乘法表
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n,i,j;
	cin >> n;
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j < i; j++)
		{
			cout << i << "*" << j << "=" << i*j << " ";
		}
                cout << i << "*" << j << "=" << i*j ;
		cout << endl;
	}
	return 0;
}
  1. 不一样的九九乘法表
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{
	int n, i, j;
	cin >> n;
	for (i = n; i >= 1; i--)
	{
		for (j = 1; j < i; j++)
		{
			cout << i << "*" << j << "=" << i*j << " ";
		}
		cout << i << "*" << j << "=" << i*j;
		cout << endl;
	}
	return 0;
}
  1. Fibonacci序列
    在这里插入图片描述
#include "iostream"

using namespace std;

int main()
{ 
	int n, fib=1,fib_1=1,fib_2=0;
	cin >> n;
	cout << "0";
	while (n)
	{
		cout <<" "<<fib;
		fib = fib_1 + fib_2;
		fib_2 = fib_1;
		fib_1 = fib;
		n--;
	}
	return 0;
}

以上为第三次基础练习。

猜你喜欢

转载自blog.csdn.net/qq_35779286/article/details/83892931