A - 士兵队列训练问题
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample Input
2 20 40
Sample Output
1 7 19 1 19 37
//这道题我的想法是用模拟的办法解决的,用一个数组保存 位置[]上人的编号,然后根据命令,报2,3的位置 位置[] 等于0,然后将 位置[] 不为0的往最前面靠,形成一个新的列。

1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 int people[5010]; 6 7 int er() 8 { 9 int i,j; 10 for (i=2;i<=5001;i++) 11 { 12 if (people[i]==0&&people[i+1]==0) 13 return 0;//完成 14 if (i%2!=0) 15 { 16 for (j=1;j<i;j++) 17 { 18 if(people[j]==0) 19 { 20 break; 21 } 22 } 23 if (j!=i) 24 { 25 people[j]=people[i]; 26 people[i]=0; 27 } 28 29 } 30 else if (i%2==0) 31 { 32 people[i]=0; 33 } 34 } 35 return 0; 36 } 37 38 int san() 39 { 40 int i,j; 41 for (i=2;i<=5001;i++) 42 { 43 if (people[i]==0&&people[i+1]==0) 44 return 0;//完成 45 if (i%3!=0) 46 { 47 for (j=1;j<i;j++) 48 { 49 if(people[j]==0) 50 { 51 break; 52 } 53 } 54 if (j!=i) 55 { 56 people[j]=people[i]; 57 people[i]=0; 58 } 59 60 } 61 else if (i%3==0) 62 { 63 people[i]=0; 64 } 65 } 66 return 0; 67 } 68 69 70 int main() 71 { 72 int m,n,i; 73 cin>>n; 74 while (n--) 75 { 76 memset(people,0,sizeof(int)*5010); 77 cin>>m; 78 for (i=0;i<=m;i++) 79 people[i]=i;//数据是编号 80 if (m>3)//小于3的话去判断就不符合题意了 81 { 82 for (i=1;i<=m;i++) 83 { 84 if (i%2==1) 85 { 86 er(); 87 if (people[4]==0) break; 88 } 89 else if (i%2==0) 90 { 91 san(); 92 if (people[4]==0) break; 93 } 94 } 95 } 96 for (i=1;i<=3;i++) 97 { 98 if (people[i]!=0) 99 { 100 if (i!=1) cout<<" "; 101 cout<<people[i]; 102 } 103 } 104 cout<<endl; 105 } 106 return 0; 107 }