第三题,输入一个成绩,打印相应的等级
#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