WA了好几次,主要是有些情况没有想到,思维不行。暂时不知道怎么避免这类错误重复出现。
#include "iostream"
#include <cstring>
using namespace std;
int a[6];
int n, k;
int sum;
void four(){
int i, j;
for(i = 1; i <= n; i++){
int he = (sum - a[i]) % 1024; //对其他三个的和取模
if(he == 0){ //要是等于0,那么肯定1024
cout<<"1024"<<endl;
return;
}
}
int max = -1;
for(i = 1; i < n; i++){
for(j = i + 1; j <= n; j++){
int he = sum - a[i]- a[j]; //选择a[i] a[j]与空白配成1024倍数
int yu = he % 1024;
if(he >= 1024 && yu == 0){ //针对1 0 0 1结果为0, 和1 0 1024 1结果为1024
cout<<"1024"<<endl;
return;
}
if(yu > max)
max = yu;
}
}
cout<<max<<endl;
}
void five(){
int i, j;
int max = -1;
for(i = 1; i < n; i++){
for(j = i + 1; j <= n; j++){
int he = sum - a[i]- a[j]; //选中a[i] a[j]另外三个数,这是他们的和
int yu = a[i] + a[j]; //剩下的两个数对1024取模
if(he % 1024 == 0){
if(yu >= 1024 && yu %1024 == 0){//像1024 1 0 0 0结果应该为1,而不是进去输出1024
cout<<"1024"<<endl;
return;
}
else yu = yu % 1024;
if(yu > max)
max = yu;
}
}
}
if(max == -1)
cout<<"0"<<endl;
else
cout<<max<<endl;
}
int main(){
//freopen("in.txt", "r", stdin);
while(cin>>n){
sum = 0;
memset(a,0,sizeof(a));
for(int i = 1; i <= n; i++){
cin>>a[i];
sum += a[i];
}
if(n < 4)
cout<<"1024"<<endl;
else if(n == 4)
four();
else
five();
}
return 0;
}