1. 单词的长度
【问题描述】
编写程序,从word.in文件中读入多个单词,文件中每行一个单词,直到读到文件结束为止,在word.out文件中保存各个单词的长度。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
freopen("word.in","r",stdin);
freopen("word.out","w",stdout);
string s;
while (cin >> s)
{
cout << s.size() << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
2. 按成绩排序
【问题描述】
编写程序,从score.in文件中读入多名学生的姓名和分数,将读入的信息按分数由高到低排序后保存到score.out文件中。
【题解代码】
#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
string name;
int score;
bool operator < (const Node &x) const {
if (score == x.score) {
return name < x.name;
}
return score > x.score;
}
}a[1010];
int main()
{
freopen("score.in","r",stdin);
freopen("score.out","w",stdout);
int n;
cin >> n;
for (int i = 0; i < n; i ++)
{
cin >> a[i].name >> a[i].score;
}
sort(a, a+n);
for (int i = 0; i < n; i ++)
{
cout << a[i].name << " " << a[i].score << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}

被折叠的 条评论
为什么被折叠?



