2021-10-24 PAT(5)

目录

1140 Look-and-say Sequence (20 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题目大意: 

答案:

1116 Come on! Let's C (20 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题目大意: 

答案:

1121 Damn Single (25 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题目大意: 

思路:

答案:


1140 Look-and-say Sequence (20 分)

Look-and-say sequence is a sequence of integers as the following:

D, D1, D111, D113, D11231, D112213111, ...

where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1's, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (≤ 40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111

题目大意: 

第n+1个串是第n个串的描述。比如第n个串是11122234(随便写的),那么第n+1个串就是13233141,这个意思就是在第n个串中  1有3个, 2有3个, 3有1个, 4有1个,所以是13233141。 

答案:

#include <stdio.h>

int exterior(char *s1, char *s2)
{
        int count = 0;
        while (*s1) {
                count++;
                if (*s1 != *(s1 + 1)) {
                        *s2++ = *s1;
                        //转换为字符串形式 
                        *s2++ = count + '0';
                        count = 0;
                }
                s1++;
        }
        return 0;
}

int main()
{
        //初始化数组长度要大
        char str1[100000] = {0}, str2[100000] = {0};
        int N, i;

        scanf("%s %d", str1, &N);

        for (i = 1; i < N; i++)
                i % 2 ? exterior(str1, str2) : exterior(str2, str1);

		//字符串输出 
        puts(i % 2 ? str1 : str2);

        return 0;
}

搜的,没太看懂

while(*s1)可以一直可以循环,不就进死循环了吗,不理解,求解答。

1116 Come on! Let's C (20 分)

"Let's C" is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

  • 0、 The Champion will receive a "Mystery Award" (such as a BIG collection of students' research papers...).
  • 1、 Those who ranked as a prime number will receive the best award -- the Minions (小黄人)!
  • 2、 Everyone else will receive chocolates.

Given the final ranklist and a sequence of contestant ID's, you are supposed to tell the corresponding awards.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤104), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant's ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID's.

Output Specification:

For each query, print in a line where the award is , or , or . If the ID is not in the ranklist, print instead. If the ID has been checked before, print .ID: awardMystery AwardMinionChocolateAre you kidding?ID: Checked

Sample Input:

6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample Output:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

题目大意: 

给出N个学生的排名,输出他们每个人的奖品.

第一:Mystery Award。

排名是素数的奖品小黄人(Minion).

每个人都有巧克力。

若查询人不在排名里,则输出“Are you kidding?”.

查询过的输出“Checked”.

答案:

#include<stdio.h>
#include<math.h>
int a[10010]={0};
char rewards[6][100] = {"Are you kidding?","Mystery Award","Minion","Chocolate","Checked"};

int primeN( int p )
{
    if(p<=1)
        return 0;
    else if(p==2)
        return 1;
    int i;
     for(i=2 ; i<=p-1 ; i++)
    {
         if(p%i==0)
         {
             return 0;
         }
    }
        if(i==p) return 1;
    }

int main(){
	int n,i,N;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		int temp;
		scanf("%d",&temp);
		if(i==1)
			a[temp]=1;
		else if(primeN(i))
			a[temp]=2;
		else
			a[temp]=3;
	}
	scanf("%d",&N);
	for(i=1;i<=N;i++){
		int temp;
		scanf("%d",&temp);
		printf("%04d: %s\n",temp,rewards[a[temp]]);
		if(a[temp]>0)
			a[temp]=4;
	}
}

二维数组

素数的判断。
int primeN( int p )
{
    if(p<=1)
        return 0;
    else if(p==2)
        return 1;
        int i;
 for(i=2 ; i<=p-1 ; i++)
{
     if(p%i==0)
     {
         return 0;
     }
}
if(i==p) return 1;
}

1121 Damn Single (25 分)

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 50,000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (≤ 10,000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888

题目大意: 

第一行N为下列行数,跟着N行每行一对夫妻的代号。再输入K个人的代号。

求出单身或配偶不在场的人的总数和每人的代号。

思路:

  1. 用 -1 当初始值,均为单身狗(因为有 0 号客人,所以最好不用 0 当初始值)
  2. 数组映射一对伴侣
  3. 用 -2 表示客人到场。读取客人入场,用映射寻找自己的伴侣,当自己单身或伴侣还未到场,自己用 -2 表示到场
  4. 若映射的伴侣状态为 -2 到场,则两人配对成功,两人用 -3 表示脱离单身狗标签,单身狗数目减2

答案:

#include <stdio.h>

#define BLANK -1  //都是单身狗 
#define ATTEND -2 //客人到场是单身或伴侣未到场 
#define PAIR -3  //有伴侣 且两人都到场 

int main()
{
        int n, m, couple[100000];
        int id1, id2, count = 0;
        int i;
        for (i = 0; i < 100000; i++)
                couple[i] = BLANK;

        scanf("%d", &n);
        for (i = 0; i < n; i++) {
                scanf("%d %d", &id1, &id2);
                //用数组映射一对伴侣
                couple[id1] = id2;
                couple[id2] = id1;
        }

        scanf("%d", &m);
        for (i = 0; i < m; i++) {
                scanf("%d", &id1);
                //id1到场,且他的伴侣也到场 
                if (couple[id1] >= 0 && couple[couple[id1]] == ATTEND) {
                		//id1和他的伴侣都是PAIR 
                        couple[couple[id1]] = PAIR;
                        couple[id1] = PAIR;
                        //不是单身狗的人数加2 
                        count += 2;
                } else {
                        couple[id1] = ATTEND;
                }
        }
		//输出人数 
        printf("%d\n", m - count); 
                count = m - count;
        for (i = 0; i < 100000; i++)
                if (couple[i] == ATTEND)
                        printf("%05d%s", i, --count? " " : "");

        return 0;
}

数组映射

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值