2019独角兽企业重金招聘Python工程师标准>>>
#include
#include
#include
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int k;
double exp[1001];
//memset(exp,'0',sizeof(exp));
memset(exp,0,sizeof(double)*1001);
int flag = 0;
while(scanf("%d",&k)!= EOF){
int i;
while( 0 < k--){
scanf("%d",&i);
double coff;
scanf("%lf",&coff);
exp[i] += coff;
}
flag++;
if(flag%2 == 0) {
int count=0;
for(i=1000;i>=0;i--){
if(exp[i]>1e-5) count++;
}
if(count>0) printf("%d",count );
for(i=1000;i>=0;i--){
if(exp[i]>1e-5) printf(" %d %.1lf",i,exp[i]);
}
if(count>0) printf("\n");
//
memset(exp,0,sizeof(double)*1001);
}
}
return 0;
}
忘了系数为负数:
if(exp[i]!=0) count++;
正确代码:
#include
#include
#include
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int k;
double exp[1001];
//memset(exp,'0',sizeof(exp));
memset(exp,0,sizeof(exp));
int flag = 0;
while(scanf("%d",&k)!= EOF){
int i;
int j;
for (j = 0; j < k; j++){
scanf("%d",&i);
double coff;
scanf("%lf",&coff);
exp[i] += coff;
}
//flag++;
//if(flag%2 == 0) {
// int count=0;
// for(i=1000;i>=0;i--){
// if(exp[i]>1e-5) count++;
// }
// if(count>0) printf("%d",count );
// for(i=1000;i>=0;i--){
// if(exp[i]>1e-5) printf(" %d %.1lf",i,exp[i]);
// }
//
// if(count>0) printf("\n");
// //
// memset(exp,0,sizeof(exp));
//}
scanf("%d", &k);
/* while( 0 < k--){*/
for (j = 0; j < k; j++){
scanf("%d",&i);
double coff;
scanf("%lf",&coff);
exp[i] += coff;
}
int count=0;
for(i=1000;i>=0;i--){
if(exp[i]!=0) count++;
}
printf("%d",count );
for(i=1000;i>=0;i--){
if(exp[i]!=0) printf(" %d %.1lf",i,exp[i]);
}
printf("\n");
//
memset(exp,0,sizeof(exp));
}
return 0;
}