求所有和中最大的和,它包含数字和
Find the biggest sum out of the all, it containing numbers and
我需要找到最大和,它包含数字和两个数字中的第一个还是第二个更大如何找到它?
假设n=10,两个put数分别为6和2,分别为:7和1,5和6,1和8,4和3。那么答案应该是,最大的和是11,它包含的数字是5和6,更大的麻木是第二个。
我有一个代码:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
我认为您的代码应该是这样的:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"n";
cout <<"The first number is "<<first<<"n";
cout<<"The second number is "<<second<<"n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated:您应该小心for循环的索引i,它应该以2开头,因为您在for循环之前读取了前两个数字。
相关文章:
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 如何将较大数字中从0到9的单个数字放入只包含数字的数组中
- 如何识别字符串只包含数字
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- 如何对包含数字和符号的字符串进行排序?
- 如何在包含数字的.bmp(二进制图像)中执行组件标记9 4 3 6
- 分类包含数字和单词的字符串向量
- 如何检查整数是否包含数字 0 和 1
- 求所有和中最大的和,它包含数字和
- 用于确定 2x2 正方形是否包含数字 1-4(无重复)的算法
- 将字符串(包含数字)转换为整数并返回该整数
- 函数来检查字符串是否包含数字
- 对包含数字的字符串进行排序
- 将包含数字的字符串解析为整数数组
- 在检测字符串是否包含数字字符时,这有什么问题
- 用于查找包含数字的非重叠范围的有效数据结构
- 福尔。while 循环:我应该怎么做,使输入 (int) 不包含数字 '1' 或"0"?
- c++递归检查number是否包含数字
- 将包含数字和单词的文本文件列表转换为整数2D数组