int 类型有它的范围:
int: -2,147,483,648 to 2,147,483,647
如果使用 long long int 类型会更好。
long long int: -(2^63) to (2^63)-1
以下是我的测试代码,请参考 LernerCpp 的注释以附加 typedef 语句:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
typedef long long int INT64;
INT64 minimini(INT64 list[]){
INT64 minisum=0;
INT64 taker;
INT64 a=max({list[0],list[1],list[2],list[3],list[4]});
for (int i=0; i<5; i++){
taker=list[i];
if(taker!=a){
minisum=minisum+taker;
}
}
return minisum;
}
INT64 maxa(INT64 list[]){
INT64 maxsum=0;
INT64 taker;
INT64 a=min({list[0],list[1],list[2],list[3],list[4]});
for (int i=0; i<5; i++){
taker=list[i];
if(taker!=a){
maxsum=maxsum+taker;
cout<<"maxsum >>"<<maxsum<<endl;
cout<<"a="<<a<<endl;
}
}
return maxsum;
}
int main(){
INT64 list[5];
INT64 minisum, maxsum;
// int: -2,147,483,648 to 2,147,483,647
// long long int: -(2^63) to (2^63)-1
for (int i=0; i<5; i++){
std::string tmp;
char* pEnd=0;
printf("enter number %d:\n",i);
cin>>tmp;
list[i]=strtoll(tmp.c_str(),&pEnd,10);
printf("number %d: %lld\n",i,list[i]);
}
printf("the list array are: [%lld,%lld,%lld,%lld,%lld,]\n",list[0],list[1],list[2],list[3],list[4]);
minisum=minimini(list);
maxsum=maxa(list);
cout<<minisum<<" "<<maxsum;
return 0;
}
coliru 现场演示
结果:
C:\Users\s41167\Documents\mingw-w64>test_maximum.exe
enter number 0:
10000000000
number 0: 10000000000
enter number 1:
-10000000000
number 1: -10000000000
enter number 2:
20000000000
number 2: 20000000000
enter number 3:
-20000000000
number 3: -20000000000
enter number 4:
30000000000
number 4: 30000000000
the list array are: [10000000000,-10000000000,20000000000,-20000000000,300000000
00,]
maxsum >>10000000000
a=-20000000000
maxsum >>0
a=-20000000000
maxsum >>20000000000
a=-20000000000
maxsum >>50000000000
a=-20000000000
0 50000000000
C:\Users\s41167\Documents\mingw-w64>
和 ANIKET LAVKUSH VISHWAKARMA 19B 的输入:
C:\Users\s41167\Documents\mingw-w64>test_maximum.exe
enter number 0:
256741038
number 0: 256741038
enter number 1:
623958417
number 1: 623958417
enter number 2:
467905213
number 2: 467905213
enter number 3:
714532089
number 3: 714532089
enter number 4:
938071625
number 4: 938071625
the list array are: [256741038,623958417,467905213,714532089,938071625,]
maxsum >>623958417
a=256741038
maxsum >>1091863630
a=256741038
maxsum >>1806395719
a=256741038
maxsum >>2744467344
a=256741038
2063136757 2744467344
C:\Users\s41167\Documents\mingw-w64>