zoukankan      html  css  js  c++  java
  • PAT 1116 Come on! Let's C [简单]

    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 ID: award where the award is Mystery Award, or Minion, or Chocolate. If the ID is not in the ranklist, print Are you kidding? instead. If the ID has been checked before, print 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个排名的id,序号代表排名,如果是第一名输出Mystery Award,素数名输出Minion,其他人输出Chocolate,重复查询提示。

    水水的AC:

    #include <iostream>
    #include <algorithm>
    #include<cstdio>
    #include <map>
    #include<cmath>
    using namespace std;
    
    map<int,string> award;
    map<int,int> query;
    bool prime(int a){
        int q=sqrt(a);
        for(int i=2;i<=q;i++){
            if(a%i==0){
                return false;
            }
        }
        return true;
    }
    int main()
    {
        int n,no;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&no);
            if(i==0){
                award[no]="Mystery Award";
            }else{
                if(prime(i+1)){
                    award[no]="Minion";
                }else{
                    award[no]="Chocolate";
                }
            }
        }
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d",&no);
            if(award.count(no)==0){
                printf("%04d: Are you kidding?
    ",no);
            }else {
                if(query.count(no)==1){
                    printf("%04d: Checked
    ",no);
                }else{//使用printf怎么输出String啊啊?好像没办法。。。
                    printf("%04d: ",no);
                    cout<<award[no]<<"
    ";
                    query[no]=1;
                }
            }
        }
        return 0;
    }

    //有一个槽点,就是在写最后出现了个问题,代码中有注释,所以就混合使用printf和cout输出了,罪过。

    1.查了一下发现是需要将其转换成char*类型,直接使用:

    printf("%04d: %s
    ",no,award[no].c_str());

    即可!c_str()就是将string转换为char*的函数!!!

  • 相关阅读:
    [RTT例程练习] 3.1 动态内存管理之rt_malloc和rt_free
    [RTT例程练习] 3.3 静态内存管理,内存池mempool
    [RTT例程练习] 6.2 在 Finsh 中运行自定义函数
    [RTT例程练习] 2.9 事件机制event
    [SCons 有点翻译的scons学习] 3. 生成和使用库
    [RTT例程练习] 3.2 动态内存管理之rt_realloc和free
    vim 启动 python的自动补全
    [RTT例程练习] 6.1 Finsh 的基本使用
    ELF文件重定位
    [RTT例程练习] 4.2 动态定时器
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9915671.html
Copyright © 2011-2022 走看看