
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\n",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=='\n') 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\n",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\n", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf("\n"); } printf("Please input the key to search:\n"); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d\n", j); else printf("Not found!\n"); } 樣例輸出 #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\n", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf("\n"); } printf("Please input the key to search:\n"); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d\n", j); else printf("Not found!\n"); } 提示 注意字符串,字符,轉義字符的情況。 看看自己有沒有考慮 "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]!='\0';i++) 13 { 14 if(flag==true&&(s[i]!='/'||s[i+1]!='*')) 15 { 16 printf("%c",s[i]); 17 if(s[i]=='\\'&&t==true) 18 t = false; 19 else if(s[i]=='\"') 20 { 21 if(f==true) 22 f=false; 23 else 24 { 25 if(t==false) 26 t=true; 27 else 28 f=true; 29 } 30 } 31 else 32 t = true; 33 } 34 else if( flag==true &&f==true) 35 flag=false; 36 else if(flag==true &&f==false) 37 { 38 printf("%c",s[i]); 39 } 40 else if(flag == false) 41 { 42 if(i>0) 43 { 44 if(!(s[i-1]=='*'&&s[i]=='/')) 45 ; 46 else 47 { 48 flag=true; 49 } 50 } 51 else 52 ; 53 } 54 } 55 if(flag==true) 56 printf("\n"); 57 } 58 return 0; 59 }
E:求逆序對數

總時間限制: 500ms 內存限制: 65536kB 描述 對於一個長度為N的整數序列A,滿足i < j 且 Ai > Aj.的數對(i,j)稱為整數序列A的一個逆序 請求出整數序列A的所有逆序對個數 輸入 輸入包含多組測試數據,每組測試數據有兩行 第一行為整數N(1 <= N <= 20000),當輸入0時結束 第二行為N個整數,表示長為N的整數序列 輸出 每組數據對應一行,輸出逆序對的個數 樣例輸入 5 1 2 3 4 5 5 5 4 3 2 1 1 1 0 樣例輸出 0 10 0
貌似維護一個遞減鏈表可以解。當時並未做出
F:Battle City

總時間限制: 1000ms 內存限制: 65536kB 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture). Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there? 輸入 The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed. 輸出 For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead. 樣例輸入 3 4 YBEB EERE SSTE 0 0 樣例輸出 8
G:Charm Bracelet

總時間限制: 1000ms 內存限制: 65536kB 描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N(1 ≤ N≤ 3,402) available charms. Each charm iin the supplied list has a weight Wi(1 ≤ Wi≤ 400), a 'desirability' factor Di(1 ≤ Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M(1 ≤ M≤ 12,880). Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings. 輸入 Line 1: Two space-separated integers: N and M Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di 輸出 Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints 樣例輸入 4 6 1 4 2 6 3 12 2 7 樣例輸出 23
簡單零一背包問題,很水。
AC代碼:

1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 int n,m; 6 int d[20000]={0}; 7 int main() 8 { 9 scanf("%d %d",&n,&m); 10 for(int i=0;i<n;i++) 11 { 12 int a,b; 13 scanf("%d %d",&a,&b); 14 for(int j=m;j>=a;j--) 15 { 16 if(d[j]<=d[j-a]+b) 17 d[j] = d[j-a]+b; 18 } 19 } 20 printf("%d\n",d[m]); 21 return 0; 22 }
H:Binary Tree

總時間限制: 1000ms 內存限制: 65536kB 描述 Background Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this: The root contains the pair (1, 1). If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b) Problem Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right child? 輸入 The first line contains the number of scenarios. Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*109) that represent a node (i, j). You can assume that this is a valid node in the binary tree described above. 輸出 The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario. 樣例輸入 3 42 1 3 4 17 73 樣例輸出 Scenario #1: 41 0 Scenario #2: 2 1 Scenario #3: 4 6
這道題很水,就是注意到一個值為1的時候的情況而已,並非直接取模。其他方面的話,這題也沒什么難的。
AC代碼:

1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 int n; 6 int l=0,r=0; 7 void work(int a,int b) 8 { 9 while(1) 10 { 11 if(a==1&&b==1) 12 break; 13 if(a==1) 14 { 15 r+=b/a-1; 16 b=1; 17 continue; 18 } 19 if(b==1) 20 { 21 l+=a/b-1; 22 a=1; 23 continue; 24 } 25 if(a>b) 26 { 27 l+=a/b; 28 a=a%b; 29 } 30 else if(a<b) 31 { 32 r+=b/a; 33 b=b%a; 34 } 35 } 36 } 37 int main() 38 { 39 scanf("%d",&n); 40 int count = 1; 41 while(n--) 42 { 43 int a,b; 44 scanf("%d %d",&a,&b); 45 l=0,r=0; 46 work(a,b); 47 printf("Scenario #%d:\n%d %d\n\n",count,l,r); 48 count++; 49 } 50 return 0; 51 }
I:寶昌縣長要修路

總時間限制: 1000ms 內存限制: 10000kB 描述 寶昌縣長意氣風發,他決定整修之前縣里的道路。縣里的道路很多,但維護費用昂貴。具體如圖A所示。線段上面的數據表示兩個節點之間的所需要的維修費用,現在需要對鄉村進行道路優化,最基本的要求是將所有的村庄節點都要聯通起來,並且要求每月的維護費用最小。比如優化后的圖如B所示。 輸入 第一行只包含一個表示村庄個數的數n,n不大於26,並且這n個村庄是由大寫字母表里的前n個字母表示。接下來的n-1行是由字母表的前n-1個字母開頭。最后一個村庄表示的字母不用輸入。對於每一行,以每個村庄表示的字母開頭,然后后面跟着一個數字,表示有多少條道路可以從這個村到后面字母表中的村庄。如果k是大於0,表示該行后面會表示k條道路的k個數據。每條道路的數據是由表示連接到另一端村庄的字母和每月維修該道路的花費組成。維修費用是正整數的並且小於100。該行的所有數據字段分隔單一空白。該公路網將始終連接所有的村庄。該公路網將永遠不會超過75條道路。沒有任何一個村庄會有超過15條的道路連接到其他村庄(之前或之后的字母)。在下面的示例輸入,數據是與上面的地圖相一致的。 輸出 輸出是一個整數,表示每個數據集中每月維持道路系統連接到所有村庄所花費的最低成本。 樣例輸入 9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 樣例輸出 216 提示 考慮看成最小生成樹問題,注意輸入表示。
就是簡單的求最小生成樹而已,本題也很水,只要熟悉最小生成樹的代碼,這題也絕壁輕松AC

1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 int d[27][27]={0}; 6 int n; 7 const int MAX=10000; 8 int s[27]={0}; 9 int flag[27]={0}; 10 int main() 11 { 12 cin>>n; 13 for(int i=0;i<n;i++) 14 { 15 for(int j=0;j<n;j++) 16 d[i][j] =MAX; 17 } 18 19 for(int i=0;i<n-1;i++) 20 { 21 char c; 22 int k; 23 cin>>c; 24 cin>>k; 25 for(int j=0;j<k;j++) 26 { 27 char a; 28 int dis; 29 cin>>a>>dis; 30 if(d[c-'A'][a-'A'] >dis && d[a-'A'][c-'A'] >dis) 31 { 32 d[c-'A'][a-'A']=dis; 33 d[a-'A'][c-'A']=dis; 34 } 35 } 36 } 37 int sum=0; 38 flag[0] = 1; 39 for(int i=0;i<n;i++) 40 s[i] = d[0][i]; 41 for(int i=0;i<n-1;i++) 42 { 43 int m= MAX; 44 int num; 45 for(int j=0;j<n;j++) 46 { 47 if(flag[j]==0&&s[j]<m) 48 { 49 m = s[j]; 50 num = j; 51 } 52 } 53 sum+=m; 54 flag[num] = 1; 55 for(int j=0;j<n;j++) 56 { 57 if(s[j] > d[num][j]) 58 s[j] = d[num][j]; 59 } 60 } 61 cout<<sum<<endl; 62 return 0; 63 }
最后那個J題就是純扯淡了。。。根本不用看,考試的那點兒時間絕壁做不出。
里面有幾道沒貼AC代碼的,改天A掉之后貼出。