A:人民币支付 总时间限制: 1000ms 内存限制: 65536kB 描述 从键盘输入一指定金额(以元为单位,如345),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的钞票。 输入 一个小于1000的正整数。 输出 输出分行,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数 样例输入 735 样例输出 7 0 1 1 1 0
这题太水,直接贴代码。
1 #include <iostream> 2 #include <stdio.h> 3 4 5 using namespace std; 6 int n; 7 int coun[6]={0}; 8 int main() 9 { 10 scanf("%d",&n); 11 while(n!=0) 12 { 13 coun[0] = n/100; 14 n = n%100; 15 coun[1] = n/50; 16 n = n%50; 17 coun[2] = n/20; 18 n=n%20; 19 coun[3] = n/10; 20 n=n%10; 21 coun[4] = n/5; 22 n=n%5; 23 coun[5] = n/1; 24 n=n%1; 25 26 } 27 for(int i = 0;i<6;i++) 28 { 29 printf("%d ",coun[i]); 30 } 31 return 0; 32 }
B:排队游戏
总时间限制: 1000ms 内存限制: 65536kB 描述 在幼儿园中,老师安排小朋友做一个排队的游戏。首先老师精心的把数目相同的小男孩和小女孩编排在一个队列中,每个小孩按其在队列中的位置发给一个编号(编 号从0开始)。然后老师告诉小朋友们,站在前边的小男孩可以和他后边相邻的小女孩手拉手离开队列,剩余的小朋友重新站拢,再按前后相邻的小男孩小女孩手拉 手离开队列游戏,如此往复。由于教师精心的安排,恰好可以保证每两个小朋友都能手拉手离开队列,并且最后离开的两个小朋友是编号最小的和最大的两个小朋 友。(注:只有小男孩在前,小女孩在后,且他们两之间没有其他的小朋友,他们才能手拉手离开队列)。请根据老师的排队,按小女孩编号从小到大的顺序,给出 所有手拉手离开队列的小男孩和小女孩的编号对。 输入 用一个字符串代表小朋友队列。字符串中只会出现两个字符,分别代表小男孩和小女孩,首先出现的字符代表小男孩,另一个字符代表小女孩。小孩总数不超过100 输出 按小女孩编号顺序,顺序输出手拉手离开队列的小男孩和小女孩的编号对,每行一对编号,编号之间用一个空格分隔。 样例输入 ((()(())())(())) 样例输出 2 3 5 6 4 7 8 9 1 10 12 13 11 14 0 15
这题坑的地方主要在于测试数据用的是左右括号,让人误以为男生用左括号表示,而女生用右括号表示,仔细看题的话,这题也很水。
1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 int n; 6 int m[200]={0}; 7 char femal,mal; 8 int main() 9 { 10 char c; 11 int i=0; 12 bool flag=true; 13 while(cin.get(c)) 14 { 15 if(c==' ') 16 break; 17 if(flag) 18 { 19 mal=c; 20 flag=false; 21 } 22 else if(c!=mal) 23 femal=c; 24 if(c==mal) 25 m[i++]=1; 26 else if(c==femal) 27 m[i++]=2; 28 } 29 30 for(int j=0;j<i/2;j++) 31 { 32 int a=0,b=0; 33 for(int k=0;k<i;k++) 34 { 35 if(m[k]==1) 36 a=k; 37 else if(m[k]==2) 38 { 39 b=k; 40 m[a]=0; 41 m[b]=0; 42 break; 43 } 44 } 45 printf("%d %d ",a,b); 46 } 47 return 0; 48 }
C:取石子游戏
总时间限制: 1000ms 内存限制: 65536kB 描述 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍.最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和7 25 7 --> 11 7 --> 4 7 --> 4 3 --> 1 3 --> 1 0 选手1取 选手2取 选手1取 选手2取 选手1取 最后选手1(先取的)获胜,在取的过程中选手2都只有唯一的一种取法。 给定初始时石子的数目,如果两个人都采取最优策略,请问先手能否获胜。 输入 输入包含多数数据。每组数据一行,包含两个正整数a和b,表示初始时石子的数目。 输入以两个0表示结束。 输出 如果先手胜,输出"win",否则输出"lose" 样例输入 34 12 15 24 0 0 样例输出 win lose 提示 假设石子数目为(a,b)且a >= b,如果[a/b] >= 2则先手必胜,如果[a/b]<2,那么先手只有唯一的一种取法. [a/b]表示a除以b取整后的值.
这题我还不知道我错在哪儿,有提示的情况下,按道理来说很水啊。
下面代码为WA代码,知道错误的童鞋求指出。。。这题考试时候花了半个小时。。。无语死了。。。结果还是没A
1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 6 int a,b; 7 int main() 8 { 9 while(cin>>a>>b) 10 { 11 if(a==0&&b==0) 12 break; 13 int flag=1; 14 while(1) 15 { 16 if(a>=b*2) 17 break; 18 else if(b>=a*2) 19 break; 20 else if(a==b) 21 break; 22 if(a>b) 23 a-=b; 24 else 25 b-=a; 26 flag=1-flag; 27 } 28 if(flag==1) 29 cout<<"win"<<endl; 30 else if(flag==0) 31 cout<<"lose"<<endl; 32 } 33 return 0; 34 }
D:去除C程序中的注释
总时间限制: 1000ms 内存限制: 65536kB 描述 C程序的注释用/*...*/来表示。请写一个程序,将输入的C程序源代码中的注释去掉,输出去掉注释之后的源代码。 用于测试的C代码保证符合语法,不使用C++的//注释语法。 注意,C语言不允许出现嵌套注释。具体来说,对于/*/**/"*/",如果不允许嵌套注释,那么它表示字符串"*/";如果允许嵌套注释,它表示一个引号"。 还请注意,字符串中出现的注释符/*属于字符串的一部分,注释中出现的双引号"属于注释的一部分。 输入 符合语法的C代码文本文件。代码每行不超过200个字符。 输出 去掉注释后的C代码。要求只能去掉注释,不可以做其他的修改,比如调整缩进,去除注释之外的换行符等。 样例输入 #include <stdio.h> #include <time.h> #include <stdlib.h> /*Hash Search: Hash function: division method; handling collisions: open addressing's linear probing. In this exercise, M is the basic area's length, all keys are non negative integers.*/ #define M 11 int hash(int key) { return key % M; } void init_hash(int* hashtable) { int i; for(i = 0; i < M; ++i) { hashtable[i] = -1; } } /*return value: 1:found, *position is the key's index; 0:not found, *position is where to insert the key; -1:overflow. */ int search_hash(int* hashtable, int key, int* position) { int i, h = hash(key); for(i = 0; i < M; ++i) { if(key == hashtable[h]) { *position = h; return 1; } if(-1 == hashtable[h]) { *position = h; return 0; } h = (h+1) % M; } *position = -1; return -1; } /*return value: 1:inserted, 0:overflow*/ int insert_hash(int* hashtable, int key) { int position, result; result = search_hash(hashtable, key, &position); if(-1 == result) return 0; hashtable[position] = key; return 1; } void main() { int hashtable[M]; init_hash(hashtable); srand(time(NULL)); int i, j, key; for(i = 0; i < 8; ++i) /*make a hash table with 8 elements*/ { key = rand() % 50; insert_hash(hashtable, key); printf("Insert %d ", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf(" "); } printf("Please input the key to search: "); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d ", j); else printf("Not found! "); } 样例输出 #include <stdio.h> #include <time.h> #include <stdlib.h> #define M 11 int hash(int key) { return key % M; } void init_hash(int* hashtable) { int i; for(i = 0; i < M; ++i) { hashtable[i] = -1; } } int search_hash(int* hashtable, int key, int* position) { int i, h = hash(key); for(i = 0; i < M; ++i) { if(key == hashtable[h]) { *position = h; return 1; } if(-1 == hashtable[h]) { *position = h; return 0; } h = (h+1) % M; } *position = -1; return -1; } int insert_hash(int* hashtable, int key) { int position, result; result = search_hash(hashtable, key, &position); if(-1 == result) return 0; hashtable[position] = key; return 1; } void main() { int hashtable[M]; init_hash(hashtable); srand(time(NULL)); int i, j, key; for(i = 0; i < 8; ++i) { key = rand() % 50; insert_hash(hashtable, key); printf("Insert %d ", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf(" "); } printf("Please input the key to search: "); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d ", j); else printf("Not found! "); } 提示 注意字符串,字符,转义字符的情况。 看看自己有没有考虑 "a"/*ccc*/" 这种情况。
重点就在提示处,注意引号、注释符、以及转义符就OK,我当时做的时候没有看到提示,卧槽。。。
AC代码在下面
1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 6 char s[202]={0}; 7 bool flag=true,f=true,t=true; 8 int main() 9 { 10 while(cin.getline(s,200)) 11 { 12 for(int i=0;s[i]!='