1059

本文介绍了一个C++程序,该程序利用素数筛选和二分搜索技术来处理学生ID并根据不同条件输出相应的奖品。首先通过筛法找出素数,然后使用二分搜索快速定位输入的学生ID,并依据学生的特定属性给出奖品类型。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100000
#define e 1e-3
using namespace std;

int n,m,s[N]={1,1};

struct stu
{
    char id[10];
    int mc,f;
}A[N];

bool cmp(struct stu a,struct stu b)
{
    return strcmp(a.id,b.id)<0;
}

void sushu()
{
    for(int i=1;i<=10000;i++)
    {
        if(s[i]==1)
            continue;
        for(int j=i*2;j<=10000;j+=i)
            s[j]=1;
    }
} 
void find(char aa[])
{
    int pos=0;
    int l=1,r=n;
    if(l==r)
        pos=n;
    else 
    while(r-l>=1)
    {
        if(r-l==1)
        {
            if(strcmp(A[r].id,aa)==0)
                pos=r;
            else if(strcmp(A[l].id,aa)==0)
                pos=l;
            else 
                pos=0;
            break; 
        }
        pos=(l+r)/2;
        if(strcmp(A[pos].id,aa)==0)
            break; 
        else if(strcmp(A[pos].id,aa)>0)
            r=pos;
        else if(strcmp(A[pos].id,aa)<0)
            l=pos;
    }
    if(strcmp(A[pos].id,aa)==0)
    {
        A[pos].f++;
    }
    else
        pos=0;
    if(A[pos].f>1)
        printf("%s: Checked\n",aa);
    else 
    {
        if(A[pos].mc==1)
            printf("%s: Mystery Award\n",aa);
        else if(pos==0)
            printf("%s: Are you kidding?\n",aa);
        else if(s[A[pos].mc]==0)
            printf("%s: Minion\n",aa);
        else
            printf("%s: Chocolate\n",aa);
    }
}
int main()
{
    sushu();

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {   
        scanf("%s",&A[i].id);
        A[i].f=0;
        A[i].mc=i;
    }

    sort(A+1,A+1+n,cmp);

    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        char p[10];
        scanf("%s",p);
        find(p);    
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值