17、现有若干个学生(不超过100的数据记景,每个记录包括学号姓名,三科成绩,学号不超过15位,且有字母。成绩为整数,每名学生的姓名不超过10个字母,且只包含字母,读入条记录,再按要求输出
- 输入:学生数量n占一行每个学生的学号、姓名、三科成绩占一行,空格分开。
- 输出:每个学生的学号、姓名、三科成绩占一行,逗号分开。
1、思路 题目需要存放同一个对象的多个数据,采用结构体实现,信息存储,输入完成后,遍历结构体数组并输出。
2、具体实现
using namespace std;
struct student{
string id;
string name;
int score1;
int score2;
int score3;
};
int main()
{
student stu[100];
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>stu[i].id>>stu[i].name>>stu[i].score1>>stu[i].score2>>stu[i].score3;
}
for(int i=0;i<n;i++)
{
cout<<stu[i].id<<','<<stu[i].name<<','<<stu[i].score1<<','<<stu[i].score2<<','<<stu[i].score3<<endl;
}
}
}
3、小结 对于多个不同类别数据的存放,可以采用结构体来存储,并通过结构体数组存放多个数据。
18、求和s=1!+2!+3!++10!(阶乘求和) 1、思路 求阶乘之和采用递归操作,通过展开阶乘,寻找阶乘递归规律,采用一层for循环求解。
2、具体实现
#include<iostream>
using namespace std;
int f(int a)
{
if(a==1)return 1;
else
{
return a*f(a-1);
}
}
int main()
{
int sum=0;
for(int i=1;i<=10;i++)
{
sum+=f(i);
}
cout<<sum<<endl;
}
3、小结 对于递归的规律,可以通过展开递归式来寻找规律,针对该规律进行求解。
。