C++编程练习题 复习必备(3-10)

第三题,输入一个成绩,打印相应的等级

#include<iostream>
using namespace std;

int main()
{
	int score;
	cout << "pleae enter your score" << endl;
	cin >> score;
	if (score >= 0 && score <= 100)
	{
		switch (score / 10)
		{
		case 10:
		case 9: cout << "你的分数等级是 A。" << endl; break;
		case 8:
		case 7:cout << "你的分数等级是B。" << endl; break;
		case 6:cout << "你的分数等级是C。" << endl; break;
		default:cout << "你的分数不及格。" << endl;
		}
	}
	else
		cout << "please enter a valid score." << endl;
		return 0;
}

学过后面一点的知识点,所以可操作性比较强一点。不过只要能实现一样的功能就好了。

第四题,输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边

#include<iostream>
using namespace std;

int main()
{
	double a, b, c;
	cout << "please enter three number :" << endl;
	cin >> a >> b >> c;
	
	if ((a + b) > c && (a + c) > b && (b + c) > a)
	{
		cout << "These three numbers can form a triangle" << endl;
	}
	else
	{
		cout << "Cannot form a triangle" << endl;
	}
	return 0;
}

第五题,输入20个数,求其最大、最小和平均值

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	int arr[10];
	cout << "please enter ten numbers:";
	for (int i = 0; i < 10; i++)
	{
		cin >> arr[i];
	}
	int max = arr[0];
	for (int j = 0; j < 10; j++)
	{
		if (arr[j] > max)
		{
			max = arr[j];
		}
	}
	int min = arr[0];
	for (int k = 0; k < 10; k++)
	{
		if (arr[k] < max)
		{
			min = arr[k];
		}
	}
	int  sum = 0;
	for (int t = 0; t < 10; t++)
	{
			sum =sum+ arr[t];
	}
	double ave = sum / 10.0;
	cout << "The maximum value of these twenty numbers is:" << max << endl;
	cout << "The minimum value of these twenty numbers is:" << min << endl;
	cout << "The average value of these twenty numbers is:" << ave << endl;

	return 0;

}

 20个数字太多了,我改成了十个数字,也是一样的效果。答案那里写得很简洁,但是我自己不喜欢那种做法。

第六题,输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	
	cout << "please enter a numbers:";
	double a;
	cin >> a;
	int arr[] = {0};
	cout << "please enter " << a << " number" << endl;
	for (int i = 0; i < a; i++)
	{
		cin >> arr[i];
	}
	int max = arr[0];
	for (int j = 0; j < a; j++)
	{
		if (arr[j] > max)
		{
			max = arr[j];
		}
	}

	int  sum = 0;
	for (int t = 0; t < a; t++)
	{
		sum = sum + arr[t];
	}
	double ave = sum / a;
	cout << "The maximum value of these twenty numbers is:" << max << endl;
	cout << "The average value of these twenty numbers is:" << ave << endl;

	return 0;

}

这个程序其实有问题,但是基本能通过。算乐下次再改它吧。定义一个初始不定长度的数组我还不会。

2022年10月14日15:36:40

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值