[小算法] 求高于平均分的学生学号及成绩(学号和成绩从文件读取)
2011-08-24 15:30 Kevin Pan 阅读(323) 评论(0) 编辑 收藏 举报#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("data.txt");
double find(int total, int n);
int main()
{
double average = find(0, 0);
cout << "Average: " << average << endl;
system("pause");
return 0;
}
double find(int total, int n)
{
int number, score;
double average;
fin >> number >> score;
if(number != 0)
{
cout << number << ": " << score << endl;
average = find(total + score, n + 1);
if(score >= average)
{
cout << number << ": " << score << endl;
}
return average;
}
return (double)total / n;
}
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("data.txt");
double find(int total, int n);
int main()
{
double average = find(0, 0);
cout << "Average: " << average << endl;
system("pause");
return 0;
}
double find(int total, int n)
{
int number, score;
double average;
fin >> number >> score;
if(number != 0)
{
cout << number << ": " << score << endl;
average = find(total + score, n + 1);
if(score >= average)
{
cout << number << ": " << score << endl;
}
return average;
}
return (double)total / n;
}
data.txt 文件内容如下:
1 25
2 33
3 45
4 36
5 29
6 64
7 18
8 22
9 26
10 21
0 0