问题描述
我需要找到最小的最大奇数和偶数来自一组数据。
I need to find the smallest largest number of odd and sum of even from a group of data.
数据文件示例
2
1 2 4 5 6 7 8
$
2 5 6 7 4 2 1
2
1 2 4 5 6 7 8
2 5 6 7 4 2 1
第一行给出需要读取的行数(示例中为2)
The first line gives the number of lines that needs to be read (which is 2 in the example)
这就是我的尝试
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open("input.txt");
outfile.open("output.txt");
int max = INT_MIN, min = INT_MAX, num_even = 0, num_odd = 0;
int number, sum_even=0;
int counter = 0;
cout << "Test\n\n";
cout << setw(15)<<left<<"Line";
cout << setw(15) << left << "Smallest #";
cout << setw(15) << left << "Largest #";
cout << setw(15) << left << "# of Odds";
cout << setw(15) << left << "Sum of evens\n\n";
for (infile>>counter; counter>=1; counter--)
{
while (infile >> number)
{
if (number % 2 == 0) {
num_even++;
sum_even = sum_even + num_even;
}
else
num_odd++;
if (number > max)
max = number;
if (number < min)
min = number;
}
cout << setw(15) << left << counter;
cout << setw(15) << left << min;
cout << setw(15) << left << max ;
cout << setw(15) << left << num_odd;
cout << setw(15) << left << sum_even << endl;
}
infile.close();
outfile.close();
system("pause");
return 0;
}
它只是将数据作为一个完整的集合而不是逐行读取
摘要
最小[数字],最大[数字],奇数[数字]数和来自
数据组的偶数[数字]之和。它运行正常,但我希望它读取第一个数字(2),并知道它有2行数据可供阅读。然后我希望它读取这两行数据并从中找到最小[数字],最大[数字],奇数[数字]和偶数[数字]
之和。数据文件中的第一行给出了它应该单独读取的行数
smallest [number], largest [number], number of odd [numbers] and sum of even [numbers] from a group of data. It runs fine but I want it to read the first number (2) and know that it has 2 lines of data to read. Then I want it to read those 2 lines of data and find the smallest [number], largest [number], number of odd [numbers] and sum of even [numbers] from it. The first line in the data file gives the number of lines it should read seperately
推荐答案
这篇关于从一组数据中找出最小,最大,奇数和偶数之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!