题目“输入任意数量整数,输出输入整数中的最大数,最小数,和总和”这个程序用c++怎么编写,越简单越好~

如题所述

代码如下:(望赏),有问题可以追问

#include<stdio.h>
int main()
{
int n,i;//整数个数
int sum = 0,min,max;
int tmp;
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&tmp);
if(i == 0)
{
min = tmp,max = tmp;
}
min = (min < tmp)?min:tmp;
max = (max > tmp)?max:tmp;
sum += tmp;
}
printf("min = %d max = %d sum = %d\n",min,max,sum);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-09
使用C++的输入输出流实现,并通过流状态判断输入的合法性。

#include <iostream>

using namespace std;
int main()
{
int max=0, min=0, total=0, count=0;
while(true){ //循环输入
int tmp;
cin >> tmp; //获取输入的整数
if(cin.fail()){ //如果输入的不是整数,退出输入。输入结束
cout << endl << "Input End!" << endl << endl;
break;
}
count++; // 计算个数
total += tmp; // 计算总数

//计算最大数和最小数
if(count==1){
max = tmp;
min = tmp;
} else{
min=min<tmp?min:tmp;
max=max>tmp?max:tmp;
}
}

//输出结果
cout << "共输入"<<count<<"个有效数字:"<<endl;
cout << "总数=" << total << endl;
cout << "最大数=" << max << endl;
cout << "最小数=" << min << endl;
}追问

使用c语言,不是c++,,,,,我是新手是不有区别。。

追答

有区别
C是C,C++是C++,千万别认为是一个东西。

如果你希望未来参与到大型系统的项目中,那么就学C++。
如果你希望未来做驱动或高性能的底层服务,就学C。

当然C++和C在很多层面有交集,甚至C++语法上99%的兼容C(不是100%完全兼容哟)。但是C++和C主要在理念上其实存在巨大分歧。
语法层面的问题永远不是大问题。

C是易学难精。
C++是难学难精。

给你一个C版本的:
#include
int main()
{
int max=0, min=0, total=0, count=0;
while(true){
int tmp=0;
int ret = scanf("%d",&tmp);
if(ret == 0){
printf("\n非整数输入,退出输入状态!\n\n");
break;
}
count++;
total += tmp;
if(count==1){
max = tmp;
min = tmp;
} else{
min=mintmp?max:tmp;
}
}
printf("共输入%d个有效数字:\n 总数=%d\n最大数=%d\n最小数=%d\n",count, total, max, min);
}

本回答被提问者采纳
第2个回答  2013-04-09
include"iostream"
#include"vector"
#include <algorithm>
using namespace std;
int main()
{
vector<int> a;
int b,c,d=0;
cout<<"the number you want to input:";
cin>>b;
cout<<"now input all the number"<<endl;
for(int i=1;i<=b;i )
{
cin>>c;
d =c;
a.push_back(c);
}
sort(a.begin(),a.end());
cout<<a[0]<<endl<<a[b-1]<<endl<<d<<endl;

}追问

使用c语言,不是c++,,,,,我是新手是不有区别。。

追答

可是你的问题上写的是c++......题目“输入任意数量整数,输出输入整数中的最大数,最小数,和总和”这个程序用c++怎么编写,越简单越好~

第3个回答  2013-04-09
用冒泡排序不就行了。这边没VC,等下个人给你代码吧。。
第4个回答  2013-04-09
嗯 定义3个变量一个保存最大,一个保存最小,一个保存总和
相似回答