2014北大计算机学科保研直博夏令营上机poj考试

A:人民币支付
总时间限制: 1000ms 内存限制: 65536kB
描述
从键盘输入一指定金额(以元为单位,如345),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的钞票。

输入
一个小于1000的正整数。
输出
输出分行,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数
样例输入
735
样例输出
7
0
1
1
1
0
View Code

这题太水,直接贴代码。

 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 }
View Code

B:排队游戏

总时间限制: 1000ms 内存限制: 65536kB
描述
在幼儿园中,老师安排小朋友做一个排队的游戏。首先老师精心的把数目相同的小男孩和小女孩编排在一个队列中,每个小孩按其在队列中的位置发给一个编号(编 号从0开始)。然后老师告诉小朋友们,站在前边的小男孩可以和他后边相邻的小女孩手拉手离开队列,剩余的小朋友重新站拢,再按前后相邻的小男孩小女孩手拉 手离开队列游戏,如此往复。由于教师精心的安排,恰好可以保证每两个小朋友都能手拉手离开队列,并且最后离开的两个小朋友是编号最小的和最大的两个小朋 友。(注:只有小男孩在前,小女孩在后,且他们两之间没有其他的小朋友,他们才能手拉手离开队列)。请根据老师的排队,按小女孩编号从小到大的顺序,给出 所有手拉手离开队列的小男孩和小女孩的编号对。

输入
用一个字符串代表小朋友队列。字符串中只会出现两个字符,分别代表小男孩和小女孩,首先出现的字符代表小男孩,另一个字符代表小女孩。小孩总数不超过100
输出
按小女孩编号顺序,顺序输出手拉手离开队列的小男孩和小女孩的编号对,每行一对编号,编号之间用一个空格分隔。
样例输入
((()(())())(()))
样例输出
2 3
5 6
4 7
8 9
1 10
12 13
11 14
0 15
View Code

这题坑的地方主要在于测试数据用的是左右括号,让人误以为男生用左括号表示,而女生用右括号表示,仔细看题的话,这题也很水。

 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 }
View Code

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取整后的值.
View Code

这题我还不知道我错在哪儿,有提示的情况下,按道理来说很水啊。

下面代码为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 }
View Code

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*/"
这种情况。
View Code

重点就在提示处,注意引号、注释符、以及转义符就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]!='';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("
");
57     }
58     return 0;
59 }
View Code

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
View Code

貌似维护一个递减链表可以解。当时并未做出

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
View Code

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
View Code

简单零一背包问题,很水。

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
",d[m]);
21     return 0;
22 }
View Code

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
View Code

这道题很水,就是注意到一个值为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:
%d %d

",count,l,r);
48             count++;
49     }
50     return 0;
51 }
View Code

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
提示
考虑看成最小生成树问题,注意输入表示。
View Code

就是简单的求最小生成树而已,本题也很水,只要熟悉最小生成树的代码,这题也绝壁轻松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 }
View Code

最后那个J题就是纯扯淡了。。。根本不用看,考试的那点儿时间绝壁做不出。

里面有几道没贴AC代码的,改天A掉之后贴出。

原文地址:https://www.cnblogs.com/xiaoshen555/p/3843870.html