[蓝蓝计算机考研算法]-day13

90 阅读1分钟

学生成绩

现有若干个学生(不超过100的数据记景,每个记录包括学号姓名,三科成绩,学号不超过15位,且有字母。成绩为整数,每名学生的姓名不超过10个字母,且只包含字母,读入条记录,再按要求输出

  • 输入:学生数量n占一行每个学生的学号、姓名、三科成绩占一行,空格分开。
  • 输出:每个学生的学号、姓名、三科成绩占一行,逗号分开。
  • 思路

定义学生结构体

  • 具体实现
#include<iostream>
#include<string>
using namespace std;

struct student
{
	string id;
	string name;
	int score1, score2, score3;
	student(string id_val, string name_val, int s1, int s2, int s3) :
		id(id_val), name(name_val), score1(s1), score2(s2), score3(s3) {};
};

int main()
{
	int n = 0, s1 = 0, s2 = 0,s3 = 0,i=0;
	string id = "", name = "";
	student* students[100];
	cin >> n;
	while (n-- && cin >> id >> name >> s1 >> s2 >> s3)
	{
		student* s = new student(id, name, s1, s2, s3);
		students[i++] = s;
	}
	cout << "输出:" << endl;
	for (auto it : students)
	{
		if (!it)break;
		cout << it->id << ',' << it->name << ',' << it->score1 << ',' << it->score2 << ',' << it->score3 << ',';
	}
	return 0;
}

阶乘求和

求和s=1!+2!+3!++10!

  • 定义阶乘算法
  • 具体实现
#include<iostream>
using namespace std;

int main()
{
	int sum = 0, fact = 1;
	for (int i = 1; i <= 10; i++)
	{
		fact *= i;
		sum += fact;
	}
	cout << sum;
	return 0;