摘要:Uva 402 M*A*S*H
This problem is not difficult, but have two points.
The first is the output, in the end of line can't have space.
For example
printf("Selection #%d\n",dataCount);
for(kLoop = 0; kLoop < people; kLoop++){
if(peopleList[kLoop] != 0){
printf("%d ",kLoop+1);
}
}
}
puts("\n");
//Good
printf("Selection #%d\n",dataCount);
for(kLoop = 0; kLoop < people; kLoop++){
if(peopleList[kLoop] != 1){
if(flag == 0){
printf("%d",kLoop+1);
flag = 1;
}
else{
printf(" %d",kLoop+1);
}
}
}
puts("\n");
The second is when the number os lucky "home" position is bigger or smaller then the participate. U r program should still print the right answer.
My program:
#include
int main(){
int iLoop = 0, deck[20], people = 0, luckymen = 0;
int nowmen, kLoop = 0;
int dataCount = 1;
int peopleList[50];
while(scanf("%d %d",&people,&luckymen)==2){
for(iLoop = 0; iLoop < 20; iLoop++){
scanf("%d ",&deck[iLoop]);
}
nowmen = people;
for(iLoop = 0; iLoop < 50; iLoop++){
peopleList[iLoop] = 1;
}
for(iLoop = 0; iLoop < 20; iLoop++){
int j, cnt = 0;
for(j = 0; j < people; j++) {
cnt += peopleList[j];
if(cnt == deck[iLoop]) {
if(luckymen == nowmen)
break;
peopleList[j] = 0, cnt = 0, nowmen--;
}
}
}
int flag = 0;
printf("Selection #%d\n",dataCount);
for(kLoop = 0; kLoop < people; kLoop++){
if(peopleList[kLoop] != 0){
if(flag == 0){
printf("%d",kLoop+1);
flag = 1;
}
else{
printf(" %d",kLoop+1);
}
}
}
puts("\n");
dataCount++;
}
return 0;
}