PTA乙级 PAT (Basic Level) Practice C语言(完结)

PTA乙级习题

链接
PTA乙级

1001 害死人不偿命的(3n+1)猜想

#include<stdio.h>
#include<stdlib.h>
int main(){
    int n;
    if(scanf("%d",&n)){}
    int i=0;
    while(n!=1){
        if(n%2==1)  n=(3*n+1)/2;
        else        n=n/2;
        i++;        
    }
    printf("%d",i);
    return 0;
}

1002 写出这个数

#include<stdio.h>
#include<stdlib.h>
char p[][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int main(){
    char c;
    int sum=0;
    while(scanf("%c",&c)){
        if(c!='\n')
            sum=sum+(c-'0');
        else 
            break;       
    }
    char s[4];
    sprintf(s,"%d",sum);
    for(int i=0;s[i]!='\0';i++){
        if(i>0) printf(" ");
        printf("%s",p[s[i]-'0']);
    }
    return 0;
}

1003 我要通过!

#include<stdio.h>
#include<stdlib.h>
int check(char *s){
    int i=0;
    int P_loc=-1,T_loc=-1;
    int P_count=0,T_count=0;
    while(s[i]!='\0'){
        if(s[i]!='P'&&s[i]!='A'&&s[i]!='T')
            return 0;
        if(s[i]=='P'){
            P_loc=i;P_count++;
        }
        else if(s[i]=='T'){
            T_loc=i;T_count++;
        }
        i++;
    }
    if(T_count!=1||P_count!=1)
        return 0;
    if(T_loc-P_loc==1)
        return 0;
    if((P_loc)*(T_loc-P_loc-1)!=(i-T_loc-1))
        return 0;
    return 1;
}
int main(){
    int n;
    int b;
    char s[101];
    if(scanf("%d",&n)){}
    for(int i=0;i<n;i++){
        if(scanf("%s",s)){}
        b=check(s);
        b==1?printf("YES\n"):printf("NO\n");
    }
    return 0;
}

1004 成绩排名

#include<stdio.h>
#include<stdlib.h>
struct Info
{
    char name[11];
    char num[11];
    int score;
};

int main(){
    struct Info Max;
    struct Info Min;
    int n;
    if(scanf("%d",&n)){}
    if(scanf("%s %s %d",Max.name,Max.num,&Max.score))
    Min=Max;
    struct Info t;
    for(int i=0;i<n-1;i++){
        if(scanf("%s %s %d",t.name,t.num,&t.score)){}
        if(t.score>Max.score)   Max=t;
        else if(t.score<Min.score)  Min=t;
    }
    printf("%s %s\n",Max.name,Max.num);
    printf("%s %s",Min.name,Min.num);
    return 0;
}

1005 继续(3n+1)猜想

#include<stdio.h>
#include<stdlib.h>
int main(){
    int K,t;
    int p[101],exist[101];
    for(int i=0;i<100;i++){
        p[i]=0;exist[i]=0;
    }
    if(scanf("%d",&K)){}
    
    for(int i=0;i<K;i++)
        if(scanf("%d",&t)){p[t]=1;}
    for(int i=2;i<100;i++){
        if(p[i]!=1||exist[i]==1)  continue;
        t=i;
        while(t!=1){
            if(t%2==1)  t=(3*t+1)/2;
            else        t=t/2;
            if(t<=100)   exist[t]=1;
        }
    }
    for(int i=100,j=0;i>=2;i--){
        if(p[i]==1&&exist[i]!=1){
            if(j==0)printf("%d",i);
            else    printf(" %d",i);
            j++;
        }
    }
    return 0;
}

1006 换个格式输出整数

#include<stdio.h>
#include<stdlib.h>
int main(){
    int k;
    if(scanf("%d",&k)){}
    int i;
    for(i=0;i<k/100;i++)    printf("B");
    for(i=0;i<k%100/10;i++) printf("S");
    for(i=0;i<k%10;i++)     printf("%d",i+1);
    return 0;
}

1007 素数对猜想

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int isPrime(int k){
    for(int i=(int)sqrt(k);i>1;i--){
        if(k%i==0)
            return 0;
    }
    return 1;
} 
int main(){
    int N,sum=0;
    if(scanf("%d",&N)){}
    int p=1;
    for(int i=3;i<=N-2;i+=2){
        if(p==0){
            p=isPrime(i+2);
            continue;
        }
        if(isPrime(i+2))    sum++;
        else                p=0;
    }
    printf("%d",sum);
    return 0;
}

1008 数组元素循环右移问题

#include<stdio.h>
#include<stdlib.h>
void reverse(int *A, int start, int end){
    while(start<end){
        int t=A[start];
        A[start++]=A[end];
        A[end--]=t;
    }
}
int main(){
    int N,M;
    int A[100];
    if(scanf("%d %d",&N,&M)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&A[i])){}
    }
    M=M%N;
    reverse(A,0,N-M-1);
    reverse(A,N-M,N-1);
    reverse(A,0,N-1);
    for(int i=0;i<N;i++){
        if(i!=0) printf(" ");
        printf("%d",A[i]);
    }
    return 0;
}

1009 说反话

#include<stdio.h>
#include<stdlib.h>
int main(){
    char sentence[81][81];
    int i=0;
    while(scanf("%s",sentence[i++])!=EOF){}
    for(i=i-2;i>=0;i--){
        if(i==0)    printf("%s",sentence[i]);
        else        printf("%s ",sentence[i]);
    }
    return 0;
}

1010 一元多项式求导

#include<stdio.h>
#include<stdlib.h>
int main(){
    int a,n;
    int i=0;
    while(scanf("%d %d",&a,&n)!=EOF){
        if(i!=0&&n!=0)    printf(" ");
        i++;
        if(n!=0)    printf("%d %d",a*n,n-1);
        else if(i==1)        printf("0 0");
        
    }
    return 0;
}

1011 A+B 和 C

#include<stdio.h>
#include<stdlib.h>
int main(){
    int T;
    long long A,B,C;
    if(scanf("%d",&T)){}
    for(int i=0;i<T;i++){
        if(scanf("%lld %lld %lld",&A,&B,&C)){}
        if(i!=0)    printf("\n");
        printf("Case #%d: ",i+1);
        if(A+B>C)   printf("true");
        else        printf("false");
    }
    return 0;
}

1012 数字分类

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,s;
    int A[5];
    int A1_sign=1,n[5];
    for(int i=0;i<5;i++){
        A[i]=0;n[i]=0;
    }
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&s)){}
        switch(s%5)
        {
        case 0:
            if(s%2==0){
                A[0]+=s;n[0]++;
            }            
            break;
        case 1:
            A[1]=A[1]+A1_sign*s;
            A1_sign=-A1_sign;
            n[1]++;
            break;
        case 2:
            A[2]++;
            n[2]++;
            break;
        case 3:
            A[3]+=s;n[3]++;
            break;
        case 4:
            if(s>A[4])    A[4]=s;
            n[4]++;
            break;
        default:
            break;
        }
        
    }
    for(int i=0;i<5;i++){
        if(i!=0)    printf(" ");
        if(n[i]!=0){
            if(i!=3)    printf("%d",A[i]);
            else        printf("%.1f",(float)A[i]/n[i]);
        }
        else    printf("N");
    
    }
    return 0;
}

1013 数素数

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isPrime(int k){
    for(int i=(int)sqrt(k);i>1;i--){
        if(k%i==0)
            return 0;
    }
    return 1;
} 
int main(){
    int M,N;
    int i=3,n=1;
    if(scanf("%d %d",&M,&N)){}
    if(M==1)    printf("%d",2);
    while(n<N){
        if(isPrime(i)==1){
            n++;
            if(n>M&&(n-M)%10!=0) printf(" ");
            else if(n>M&&(n-M)%10==0)    printf("\n");
            if(n>=M)printf("%d",i);
        }
        i+=2;
    }
    
    return 0;
}

1014 福尔摩斯的约会

#include<stdio.h>
#include<stdlib.h>
int main(){
    char s[4][61];
    char day[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
    for(int i=0;i<4;i++)    if(scanf("%s",s[i])){}
    int i=0,f=0;
    while(s[0][i]!='\0'&&s[1][i]!='\0'){
        if(s[0][i]==s[1][i]){
            if(f==0&&s[0][i]>='A'&&s[0][i]<='G'){
                printf("%s ",day[s[0][i]-'A']);
                f=1;
            }
            else if(f!=0){
                if(s[0][i]>='0'&&s[0][i]<='9'){
                    printf("%02d:",s[0][i]-'0');
                    break;    
                }
                else if(s[0][i]>='A'&&s[0][i]<='N'){
                    printf("%02d:",s[0][i]-'A'+10);
                    break;
                }
            }
        }
        i++;
    }
    i=0;
    while(s[2][i]!='\0'&&s[3][i]!='\0'){
        if(s[2][i]==s[3][i]&&((s[2][i]>='A'&&s[2][i]<='Z')||(s[2][i]>='a'&&s[2][i]<='z'))){
            printf("%02d",i);
            break;
        }
        i++;
    }
    return 0;
}

1015 德才论

#include<stdio.h>
#include<stdlib.h>
typedef struct stu{
    int num;
    int MGrade;
    int AGrade;
    int sum;
}student;
student Arr[4][100000];
void quicksort(student *A,int start,int end){
    if(start>=end)    return;
   
    int i=start;
    int j=end;
    student f=A[i];
    student t;
    while(i<j){
        while(i<j){
            if(A[j].sum<f.sum)
                j--;
            else if(A[j].sum==f.sum){
                if(A[j].MGrade<f.MGrade)
                    j--;
                else if(A[j].MGrade==f.MGrade&&A[j].num>=f.num)
                    j--;
                else    break;
            }
            else    break;
        }

        t=A[i];
        A[i]=A[j];
        A[j]=t;
        while(i<j){
            if(A[i].sum>f.sum)
                i++;
            else if(A[i].sum==f.sum){
                if(A[i].MGrade>f.MGrade)
                    i++;
                else if(A[i].MGrade==f.MGrade&&A[i].num<=f.num)
                    i++;
                else    break;
            }
            else    break;
        }

        t=A[i];
        A[i]=A[j];
        A[j]=t;    
    }
    quicksort(A,start,i-1);
    quicksort(A,i+1,end);

}
int main(){
    int N,L,H,M=0;
    int num[4];
    for(int i=0;i<4;i++)    num[i]=0;
    if(scanf("%d %d %d",&N,&L,&H)){}
    student a;
    for(int i=0;i<N;i++){
        if(scanf("%d %d %d",&a.num,&a.MGrade,&a.AGrade)){}
        if(a.MGrade>=L&&a.AGrade>=L){
            M++;
            a.sum=a.AGrade+a.MGrade;
            if(a.MGrade>=H&&a.AGrade>=H){
                Arr[0][num[0]]=a;
                num[0]++;
            }
            else if(a.MGrade>=H){
                Arr[1][num[1]]=a;
                num[1]++;
            }
            else if(a.MGrade>=a.AGrade){
                Arr[2][num[2]]=a;
                num[2]++;
            }
            else{
                Arr[3][num[3]]=a;
                num[3]++;
            }
        }
    }
    for(int i=0;i<4;i++)    quicksort(Arr[i],0,num[i]-1);
    printf("%d\n",M);
    for(int i=0;i<4;i++){
        for(int j=0;j<num[i];j++)
            printf("%d %d %d\n",Arr[i][j].num,Arr[i][j].MGrade,Arr[i][j].AGrade);
    }

    return 0;
}

1016 部分A+B

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int num(char *s,char c){
    int k=0;
    while(*s!='\0'){
        if(*s==c)   k++;
        s++;
    }
    return k;
}
int main(){
    char A[10],D_A,B[10],D_B;
    if(scanf("%s %c %s %c",A,&D_A,B,&D_B)){}
    int i,sum=0;
    i=num(A,D_A);
    for(;i>=1;i--)
        sum+=((D_A-'0')*(int)pow(10,i-1));
    i=num(B,D_B);
    for(;i>=1;i--)
        sum+=((D_B-'0')*(int)pow(10,i-1));
    printf("%d",sum);
    return 0;
}

1017 A除以B

#include<stdio.h>
#include<stdlib.h>
int main(){
    char beichushu[1001],shang[1001];
    shang[0]='0';
    int chushu;
    int i=0,j=0;
    int x=0;
    if(scanf("%s %d",beichushu,&chushu)){}
    while(beichushu[i]!='\0'){
        x=x*10+(beichushu[i++]-'0');
        if(j==0&&x/chushu==0)   continue;
        shang[j++]=(x/chushu+'0');
        x=x%chushu;
    }
    if(shang[0]=='0') shang[1]='\0';
    else            shang[j]='\0';
    printf("%s %d",shang,x);
    return 0;
}

1018 锤子剪刀布

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    char A,B;
    int p1,p2;
    int RA[3],RB[3],R[3];
    for(int i=0;i<3;i++){
        R[i]=0;RA[i]=0;RB[i]=0;
    }
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        getchar();
        if(scanf("%c %c",&A,&B)){}
        p1=(A=='B'?0:(A=='C'?1:2));
        p2=(B=='B'?0:(B=='C'?1:2));
        //printf("%d %d\n",p1,p2);
        if((p1+1)%3==p2){
            RA[p1]++;
            R[0]++;
        }
        else if((p2+1)%3==p1){
            RB[p2]++;
            R[2]++;
        }
        else{
            R[1]++;
        }
    }
    printf("%d %d %d\n",R[0],R[1],R[2]);
    printf("%d %d %d\n",R[2],R[1],R[0]);
    int max1=0,max2=0;
    for(int i=1;i<3;i++){
        if(RA[i]>RA[max1])  max1=i;
        if(RB[i]>RB[max2])  max2=i;
    }
    char C[4]={'B','C','J','\0'};
    printf("%c %c",C[max1],C[max2]);
    return 0;
}

1019 数字黑洞

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int comp(const void*a,const void*b){
return *(char *)a-*(char *)b;
}
int main(){
    int N;
    char s[5];
    if(scanf("%d",&N)){}
    sprintf(s,"%04d",N);
    qsort(s,4,sizeof(char),comp);
    int x1,x2;
    while(1){
        x1=0;x2=0;
        for(int i=0;i<4;i++){
            x1=x1+(s[i]-'0')*(int)pow(10,i);
            x2=x2+(s[i]-'0')*(int)pow(10,3-i);
        }
        printf("%04d - %04d = %04d\n",x1,x2,x1-x2);
        
        if(x1-x2==6174||x1-x2==0)   break;
        sprintf(s,"%04d",x1-x2);
        qsort(s,4,sizeof(char),comp);

    }
    return 0;
}

1020 月饼

背包问题,用贪心算法

#include<stdio.h>
#include<stdlib.h>
typedef struct {
    float w;
    float v;
    float x;
}MC;
void quicksort(MC *A,int start,int end){
    if(start>=end)    return; 
    int i=start;
    int j=end;
    MC f=A[i];
    MC t;
    while(i<j){
        while(i<j&&A[j].x<=f.x)  j--;
        t=A[i];
        A[i]=A[j];
        A[j]=t;
        while(i<j&&A[i].x>=f.x)  i++;
        t=A[i];
        A[i]=A[j];
        A[j]=t;    
    }
    quicksort(A,start,i-1);
    quicksort(A,i+1,end);

}
int main(){
    int N;
    float D;
    MC O[1000];
    float income=0.;
    if(scanf("%d %f",&N,&D)){}
    
    for(int i=0;i<N;i++)
        if(scanf("%f",&O[i].w)){}
    for(int i=0;i<N;i++){
        if(scanf("%f",&O[i].v)){
            if(O[i].w!=0.)   O[i].x=O[i].v/O[i].w;
            else             O[i].x=0.;
        }
    }

    quicksort(O,0,N-1);   
    for(int i=0;i<N;i++){
        if(D<O[i].w){
            income+=O[i].x*D;
            break;
        }
        income+=O[i].v;
        D-=O[i].w;
    }
    printf("%.2f",income);
    
    return 0;
}

1021 个位数统计

#include<stdio.h>
#include<stdlib.h>
int main(){
    int count[10];
    for(int i=0;i<10;i++)   count[i]=0;
    char c;
    while(1){
        c=getchar();
        if(c=='\n') break;
        count[c-'0']++;
    }
    for(int i=0;i<10;i++)
        if(count[i]>0)
            printf("%d:%d\n",i,count[i]);
    return 0;
}

1022 D进制的A+B

#include<stdio.h>
#include<stdlib.h>
void change(int X,int D){
    if(X==0){
        printf("%d",0);
        return;
    }
    int R[31],p=-1;
    while (X!=0){
        R[++p]=X%D;
        X=X/D;
    }
    for(int i=p;i>=0;i--)
        printf("%d",R[i]);
    
}
int main(){
    int A,B,D;
    if(scanf("%d %d %d",&A,&B,&D)){}
    change(A+B,D);
    return 0;
}

1023 组个最小数

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N[50];
    int a,s=0;
    for(int i=0;i<10;i++){
        if(scanf("%d",&a)){}
        for(int j=s;j<s+a;j++)  N[j]=i;
        s+=a;
    }
    for(int i=0;i<s;i++){
        if(N[i]!=0){
            int t=N[i];
            N[i]=N[0];
            N[0]=t;
            break;
        }
    }
    for(int i=0;i<s;i++)   printf("%d",N[i]);
    return 0;
}

1024 科学计数法

#include<stdio.h>
#include<stdlib.h>
typedef struct{
    char f;
    char z;
    char x[10000];
    int zh;
}num;
int main(){
    num A;
    if(scanf("%c%c",&A.f,&A.z)){}
    getchar();
    int i=0;
    char c;
    while(1){
        c=getchar();
        if(c=='E'){
            A.x[i]='\0';
            break;
        }
        A.x[i++]=c;
    }
    if(scanf("%d",&A.zh)){}

    if(A.f=='-')  printf("%c",A.f);
    if(A.zh<0){
        printf("0.");
        for(int j=A.zh;j<-1;j++) printf("0");
        printf("%c%s",A.z,A.x);
    }
    else{
        printf("%c",A.z);
        int j,k;
        for(j=A.zh,k=0;j>0&&k<i;j--,k++) printf("%c",A.x[k]);
        for(;j>0;j--)   printf("0");
        if(k<i){
            printf(".");
            printf("%s",(A.x+k));
        }
        

    }
    
    return 0;
}

1025 反转链表

#include<stdio.h>
#include<stdlib.h>
void reverse(int *L,int start,int end){
    int i=start,j=end;
    int t;
    while(i<j){
        t=L[i];L[i]=L[j];L[j]=t;
        i++;j--;
    }
}
int next[100000],data[100000],L[100000];
int main(){
    int F,N,K;
    if(scanf("%d %d %d",&F,&N,&K)){}
    int A;
    for(int i=0;i<N;i++){
        if(scanf("%d",&A)){}
        if(scanf("%d %d",&data[A],&next[A])){}
    }
    int p=F,sum=0;
    while(p!=-1){
        L[sum++]=p;
        p=next[p];
    }
    L[sum]=-1;
    for(int i=0,j=K-1;j<sum;i+=K,j+=K)  reverse(L,i,j);
    for(int i=0;i<sum;i++)  
        L[i+1]==-1?printf("%05d %d %d\n",L[i],data[L[i]],L[i+1]):printf("%05d %d %05d\n",L[i],data[L[i]],L[i+1]);
    return 0;
}

1026 程序运行时间

#include<stdio.h>
#include<stdlib.h>
int main(){
    int C1,C2;
    if(scanf("%d %d",&C1,&C2)){}
    int t=(int)((C2-C1)/100.0+0.5);
    int time[3];
    time[2]=t%60;
    t=t/60;
    time[1]=t%60;
    time[0]=t/60;
    printf("%02d:%02d:%02d",time[0],time[1],time[2]);
    return 0;
}

1027 打印沙漏

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    char c;
    if(scanf("%d %c",&N,&c)){}
    int i=1;
    while(2*i*i-1<=N){
        i++;
    }
    i--;
    for(int j=i;j>0;j--){
        for(int k=0;k<i-j;k++)  printf(" ");
        for(int k=0;k<2*j-1;k++)printf("%c",c);
        printf("\n");
    }
    for(int j=2;j<=i;j++){
        for(int k=0;k<i-j;k++)  printf(" ");
        for(int k=0;k<2*j-1;k++)printf("%c",c);
        printf("\n");
    }
    printf("%d\n",N-2*i*i+1);
    return 0;
}

1028 人口普查

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct p
{
    char name[6];
    char born[11];
};

int main(){
    int N,sum=0;
    if(scanf("%d",&N)){}
    struct p max,min,a;
    char y1[]={"2014/09/06\0"},y2[]={"1814/09/06\0"};
    for(int i=0;i<N;i++){
        if(scanf("%s %s",a.name,a.born)){}
        if(strcmp(a.born,y1)>0||strcmp(a.born,y2)<0)    continue;
        sum++;
        if(sum==1){
            max=a;min=a;
        }
        else{
            if(strcmp(a.born,max.born)<0)   max=a;
            if(strcmp(a.born,min.born)>0)   min=a;
        }
    }
    if(sum>0){
        printf("%d ",sum);
        printf("%s %s",max.name,min.name);
    }
    else    printf("%d",sum);
    return 0;
}

1029 旧键盘

#include<stdio.h>
#include<stdlib.h>
char upper(char c){
    if(c>='a'&&c<='z')  return c-'a'+'A';
    else                return c; 
}
int find(char *s,char c){
    int i=0;
    while(s[i]!='\0'){
        if(s[i]==c) return i;
        i++;
    }
    return -1;
}
int main(){
    char a[81],b[81],ans[28];
    ans[0]='\0';
    if(scanf("%s %s",a,b)){}
    int i=0,j=0,k=0;
    while(a[i]!='\0'){
        if(a[i]==b[j]){
            i++;j++;
        }
        else{
            if(find(ans,upper(a[i]))==-1){
                ans[k++]=upper(a[i]);
                ans[k]='\0';
            }
            i++;
        }
    }
    printf("%s",ans);
    
    return 0;
}

1030 完美数列

#include<stdio.h>
#include<stdlib.h>
int comp(const void *a,const void *b){
    return *(int *)a-*(int *)b;
}

int main(){
    int N,a[100000];
    long long p;
    if(scanf("%d %lld",&N,&p)){}
    for(int i=0;i<N;i++)
        if(scanf("%d",&a[i])){}
    qsort(a,N,sizeof(int),comp);
    int t,max=0;
    for(int i=0;i<N;i++){
        t=max;
        for(int j=i+max;j<N;j++){
            if(a[i]*p>=a[j])    t++;
            else                break;
        }
        if(t>max)   max=t;
    }
    printf("%d",max);
    return 0;
}

1031 查验身份证

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    char sfz[19];
    int w[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    int z,m,sum=0,f;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%s",sfz)){}
        z=0,f=0;
        for(int j=0;j<17;j++){
            if(sfz[j]<'0'||sfz[j]>'9'){
                f=1;
                break;
            }
            z+=(sfz[j]-'0')*w[j];
        }
        if(f==0){
            z=z%11;
            m=sfz[17]=='X'?10:sfz[17]-'0';
            if((10-z+2)%11!=m)  f=1;
        }
        if(f==1){
            sum++;
            printf("%s\n",sfz);
        }
    }
    if(sum==0)  printf("All passed");
    
    return 0;
}

1032 挖掘机技术哪家强

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,b,g;
    int s[100001];
    for(int i=0;i<100001;i++)   s[i]=0;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d %d",&b,&g)){}
        s[b]+=g;
    }
    int max=1;
    for(int i=2;i<100001;i++){
        if(s[max]<s[i]) max=i;
    }
    printf("%d %d",max,s[max]);
    return 0;
}

1033 旧键盘打字

#include<stdio.h>
#include<stdlib.h>
char upper(char c){
    if(c>='a'&&c<='z')  return c-'a'+'A';
    return c;
}
int exist(char *s,char c){
    int i=0;
    while (s[i]!='\0')
    {
        if(s[i]==c) break;
        i++;
    }
    if(s[i]=='\0')  return 1;
    return 0;
    
}
int main(){
    char d[42],t[100001];
    char c=getchar();
    if(c!='\n'){
        d[0]=c;
        if(scanf("%s",&d[1])){}
    }
    else   d[0]='\0';    
    if(scanf("%s",t)){}
    int i=0,f=0;
    while(d[i]!='\0'){
        if(d[i]=='+'){
            f=1;
            break;
        }
        i++;
    }

    i=0;
    while(t[i]!='\0'){
        if(exist(d,upper(t[i]))){
            if(f==0)    printf("%c",t[i]);
            else if(f==1&&(t[i]<'A'||t[i]>'Z'))  printf("%c",t[i]);
        }
        i++;
    }
    printf("\n");
    return 0;
}

1034 有理数四则运算

#include<stdio.h>
#include<stdlib.h>
long long gcd(long long a,long long b){
    if(b==0)    return 1;
    if(a%b!=0){
        return gcd(b,a%b);
    }
    return b;
}
void yf(long long*a,long long *b){
    if(*b<0){
        *b=-*b;
        *a=-*a;
    }
    long long y;
    if(*a<0)    y=gcd(-*a,*b);
    else        y=gcd(*a,*b);
    *a=*a/y;
    *b=*b/y;
}
void show(long long a,long long b){
    if(b==0){
        printf("Inf");
        return;
    }
    if(a%b==0){
        if(a/b>=0)
            printf("%lld",a/b);
        else
            printf("(%lld)",a/b);
        return;
    }
    if(a<0){
        if(a/b==0)  printf("(%lld/%lld)",a,b);
        else        printf("(%lld %lld/%lld)",a/b,-a+a/b*b,b);
    }
    else{
        if(a/b==0)  printf("%lld/%lld",a,b);
        else        printf("%lld %lld/%lld",a/b,a-a/b*b,b);
    }
}

int main(){
    long long a1,b1,a2,b2;
    long long r1,r2;
    if(scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2));
    yf(&a1,&b1);yf(&a2,&b2);

    r1=a1*b2+a2*b1;
    r2=b1*b2;
    yf(&r1,&r2);
    show(a1,b1);printf(" + ");show(a2,b2);printf(" = ");show(r1,r2);printf("\n");

    r1=a1*b2-a2*b1;
    r2=b1*b2;
    yf(&r1,&r2);
    show(a1,b1);printf(" - ");show(a2,b2);printf(" = ");show(r1,r2);printf("\n");

    r1=a1*a2;
    r2=b1*b2;
    yf(&r1,&r2);
    show(a1,b1);printf(" * ");show(a2,b2);printf(" = ");show(r1,r2);printf("\n");

    r1=a1*b2;
    r2=a2*b1;
    yf(&r1,&r2);
    show(a1,b1);printf(" / ");show(a2,b2);printf(" = ");show(r1,r2);printf("\n");
    return 0;
}

1035 插入与归并

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,a[100],b[100];
    int i,j;
    if(scanf("%d",&N)){}
    for(i=0;i<N;i++)
        if(scanf("%d",&a[i])){}
    for(i=0;i<N;i++)
        if(scanf("%d",&b[i])){}
    i=0;
    while(i<N-1){
        if(b[i]>b[i+1])
            break;
        i++;
    }
    i++;
    j=i;
    while(i<N){
        if(a[i]!=b[i])  break;
        i++;
    }
    if(i==N){
        printf("Insertion Sort\n");
        int key=b[j];
        j--;
        while(j>=0&&b[j]>key){
            b[j+1]=b[j];
            j--;
        }
        b[j+1]=key;
        for(j=0;j<N;j++)    j==N-1?printf("%d\n",b[j]):printf("%d ",b[j]);
    }
    else{
        printf("Merge Sort\n");
        int s1=0,mid=-1,s2=-1,k=-1,end=-1;
        int t[100];
        while(1){
            for(k=j;k<N;k++){
                end=(k+j<=N)?(k+j):N;
                while(k<end-1){
                    if(b[k]>b[k+1]) break;
                    k++;
                }
                if(k!=end-1)    break;   
            }
            if(k!=end)    j=j/2;
            else          break;
        }
        while (s1<N){
            if(N-s1<=j) break;
            mid=s1+j;s2=mid;
            end=((mid+j)<=N)?(mid+j):N;
            k=0;
            while(s1<mid&&s2<end){
                if(b[s1]<=b[s2])    t[k++]=b[s1++];
                else                t[k++]=b[s2++]; 
            }
            while(s1<mid){
                t[k++]=b[s1++];
            }
            while(s2<end){
                t[k++]=b[s2++]; 
            }
            for(k--;k>=0;k--){
                b[s1-j+k]=t[k];
            }
            s1+=j;
        }
        for(j=0;j<N;j++)    j==N-1?printf("%d\n",b[j]):printf("%d ",b[j]);
    }
    return 0;
}

1036 跟奥巴马一起编程

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    char c;
    if(scanf("%d %c",&N,&c)){}
    for(int i=0;i<N;i++)    printf("%c",c);
    printf("\n");
    for(int i=2;i<(int)(N/2.0+0.5);i++){
        printf("%c",c);
        for(int j=2;j<N;j++)  printf(" ");
        printf("%c\n",c);
    }
    for(int i=0;i<N;i++)    printf("%c",c);
    printf("\n");
    return 0;
}

1037 在霍格沃茨找零钱

#include<stdio.h>
#include<stdlib.h>
int main(){
    int P[3],A[3];
    if(scanf("%d.%d.%d",&P[0],&P[1],&P[2])&&scanf("%d.%d.%d",&A[0],&A[1],&A[2])){}
    int p=P[0]*17*29+P[1]*29+P[2];
    int a=A[0]*17*29+A[1]*29+A[2];
    int r=a-p,R[3],f=1;
    if(r<0) f=-1;
    R[2]=r%29;
    r=r/29; 
    R[1]=r%17;
    R[0]=r/17;
    f==1?printf("%d.%d.%d",R[0],R[1],R[2]):printf("%d.%d.%d",R[0],-R[1],-R[2]);

    return 0;
}

1038 统计同成绩学生

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,score[101],K;
    for(int i=0;i<=100;i++) score[i]=0;
    if(scanf("%d",&N)){}
    int t;
    for(int i=0;i<N;i++){
        if(scanf("%d",&t))
            score[t]++;
    }
    if(scanf("%d",&K)){}
    for(int i=0;i<K;i++){
        if(scanf("%d",&t)){}
        if(i!=K-1)
            printf("%d ",score[t]);
        else
            printf("%d\n",score[t]);
    }
    return 0;
}

1039 到底买不买

#include<stdio.h>
#include<stdlib.h>
int main(){
    char a[1001],b[1001];
    int s[124],r=0;
    for(int i=0;i<124;i++)  s[i]=0;
    if(scanf("%s %s",a,b)){}
    int i=0,j=0;
    while(a[i]!='\0'){
        s[a[i]]++;
        i++;
    }
    while(b[j]!='\0'){
        if(s[b[j]]>0)
            s[b[j]]--;
        else
            r++;
        j++;
    }
    if(r>0)
        printf("No %d\n",r);
    else
        printf("Yes %d\n",i-j);
    return 0;
}

1040 有几个PAT

#include<stdio.h>
#include<stdlib.h>
int main(){
    int p=0,pa=0,pat=0;
    char c;
    while ((c=getchar())){
        if(c=='\n') break;
        if(c=='P')  p++;
        if(c=='A')  pa=(pa+p)%1000000007;
        if(c=='T')  pat=(pat+pa)%1000000007;
    }
    printf("%d\n",pat);
    return 0;
}

1041 考试座位号

#include<stdio.h>
#include<stdlib.h>
struct Info{
    char zkzh[17];
    int s;
    int k;
};
struct Info S[1000];
int main(){
    int N,M;
    if(scanf("%d",&N)){}
    struct Info t;
    for(int i=0;i<N;i++)
        if(scanf("%s %d %d",&t.zkzh,&t.s,&t.k))
            S[t.s]=t;
    if(scanf("%d",&M)){}
    for(int i=0;i<M;i++)
        if(scanf("%d",&t.s))
            printf("%s %d\n",S[t.s].zkzh,S[t.s].k);
    return 0;
}

1042 字符统计

#include<stdio.h>
#include<stdlib.h>
int main(){
    int count[128],max='a';
    for(int i=0;i<128;i++)  count[i]=0;
    char c;
    while((c=getchar())){
        if(c=='\n') break;
        if(c>='a'&&c<='z')
            count[(int)c]++;
        else if(c>='A'&&c<='Z')
            count[c-'A'+'a']++;
    }
    for(int i='a';i<'z';i++)
        if(count[i]>count[max])
            max=i;
    printf("%c %d\n",max,count[max]);
    return 0;
}

1043 输出PATest

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int count[58] = {0};
    char c;
    while ((c = getchar()))
    {
        if (c == '\n')
            break;
        count[c - 'A']++;
    }
    while (count['P' - 'A'] > 0 || count['A' - 'A'] > 0 || count['T' - 'A'] > 0 || count['e' - 'A'] > 0 || count['s' - 'A'] > 0 || count['t' - 'A'] > 0)
    {
        if (count['P' - 'A']-- > 0)
            printf("P");
        if (count['A' - 'A']-- > 0)
            printf("A");
        if (count['T' - 'A']-- > 0)
            printf("T");
        if (count['e' - 'A']-- > 0)
            printf("e");
        if (count['s' - 'A']-- > 0)
            printf("s");
        if (count['t' - 'A']-- > 0)
            printf("t");
    }
    printf("\n");
    return 0;
}

1044 火星数字

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char a[][4]={"jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char b[][4]={"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
void trans1(char *s){
    
    int i=0,sum=0;
    while(s[i]!='\0'){
        sum=sum*10+(s[i++]-'0');
    }
    if(sum==0){
        printf("tret\n");
        return;
    }
    if(sum/13>0){
        printf("%s",b[sum/13-1]);
        if(sum%13!=0)    printf(" %s\n",a[sum%13-1]);
        else             printf("\n");
    }
    else
        printf("%s\n",a[sum%13-1]);
}
void trans2(char *s){
    if(strcmp(s,"tret\0")==0){
        printf("%d\n",0);
        return;
    }
    int s1=0,s2=0;
    if(s[3]==' '){
        s[3]='\0';
        for(int i=0;i<12;i++){
            if(strcmp(s,b[i])==0)
                s1=i+1;
            if(strcmp(&s[4],a[i])==0)
                s2=i+1;
        }
    }
    else
        for(int i=0;i<12;i++){
            if(strcmp(s,a[i])==0)
                s2=i+1;
            if(strcmp(s,b[i])==0)
                s1=i+1;
        }
    printf("%d\n",s1*13+s2);
}
int main(){
    int N;
    char s[10];
    if(scanf("%d\n",&N)){}
    for(int i=0;i<N;i++){
        char c;
        int j=0;
        while((c=getchar())){
            if(c=='\n') break;
            s[j++]=c;
        }
        s[j]='\0';
        if(s[0]>='0'&&s[0]<='9')
            trans1(s);
        else
            trans2(s);
    }
    return 0;
}

1045 快速排序

#include<stdio.h>
#include<stdlib.h>
int comp(const void*a,const void*b){
    return *(int *)a-*(int *)b;
}
int main(){
    int N,a[100000],b[100000],r[100000],sum=0,max;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++)
        if(scanf("%d",&a[i])){
            b[i]=a[i];
        }
    qsort(b,N,sizeof(int),comp);
    max=a[0];
    for(int i=0;i<N;i++){
        if(a[i]>max)    max=a[i];
        if(a[i]==max&&a[i]==b[i])
            r[sum++]=b[i];
    }
    printf("%d\n",sum);
    for(int i=0;i<sum;i++){
        if(i!=0)    printf(" ");
        printf("%d",r[i]);
    }
    printf("\n");
    return 0;
}

1046 划拳

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,a1,a2,b1,b2,s1=0,s2=0;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d %d %d %d",&a1,&a2,&b1,&b2)){}
        if((a2==a1+b1)^(b2==a1+b1)){
            if(a2==a1+b1)   s2++;
            else            s1++;
        }
    }
    printf("%d %d\n",s1,s2);
    return 0;
}

1047 编程团体赛

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,score[1001]={0};
    int t,b,s,max=0;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d-%d %d",&t,&b,&s)){}
        score[t]+=s;
    }
    for(int i=0;i<1001;i++){
        if(score[i]>score[max]) max=i;
    }
    printf("%d %d\n",max,score[max]);
    return 0;
}

1048 数字加密

#include<stdio.h>
#include<stdlib.h>
#define max(a,b) a>b?a:b;
int main(){
    char A[101],B[101],R[101],re[]={'0','1','2','3','4','5','6','7','8','9','J','Q','K'};
    int len1=0,len2=0,len;
    if(scanf("%s %s",A,B)){}
    while(A[len1]!='\0')    len1++;
    while(B[len2]!='\0')    len2++;
    len=max(len1,len2);
    R[len]='\0';
    int i,j,k=0;
    for(i=len1-1,j=len2-1;i>=0&&j>=0;i--,j--){
        if((k+1)%2==1)    R[k++]=re[(A[i]-'0'+B[j]-'0')%13];
        else R[k++]=re[B[j]-A[i]<0?B[j]-A[i]+10:B[j]-A[i]];
    }
    while(j>=0){
        R[k++]=B[j--];
    }
    while(i>=0){
        if((k+1)%2!=1)   R[k++]=A[i]=='0'?'0':'0'-A[i]+10+'0';
        else                R[k++]=A[i];
        i--;
    }
    for(i=len-1;i>=0;i--)
        printf("%c",R[i]);
    return 0;
}

1049 数列的片段和

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    double a;
    long long sum=0;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%lf",&a)){}
        sum+=(long long)(a*1000)*(N-i)*(i+1);
    }
    printf("%.2lf",(double)sum/1000);
    return 0;
}

1050 螺旋矩阵

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int comp(const void *a,const void *b){
    return *(int *)b-*(int *)a;
}
int a[10000]={0};
int main(){
    int N,m,n;
    if(scanf("%d",&N)){}
    for(n=sqrt((double)N);n>=1;n--)
        if(N%n==0){
            m=N/n;
            break;
        }
    for(int i=0;i<N;i++)
        if(scanf("%d",&a[i])){}
    qsort(a,N,sizeof(int),comp);
    int **b=(int **)malloc(sizeof(int *)*m);
    for(int i=0;i<m;i++)
        b[i]=(int *)malloc(sizeof(int)*n);
    int t=0;
    for(int i=0;i<m/2+m%2;i++){
        for(int j=i;j<n-i&&t<N;j++){
            b[i][j]=a[t++];
        }
        for(int k=i+1;k<m-i-1&&t<N;k++){
            b[k][n-1-i]=a[t++];
        }
        for(int l=n-i-1;l>=i&&t<N;l--){
            b[m-i-1][l]=a[t++];
        }
        for(int o=m-i-2;o>=i+1&&t<N;o--){
            b[o][i]=a[t++];
        }
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(j!=0)    printf(" ");
            printf("%d",b[i][j]);
        }
        printf("\n");
    }
    for(int i=0;i<m;i++)
        free(b[i]);
    free(b);
    return 0;
}

1051 复数乘法

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
    double R1,P1,R2,P2,A,B;
    if(scanf("%lf %lf %lf %lf",&R1,&P1,&R2,&P2)){}
    A=(cos(P1)*cos(P2)-sin(P1)*sin(P2))*R1*R2;
    B=(cos(P1)*sin(P2)+sin(P1)*cos(P2))*R1*R2;
    if(A>-0.005&&A<0)
        printf("0.00");
    else
        printf("%.2f",A);
    if(B>0) printf("+%.2fi\n",B);
    else if(B>-0.005&&B<0)   printf("+0.00i\n");
    else    printf("%.2fi\n",B);
    return 0;
}

1052 卖个萌

#include<stdio.h>
#include<stdlib.h>
int main(){
    char s[3][10][5],t;
    int len[3];
    int i,j,k,N;
    for(k=0;k<3;k++){
        i=0;
        while((t=getchar())){
            if(t=='\n')  break;
            if(t=='['){
                j=0;
                while((t=getchar())){
                    if(t==']')  break;
                    s[k][i][j]=t;
                    j++;
                }
                s[k][i][j]='\0';
                i++;
            }
        }
        len[k]=i;

    }
    if(scanf("%d",&N)){}
    for(i=0;i<N;i++){
        int a,b,c,d,e;
        if(scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)){}
        if(a>len[0]||a<1||b>len[1]||b<1||c>len[2]||c<1||d>len[1]||d<1||e>len[0]||e<1)
            printf("Are you kidding me? @\\/@\n");
        else
            printf("%s(%s%s%s)%s\n",s[0][a-1],s[1][b-1],s[2][c-1],s[1][d-1],s[0][e-1]);
    }
    return 0;
}

1053 住房空置率

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,D,K;
    double e,E,b1=0,b2=0;
    if(scanf("%d %lf %d",&N,&e,&D)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&K)){}
        int d=0;
        for(int j=0;j<K;j++){
            if(scanf("%lf",&E)){}
            if(E<e) d++;
        }
        if(d>(K/2)){
            if(K>D) b2++;
            else    b1++;
        }
    }
    printf("%.1f%% %.1f%%\n",b1/N*100,b2/N*100);
    return 0;
}

1054 求平均值

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
    int N,lN=0;
    char numa[100],numb[100];
    double sum=0,b;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%s",numa)){}
        sscanf(numa,"%lf",&b);
        sprintf(numb,"%.2f",b);
        int j=0,f=1;
        while(numa[j]!='\0'){
            if(numa[j]!=numb[j])
                f=0;  
            j++;
        }
        if(b>1000||b<-1000) f=0;
        if(f==0)
            printf("ERROR: %s is not a legal number\n",numa);
        else{
            sum+=b;
            lN++;
        }
    }
    if(lN>1)
        printf("The average of %d numbers is %.2f\n",lN,sum/lN);
    else if(lN==1)
        printf("The average of 1 number is %.2f\n",sum);
    else
        printf("The average of 0 numbers is Undefined\n");
    return 0;
    
}

1055 集体照

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
    char name[10];
    int height;
}P;
int comp(const void *a,const void *b){
    if((*(P *)b).height-(*(P *)a).height!=0)
        return (*(P *)b).height-(*(P *)a).height;
    else
        return strcmp((*(P *)a).name,(*(P *)b).name);
}
int main(){
    int N,K;
    if(scanf("%d %d",&N,&K)){}
    P *T=( P *)malloc(sizeof(P)*N);
    for(int i=0;i<N;i++){
        if(scanf("%s %d",T[i].name,&T[i].height)){}
    }
    qsort(T,N,sizeof(P),comp);
    int L=N/K+N%K,O=N/K;
    for(int j=L-2+(L+1)%2;j>=0;j-=2){
        if(j!=L-2+(L+1)%2)
            printf(" ");
        printf("%s",T[j].name);
    }
    for(int j=0;j<=L-2+O%2;j+=2){
        if(N!=1)
            printf(" %s",T[j].name);
        else
            printf("%s",T[j].name);
    }
    printf("\n");
    for(int i=0;i<K-1;i++){
        for(int j=O-2+(O+1)%2;j>=0;j-=2){
            if(j!=O-2+(O+1)%2)
                printf(" ");
            printf("%s",T[j+L+i*O].name);
        }
        for(int j=0;j<=O-2+O%2;j+=2){
            printf(" ");
            printf("%s",T[j+i*O+L].name);
        }
        printf("\n");
    }
    free(T);
    return 0;
}

1056 组合数的和

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,t,sum=0;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&t))
            sum+=(N-1)*t*11;
    }
   
    printf("%d\n",sum);
    return 0;
}

1057 数零壹

#include<stdio.h>
#include<stdlib.h>
int main(){
    char c;
    int s=0,y=0,l=0;
    while((c=getchar())){
        if(c=='\n') break;
        else if(c>='a'&&c<='z')  s+=c-'a'+1;
        else if(c>='A'&&c<='Z')  s+=c-'A'+1;
    }
    if(s){
        while(s!=1&&s!=0){
            if(s%2==1)  y++;
            else        l++;
            s=s/2;
        }
        y++;
    }
    printf("%d %d\n",l,y);
    return 0;
}

1058 选择题

#include<stdio.h>
#include<stdlib.h>
typedef struct{
    int bh;
    int point;
    int Ans;
    int miss;
}Qes;
int comp(const void *a,const void *b){
    if((*(Qes *)b).miss-(*(Qes *)a).miss!=0)
        return (*(Qes *)b).miss-(*(Qes *)a).miss;
    else
        return (*(Qes *)a).bh-(*(Qes *)b).bh;
}
int A[]={1,2,4,8,16};
int main(){
    int N,M;
    if(scanf("%d %d",&N,&M)){}
    Qes *Q=(Qes *)malloc(sizeof(Qes)*M);
    for(int i=0;i<M;i++){
        int d,l;
        if(scanf("%d %d %d",&Q[i].point,&d,&l)){}
        Q[i].Ans=0,Q[i].miss=0,Q[i].bh=i+1;
        for(int j=0;j<l;j++){
            getchar();
            Q[i].Ans+=A[getchar()-'a'];
        }
    }
    getchar();
    for(int i=0;i<N;i++){
        int score=0;
        for(int j=0;j<M;j++){
            int l,Ans=0;
            getchar();
            if(scanf("%d",&l)){}
            getchar();
            for(int k=0;k<l;k++){
                Ans+=A[getchar()-'a'];
                getchar();
            }
            getchar();
            if(Ans==Q[j].Ans)   score+=Q[j].point;
            else                Q[j].miss+=1;
        }
        printf("%d\n",score);
    }
    qsort(Q,M,sizeof(Qes),comp);
    if(Q[0].miss!=0){
        printf("%d",Q[0].miss);
        int i=0;
        while(Q[i].miss==Q[0].miss){
            printf(" %d",Q[i].bh);
            i++;
            if(i>=M)    break;
        }
    }
    else
        printf("Too simple");
    printf("\n");
    free(Q);
    return 0;
}

1059 C语言竞赛

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isPrime(int x){
    int t=sqrt((double)x);
    if(x<2) return 0;
    for(int i=t;i>1;i--)
        if(x%i==0) return 0;
    return 1;
}
int main(){
    int N,K,p[2][10000]={0},t,mc;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&t)){}
            p[0][t]=i+1;
    }
    if(scanf("%d",&K)){}
    for(int i=0;i<K;i++){
        if(scanf("%d",&t)){}
        mc=p[0][t];
        printf("%4d: ",t);
        if(mc==0)   printf("Are you kidding?\n");
        else if(p[1][t]==1) printf("Checked\n");
        else if(mc==1){
            printf("Mystery Award\n");
            p[1][t]=1;
        }
        else if(isPrime(mc)){
            printf("Minion\n");
            p[1][t]=1;
        }
        else{
            printf("Chocolate\n");
            p[1][t]=1;
        }
    }
    return 0;
}

1060 爱丁顿数

#include<stdio.h>
#include<stdlib.h>
int comp(const void *a,const void *b){
    return *(int *)b-*(int *)a;
}
int main(){
    int N,A[100000];
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d",&A[i])){}
    }
    qsort(A,N,sizeof(int),comp);
    int max=0,i;
    for(i=0;i<N;i++){
        if(A[i]<=i+1) break;
    }
    max=i;
    printf("%d\n",max);
    return 0;
}

1061 判断题

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,M,score[100],solu[100];
    if(scanf("%d %d",&N,&M)){}
    for(int i=0;i<M;i++)
        if(scanf("%d",&score[i])){}
    for(int i=0;i<M;i++)
        if(scanf("%d",&solu[i])){}
    for(int i=0;i<N;i++){
        int s,sum=0;
        for(int j=0;j<M;j++){
            if(scanf("%d",&s)){}
            if(s==solu[j])  sum+=score[j];
        }
        printf("%d\n",sum);
    }
    return 0;
}

1062 最简分数

#include<stdio.h>
#include<stdlib.h>
void swap(int *a,int *b){
    int t=*a;
    *a=*b;
    *b=t;
}
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    int N1,N2,M1,M2,K,KN;
    if(scanf("%d/%d %d/%d %d",&N1,&M1,&N2,&M2,&K)){}
    
    if(N1*M2-N2*M1>0){
        swap(&N1,&N2);
        swap(&M1,&M2);
    }
    KN=N1*K/M1+1;
    int f=0;
    while(KN*M2-N2*K<0){
        if(gcd(KN,K)==1){
            if(f==0)    f=1;
            else        printf(" ");
            printf("%d/%d",KN,K);
        }
        KN++;
    }
    printf("\n");
    return 0;
}

1063 计算谱半径

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
    int N,a,b;
    float max=0,t;
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        if(scanf("%d %d",&a,&b)){}
        t=sqrt(a*a+b*b);
        if(t>max) max=t;
    }
    printf("%.2f\n",max);

    return 0;
}

1064 朋友数

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,f[40]={0};
    char c;
    if(scanf("%d\n",&N)){}
    int t=0,n=0;
    while((c=getchar())){
        if(c==' '||c=='\n'){
            if(f[t]==0){
                n++;
                f[t]=1;
            }
            if(c=='\n') break;
            t=0;
        }
        else
            t+=c-'0';
    }
    printf("%d\n",n);
    int flag=0;
    for(int i=0;i<40;i++){
        if(f[i]!=0){
            if(flag)    printf(" ");
            printf("%d",i);
            flag=1;
        }
    }
    printf("\n");
    return 0;
}

1065 单身狗

#include <stdio.h>
#include <stdlib.h>
int comp(const void *a,const void *b){
    return *(int *)a-*(int *)b;
}
int main(){
    int N, M, couple[100000],present[100000];
    for(int i = 0; i < 100000; i++){
        couple[i]=-1;
        present[i]=0;
    }
    int a, b;
    if(scanf("%d", &N)){}
    for(int i = 0; i < N; i++){
        if(scanf("%d %d", &a, &b)){}
        couple[a] = b;
        couple[b] = a;
    }
    if(scanf("%d", &M)){}
    int *guest = (int *)malloc(sizeof(int) * M);
    int *result = (int *)malloc(sizeof(int) * M);
    int n=0;
    for(int i = 0; i < M; i++){
        if(scanf("%d", &guest[i])){}
        present[guest[i]]=1;
    }
    for(int i = 0; i < M; i++){
        if(couple[guest[i]]==-1){
            result[n++]=guest[i];
        }
        else if(present[couple[guest[i]]]==0){
            result[n++]=guest[i];
        }
    }
    qsort(result,n,sizeof(int),comp);
    printf("%d",n);
    if(n>0) printf("\n");
    for(int i=0;i<n;i++){
        if(i!=0)    printf(" ");
        printf("%05d",result[i]);
    }
    printf("\n");
    free(guest);
    free(result);
    return 0;
}

1066 图像过滤

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,M,A,B,R;
    if(scanf("%d %d %d %d %d",&M,&N,&A,&B,&R)){}
    int a;
    for(int i=0;i<M;i++){
        for(int j=0;j<N;j++){
            if(scanf("%d",&a)){}
            if(j!=0)    printf(" ");
            if(a>=A&&a<=B)
                printf("%03d",R);
            else
                printf("%03d",a);
        }
        printf("\n");
    }
    return 0;
}

1067 试密码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    char pw[21],t[100],c;
    int N,i=1,j;
    if(scanf("%s %d",pw,&N)){}
    getchar();
    while(1){
        j=0;
        while((c=getchar())){
            if(c=='\n') break;
            t[j++]=c;
        }
        t[j]='\0';
        if(strcmp(t,"#\0")==0) break;
        if(i<=N){
            if(strcmp(pw,t)==0){
                printf("Welcome in\n");
                break;
            }
            else{
                printf("Wrong password: %s\n",t);
                if(i==N){
                    printf("Account locked\n");
                    break;
                }
            }
            
        }
        i++;
    }
    return 0;
}

1068 万绿丛中一点红

#include<stdio.h>
#include<stdlib.h>
int M,N,TOL,uq[16777217];
int check(int **Image,int i,int j){
    if(i-1>=0&&j-1>=0&&abs(Image[i-1][j-1]-Image[i][j])<=TOL) return 0;
    if(j-1>=0&&abs(Image[i][j-1]-Image[i][j])<=TOL) return 0;
    if(i+1<N&&j-1>0&&abs(Image[i+1][j-1]-Image[i][j])<=TOL) return 0;
    if(i-1>=0&&abs(Image[i-1][j]-Image[i][j])<=TOL) return 0;
    if(i+1<N&&abs(Image[i+1][j]-Image[i][j])<=TOL) return 0;
    if(i-1>=0&&j+1<M&&abs(Image[i-1][j+1]-Image[i][j])<=TOL) return 0;
    if(j+1<M&&abs(Image[i][j+1]-Image[i][j])<=TOL) return 0;
    if(i+1<N&&j+1<M&&abs(Image[i+1][j+1]-Image[i][j])<=TOL) return 0;
    return 1;
}
int main(){
    int x=-1,y=-1,cnt=0;
    if(scanf("%d %d %d",&M,&N,&TOL)){}
    int **Iamge=(int **)malloc(sizeof(int *)*N);
    for(int i=0;i<N;i++)
        Iamge[i]=(int *)malloc(sizeof(int)*M);
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            if(scanf("%d",&Iamge[i][j])){}
            uq[Iamge[i][j]]++;
        }
    }
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            if(uq[Iamge[i][j]]==1&&check(Iamge,i,j)){
                cnt++;
                x=i;
                y=j;
            }
                
        }
    }
    if(cnt==1)      printf("(%d, %d): %d\n",y+1,x+1,Iamge[x][y]);
    else if(cnt==0) printf("Not Exist\n");
    else            printf("Not Unique\n");
    for(int i=0;i<N;i++)
        free(Iamge[i]);
    free(Iamge);
    return 0;
}

1069 微博转发抽奖

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int M,N,S,n=0;
    char U[21],L[1000][21];
    if(scanf("%d %d %d",&M,&N,&S)){}
    for(int i=1;i<=M;i++){
        if(scanf("%s",U)){}
        if(i==S){
            int j;
            for(j=0;j<n;j++){
                if(strcmp(L[j],U)==0)
                    break;
            }
            if(j==n){
                strcpy(L[n],U);
                n++;
                S+=N;
            }
            else
                S+=1;
        }
    }
    if(n==0)    printf("Keep going...\n");
    else{
        for(int i=0;i<n;i++)
            printf("%s\n",L[i]);
    }
    return 0;
}

1070 结绳

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b){
    return *(int *)a-*(int *)b;
}
int main(){
    int N;
    double L;
    if(scanf("%d",&N)){}
    int *d=(int *)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
        if(scanf("%d",&d[i])){}
    qsort(d,N,sizeof(int),cmp);
    L=d[0];
    for(int i=1;i<N;i++){
        L=(L+d[i])/2;
    }
    printf("%d\n",(int)L);
    free(d);
    return 0;
}

1071 小赌怡情

#include<stdio.h>
#include<stdlib.h>
int main(){
    int T,K,n1,b,t,n2;
    if(scanf("%d %d",&T,&K)){}
    for(int i=0;i<K;i++){
        if(scanf("%d %d %d %d",&n1,&b,&t,&n2)){}
        if(t>T)
            printf("Not enough tokens.  Total = %d.\n",T);
        else if((n2>n1&&b==1)||(n1>n2&&b==0)){
            T+=t;
            printf("Win %d!  Total = %d.\n",t,T);
        }
        else{
            T-=t;
            printf("Lose %d.  Total = %d.\n",t,T);
        }
        if(T<=0){
            printf("Game Over.\n");
            break;
        }
    }
    return 0;
}

1072 开学寄语

#include<stdio.h>
#include<stdlib.h>
int N,M,Obj[6];
int check(int obj){
    for(int i=0;i<M;i++){
        if(obj==Obj[i])
            return 1;
    }
    return 0;
}
int main(){
    int n,obj,c=0,cnt=0;
    char Stu[5];
    if(scanf("%d %d",&N,&M)){}
    for(int i=0;i<M;i++)
        if(scanf("%d",&Obj[i])){}
    for(int i=0;i<N;i++){
        if(scanf("%s %d",Stu,&n)){}
        int k=0;
        for(int j=0;j<n;j++){
            if(scanf("%d",&obj)){}
            if(check(obj)){
                k++;
                if(k==1){
                    printf("%s:",Stu);
                    c++;
                }
                printf(" %04d",obj);
                cnt++;
            }
        }
        if(k>0)
            printf("\n");
    }
    printf("%d %d\n",c,cnt);
    return 0;
}

1073 多选题常见计分法

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    int score;
    int ans[5];
} Qes;
Qes L[100];
int A[500];
int main()
{
    int N, M;
    if (scanf("%d %d", &N, &M)) { }
    for (int i = 0; i < M; i++) {
        int s;
        if (scanf("%d %d %d", &L[i].score, &s, &s)) { }
        getchar();
        for (int j = 0; j < s; j++) {
            L[i].ans[getchar() - 'a'] = 1;
            getchar();
        }
    }
    for (int i = 0; i < N; i++) {
        float sum = 0;
        int s;
        for (int j = 0; j < M; j++) {
            getchar();
            if (scanf("%d", &s)) { }
            getchar();
            int c[5] = { 0 };
            for (int k = 0; k < s; k++) {
                c[getchar() - 'a'] = 1;
                getchar();
            }
            for (int k = 0; k < 5; k++) {
                //printf("%d ",c[k]);
            }
            //printf("\n");
            float f = 1.0;
            for (int k = 0; k < 5; k++) {
                if (c[k] == 1 && L[j].ans[k] == 0) {
                    A[j * 5 + k]++;
                    f = 0.;
                } else if (c[k] == 0 && L[j].ans[k] == 1) {
                    A[j * 5 + k]++;
                    if (f == 1.0)
                        f = 0.5;
                }
            }
            getchar();
            sum += f * L[j].score;
        }
        printf("%.1f\n", sum);
    }
    int max = 0;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < 5; j++)
            if (max < A[i * 5 + j])
                max = A[i * 5 + j];
    }
    if (max != 0) {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < 5; j++)
                if (max == A[i * 5 + j])
                    printf("%d %d-%c\n", A[i * 5 + j], i + 1, j + 'a');
        }
    } else
        printf("Too simple\n");
    return 0;
}

1074 宇宙无敌加法器

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char *s){
    int len=strlen(s);
    for(int i=0,j=len-1;i<j;i++,j--){
        char t=s[i];
        s[i]=s[j];
        s[j]=t;
    }
}
void Add(char *A,char *B,char *BS){
    char r[22];
    int t,f=0;
    for(int i=0;i<strlen(BS);i++){
        int mod=BS[i]=='0'?10:(BS[i]-'0');
        t=(A[i]-'0')+(B[i]-'0')+f;
        r[i]=t%mod+'0';
        f=t/mod;
    }
    r[strlen(BS)]=f+'0';
    r[strlen(BS)+1]='\0';
    reverse(r);
    f=0;
    for(int i=0;i<strlen(r);i++){
        if(r[i]!='0'||f==1){
            printf("%c",r[i]);
            f=1;
        }
    }
    if(f==0)
        printf("0");
    printf("\n");
}
int main(){
    char BS[22],A[22],B[22];
    if(scanf("%s\n%s\n%s",BS,A,B)){}
    reverse(A);
    reverse(B);
    reverse(BS);
    int l;
    l=strlen(BS)-strlen(A);
    for(int i=0;i<l;i++)
        strcat(A,"0\0");
    l=strlen(BS)-strlen(B);
    for(int i=0;i<l;i++){
        strcat(B,"0\0");
    }
    Add(A,B,BS);
    return 0;
}

1075 链表元素分类

#include<stdio.h>
#include<stdlib.h>
typedef struct{
    int data;
    int next;
}node;
node L[100000];
int main(){
    int first,N,K;
    if(scanf("%d %d %d",&first,&N,&K)){}
    for(int i=0;i<N;i++){
        int Add;
        if(scanf("%d",&Add)){}
        if(scanf("%d %d",&L[Add].data,&L[Add].next)){}
    }
    int p=first;
    int f=0;
    while(p!=-1){
        if(L[p].data<0){
            if(f==0){
                printf("%05d %d ",p,L[p].data);
                f=1;
            }
            else{
                printf("%05d\n%05d %d ",p,p,L[p].data);
            }
        }
        p=L[p].next;
    }
    p=first;
    while(p!=-1){
        if(L[p].data>=0&&L[p].data<=K){
            if(f==0){
                printf("%05d %d ",p,L[p].data);
                f=1;
            }
            else
                printf("%05d\n%05d %d ",p,p,L[p].data);
        }
        p=L[p].next;
    }
    p=first;
    while(p!=-1){
        if(L[p].data>=0&&L[p].data>K){
            if(f==0){
                printf("%05d %d ",p,L[p].data);
                f=1;
            }
            else
                printf("%05d\n%05d %d ",p,p,L[p].data);
       
        }
        p=L[p].next;  
    }
    printf("-1\n");
    
    return 0;
}

1076 Wifi密码

#include<stdio.h>
#include<stdlib.h>
int main(){
    int  N;
    char s[4];
    if(scanf("%d",&N)){}
    for(int i=0;i<N;i++){
        for(int j=0;j<4;j++){
            if(scanf("%s",s)){}
            if(s[2]=='T')   printf("%d",s[0]-'A'+1);
        }
    }
    printf("\n");
    return 0;
}

1077 互评成绩计算

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,M;
    if(scanf("%d %d",&N,&M)){}
    for(int i=0;i<N;i++){
        int teacher,max=0,min=M,s,t=0;
        double sum=0;
        if(scanf("%d",&teacher)){}
        for(int j=0;j<N-1;j++){
            if(scanf("%d",&s)){}
            if(s>=0&&s<=M){
                if(s>max) max=s;
                if(s<min) min=s;
                t++;
                sum+=s;
            }
        }
        sum=((sum-max-min)/(t-2)+teacher)/2;
        printf("%d\n",(int)(sum+0.5));
    }
    return 0;
}

1078 字符串压缩与解压

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c1, c2, mode;
    mode = getchar();
    getchar();
    if (mode == 'C') {
        c1 = getchar();
        int count = 1;
        while ((c2 = getchar())) {
            if (c1 != c2) {
                if (count == 1)
                    printf("%c", c1);
                else
                    printf("%d%c", count, c1);
                count = 1;
            } else {
                count++;
            }
            c1 = c2;
            if (c1 == '\n')
                break;
        }

    } else {
        int count = 0;
        while ((c1 = getchar())) {
            if(c1=='\n')    break;
            if (c1 >= '0' && c1 <= '9') {
                count = count * 10 + c1 - '0';
            } else {
                if (count == 0)
                    printf("%c", c1);
                else {
                    for (int i = 0; i < count; i++)
                        printf("%c", c1);
                    count = 0;
                }
            }
        }
    }
    printf("\n");
    return 0;
}

1079 延迟的回文数

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void reverse(char *a){
    int i=0,j=strlen(a)-1;
    while (i<j)
    {
        char t=a[i];
        a[i]=a[j];
        a[j]=t;
        i++;
        j--;
    }
}
int isPalindromicNum(char* s)
{
    int i = 0, j = strlen(s)-1;
    while(i<j)
    {
        if(s[i]!=s[j]) return 0;
        i++;
        j--;
    }
    return 1;
}
void Add(char *a,char *b,char *c){
    int f=0,len=strlen(a);
    for(int i=0;i<len;i++){
        int t=a[i]-'0'+b[i]-'0'+f;
        c[i]=t%10+'0';
        f=t/10;
    }
    if(f!=0){
        c[len]='1';
        c[len+1]='\0';
    }
    else
        c[len]='\0';
    reverse(c);
}
int main()
{
    char *a,*b,*c;
    a=(char *)malloc(sizeof(char)*1024);
    b=(char *)malloc(sizeof(char)*1024);
    c=(char *)malloc(sizeof(char)*1024);
    if (scanf("%s", a)) { }  
    int count=0;
    while(isPalindromicNum(a)!=1)
    {
        count++;
        if(count>10)    break;
        strcpy(b,a);
        reverse(b);
        Add(a,b,c);
        printf("%s + %s = %s\n",a,b,c);
        strcpy(a,c);
    }
    if(count>10)        printf("Not found in 10 iterations.\n");
    else if(count==0)   printf("%s is a palindromic number.\n",a);
    else                printf("%s is a palindromic number.\n",c);
    free(a);
    free(b);
    free(c);
    return 0;
}

1080 MOOC期终成绩

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    char sno[21];
    int p;
    int mid;
    int final;
    int sum;
} Student;
int P, M, N, count = 0;
Student* L;
int getLoc(char* s, int start, int end)
{
    if (start > end)
        return -1;
    int mid = (start + end) / 2;
    int f = strcmp(L[mid].sno, s);
    if (f == 0)
        return mid;
    else if (f < 0)
        return getLoc(s, mid + 1, end);
    else
        return getLoc(s, start, mid - 1);
}
int cmp(const void* a, const void* b)
{
    if ((*(Student*)b).sum - (*(Student*)a).sum != 0)
        return (*(Student*)b).sum - (*(Student*)a).sum;
    else
        return strcmp((*(Student*)a).sno, (*(Student*)b).sno);
}
int main()
{
    scanf("%d %d %d", &P, &M, &N);
    L = (Student*)malloc(sizeof(Student) * P);
    for (int i = 0; i < P; i++) {
        Student t;
        scanf("%s %d", &t.sno, &t.p);
        if (t.p >= 200)
            L[count++] = t;
    }
    for (int i = 0; i < count; i++) {
        L[i].mid = -1;
        L[i].final = -1;
        L[i].sum = -1;
    }
    qsort(L, count, sizeof(Student), cmp);
    for (int i = 0; i < M; i++) {
        char t[21];
        int loc;
        scanf("%s", t);
        loc = getLoc(t, 0, count - 1);
        if (loc != -1)
            scanf("%d", &L[loc].mid);
        else
            scanf("%d", &loc);
    }
    for (int i = 0; i < N; i++) {
        char t[21];
        int loc;
        if (scanf("%s", t)) { }
        loc = getLoc(t, 0, count - 1);
        if (loc != -1)
            scanf("%d", &L[loc].final);
        else
            scanf("%d", &loc);
    }
    for (int i = 0; i < count; i++) {
        if (L[i].mid > L[i].final)
            L[i].sum = (int)(0.4 * L[i].mid + L[i].final * 0.6 + 0.5);
        else
            L[i].sum = L[i].final;
    }
    qsort(L, count, sizeof(Student), cmp);
    for (int i = 0; i < count; i++) {
        if (L[i].sum >= 60)
            printf("%s %d %d %d %d\n", L[i].sno, L[i].p, L[i].mid, L[i].final, L[i].sum);
    }
    free(L);
    return 0;
}

1081 检查密码

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    if(scanf("%d",&N)){getchar();}
    char pw[81];
    int z,s,p,k;
    for(int i=0;i<N;i++){
        z=0;s=0,p=0;
        //if(scanf("%s",pw));
        k=0;
        while(scanf("%c",&pw[k]))
        {
            if(pw[k]=='\n'){
                pw[k]='\0';
                break;
            }
            else if(pw[k]>='0'&&pw[k]<='9') s++;
            else if((pw[k]>='a'&&pw[k]<='z')||(pw[k]>='A'&&pw[k]<='Z'))  z++;
            else if(pw[k]=='.') p++;
            k++;
        }
        if(k<6)   printf("Your password is tai duan le.\n");
        else if(k>s+z+p)  printf("Your password is tai luan le.\n");
        else if(s==0)   printf("Your password needs shu zi.\n");
        else if(z==0)   printf("Your password needs zi mu.\n");
        else            printf("Your password is wan mei.\n");
    }
    return 0;
}

1082 射击比赛

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Dis(x,y) sqrt(x*x+y*y)
int main(){
    int N;
    int id,x,y;
    int Max,Min;
    float _Max,_Min;
    scanf("%d",&N);
    scanf("%d %d %d",&id,&x,&y);
    Max=id,Min=id;
    _Max=Dis(x,y),_Min=Dis(x,y);
    for(int i=1;i<N;i++){
        scanf("%d %d %d",&id,&x,&y);
        if(Dis(x,y)>_Max){
            _Max=Dis(x,y);
            Max=id;
        }
        if(Dis(x,y)<_Min){
            _Min=Dis(x,y);
            Min=id;
        }
    }
    printf("%04d %04d\n",Min,Max);
    return 0;
}

1083 是否存在相等的差

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N;
    int c[10001]={0};
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        int t;
        scanf("%d",&t);
        c[abs(t-i-1)]++;
    }
    for(int i=N;i>=0;i--){
        if(c[i]>1)
            printf("%d %d\n",i,c[i]);
    }
    return 0;
}

1084 外观数列

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int N;
    char s[81800];
    scanf("%s %d", s, &N);
    for (int k = 1; k < N; k++) {
        char t[81800], c;
        int count = 1, i = 1, j = 1;
        t[0] = s[0];
        while (1) {
            c = s[i];
            if (c == t[j - 1]) {
                count++;
            } else {
                t[j++] = count + '0';
                if (c == '\0') {
                    t[j] = '\0';
                    break;
                }
                count = 1;
                t[j++] = c;
            }
            i++;
        }
        strcpy(s, t);
    }
    printf("%s\n", s);
    return 0;
}

1085 PAT单位排行

#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef struct {
    string Name;
    int sum;
    int count;
} school;
bool cmp(school x,school y){
    return x.sum==y.sum?(y.count==x.count?x.Name<y.Name:x.count<y.count):(x.sum>y.sum);
}
int main()
{
    int N, score;
    vector<school> L;
    map<string, float> sum;
    map<string, int> count;
    string id, s;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        cin>>id>>score>>s;
        for (int j = 0; j < s.length(); j++)
            s[j] = tolower(s[j]);
        if (id[0] == 'A')
            sum[s] += score;
        else if (id[0] == 'B')
            sum[s] += score / 1.5;
        else
            sum[s] += 1.5 * score;
        count[s]++;
    }
    for (auto it = sum.begin(); it != sum.end(); it++) {
        L.push_back(school { it->first, (int)it->second, count[it->first] });
    }
    sort(L.begin(),L.end(),cmp);
    printf("%d\n",L.size());
    int n=0,b=-1;
    for (int i=0;i<L.size();i++){
        if(L[i].sum!=b)
            n=i+1;
        printf("%d %s %d %d\n",n,L[i].Name.c_str(),L[i].sum,L[i].count);
        b=L[i].sum;
    }
        return 0;
}

1086 就不告诉你

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int A,B;
    scanf("%d %d",&A,&B);
    int C=A*B;
    char s[10];
    sprintf(s,"%d",C);
    int i=0,j=strlen(s)-1;
    while(i<j){
        char t=s[i];
        s[i]=s[j];
        s[j]=t;
        i++;j--;
    }
    C=0;
    for(int i=0;i<strlen(s);i++)
        C=C*10+s[i]-'0';
    printf("%d\n",C);
    return 0;
}

1087 有多少不同的值

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
    int N,arr[15000]={0};
    scanf("%d",&N);
    for(int i=1;i<=N;i++){
        arr[i/2+i/3+i/5]++;
    }
    int sum=0;
    for(int i=0;i<15000;i++){
        if(arr[i]>0)
            sum++;
    }
    printf("%d\n",sum);
    return 0;
}

1088 三人行

#include<stdio.h>
#include<stdlib.h>
void cmp(int i,int j){
    if(i>j) printf(" Gai");
    else if(i==j)   printf(" Ping");
    else        printf(" Cong");
}
int main(){
    int M,X,Y;
    scanf("%d %d %d",&M,&X,&Y);
    int a,b;
    float c;
    int i;
    for(i=99;i>=10;i--){
        a=i;
        b=a%10*10+a/10;
        if(X*b==Y*abs(a-b))
            break;
    }
    if(i<10)    printf("No Solution\n");
    else{
        c=1.0*b/Y;
        printf("%d",a);
        cmp(M,a);
        cmp(M,b);
        if(M-c>0)         printf(" Gai");
        else if(M-c==0)   printf(" Ping");
        else              printf(" Cong");
    }
    return 0;
}

1089 狼人杀-简单版

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,v[101];
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%d",&v[i]);
    for(int i=1;i<=N;i++){
        for(int j=i+1;j<=N;j++){
            char t[101];
            int lie[2],count=0;
            for(int q=1;q<=N;q++)
                t[q]=1;
            t[i]=-1;t[j]=-1;
            for(int k=1;k<=N;k++){
                if(v[k]*t[abs(v[k])]<0){
                    if(count<2)
                        lie[count++]=k;
                    else{
                        count=-1;
                        break;
                    }
                }
            }
            if(count==2){
                if(t[lie[0]]+t[lie[1]]==0){
                    printf("%d %d\n",i,j);
                    return 0;
                }
            }
        }
    }
    printf("No Solution\n");
    return 0;
}

1090 危险品装箱

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
    int N, M;
    scanf("%d %d", &N, &M);
    map<int, vector<int>> Info;
    for (int i = 0; i < N; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        Info[a].push_back(b);
        Info[b].push_back(a);
    }
    for (int i = 0; i < M; i++) {
        int K;
        scanf("%d", &K);
        vector<int> v(K);
        int e[100000] = { 0 };
        for (int j = 0; j < K; j++) {
            scanf("%d", &v[j]);
            e[v[j]] = 1;
        }
        int f = 0;
        for (int i = 0; i < v.size(); i++) {
            for (int j = 0; j < Info[v[i]].size(); j++) {

                if (e[Info[v[i]][j]] == 1) {
                    f = 1;
                    break;
                }
            }
            if (f == 1)
                break;
        }
        if (f == 1)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}

1091 N-自守数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
    int M;
    scanf("%d",&M);
    for(int i=0;i<M;i++){
        char K[5];
        int k=0,f=0;
        scanf("%s",K);
        int len=strlen(K);
        for(int j=0;j<len;j++)
            k=k*10+K[j]-'0';
        for(int n=1;n<10;n++){
            if(((n*k*k-k)%(int)pow(10.0,len))==0){
                printf("%d %d\n",n,n*k*k);
                f=1;
                break;
            }
        }
        if(f==0)    printf("No\n");
    }
    return 0;
}

1092 最好吃的月饼

#include<stdio.h>
#include<stdlib.h>
int main(){
    int N,M,d[1001]={0};
    scanf("%d %d",&N,&M);
    for(int i=0;i<M;i++){
        for(int j=1;j<=N;j++){
            int t;
            scanf("%d",&t);
            d[j]+=t;
        }
    }
    int m=0;
    for(int i=1;i<=N;i++)
        if(d[i]>m)
            m=d[i];
    printf("%d\n",m);
    int f=0;
    for(int i=1;i<=N;i++)
        if(d[i]==m){
            if(f==0)
                f++;
            else
                printf(" ");
            printf("%d",i);
        }
    printf("\n");
    return 0;
}

1093 字符串A+B

#include<stdio.h>
#include<stdlib.h>
int main(){
    int a[128]={0};
    char s[128];
    int i=0;
    while(1){
        char c=getchar();
        a[c]++;
        if(c=='\n'&&a[c]==2)
            break;
        if(a[c]==1)
            s[i++]=c;
    }
    s[i]='\0';
    i=0;
    while(s[i]!='\0'){
        if(s[i]!='\n')
            printf("%c",s[i]);
        i++;
    }
    printf("\n");    
    return 0;
}

1094 谷歌的招聘

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isPrime(int x){
    if(x<2) return 0;
    for(int i=2;i<=sqrt(x);i++)
        if(x%i==0)
            return 0;
    return 1;
}
int main(){
    char s[1001];
    int L,K;
    scanf("%d %d",&L,&K);
    scanf("%s",s);
    for(int i=0;i<=L-K;i++){
        int sum=0;
        for(int j=i;j<i+K;j++)
            sum=sum*10+s[j]-'0';    
        if(isPrime(sum)==1){
            for(int j=i;j<i+K;j++)
                printf("%c",s[j]); 
            return 0;
        }
    }
    printf("404");
    return 0;
}

1095 解码PAT准考证

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
    char zkzh[14];
    int score;
}Student;
typedef struct{
    int kc;
    int count;
}Kc;
int toInt(char *s){
    int sum=0;
    for(int i=0;i<strlen(s);i++)
        sum=sum*10+s[i]-'0';
    return sum;
}
void substr(char *s,char *sub,int start,int len){
    for(int i=0;i<len;i++)
        sub[i]=s[i+start];
    sub[len]='\0';
}
int cmp(const void *a,const void *b){
    Student *t1=(Student *)a;
    Student *t2=(Student *)b;
    if(t1->zkzh[0]!=t2->zkzh[0])
        return t1->zkzh[0]-t2->zkzh[0];
    else if(t2->score-t1->score!=0)
        return t2->score-t1->score;
    else
        return strcmp(t1->zkzh,t2->zkzh);
}
int cmp2(const void *a,const void *b){
    Kc *t1=(Student *)a;
    Kc *t2=(Student *)b;
    if(t1->count!=t2->count)
        return t2->count-t1->count;
    else
        return t1->kc-t2->kc;
}
int kc_count[1000],kc_sum[1000];
Kc rq_count[1000];
int main(){
    int N,M;
    scanf("%d %d",&N,&M);
    getchar();
    Student *L=(Student *)malloc(sizeof(Student)*N);
    for(int i=0;i<N;i++){
        char t[14],sub1[7],sub2[7];
        int score;
        scanf("%s %d",t,&score);
        strcpy(L[i].zkzh,t);
        L[i].score=score;

        substr(t,sub1,1,3);
        substr(t,sub2,4,6);
        kc_count[toInt(sub1)]++;
        kc_sum[toInt(sub1)]+=score;
    }
    qsort(L,N,sizeof(Student),cmp);
    /*for(int i=0;i<N;i++){
        printf("%s %d\n",L[i].zkzh,L[i].score);
    }*/
    for(int i=0;i<M;i++){
        int a;
        scanf("%d",&a);
        if(a==1){
            int f=0;
            getchar();
            char t=getchar();
            printf("Case %d: %d %c\n",i+1,a,t);
            for(int j=0;j<N;j++){
                if(L[j].zkzh[0]==t){
                    f=1;
                    printf("%s %d\n",L[j].zkzh,L[j].score);
                }
            }
            if(f==0)    printf("NA\n");
        }
        else if(a==2){
            int kc;
            scanf("%d",&kc);
            printf("Case %d: %d %d\n",i+1,a,kc);
            if(kc_count[kc]!=0)
                printf("%d %d\n",kc_count[kc],kc_sum[kc]);
            else
                printf("NA\n");
        }
        else{
            for(int j=0;j<1000;j++){
                rq_count[j].count=0;
            }
            char date[7],sub[7];
            scanf("%s",date);
            for(int j=0;j<N;j++){
                substr(L[j].zkzh,sub,4,6);
                if(strcmp(sub,date)==0){
                    substr(L[j].zkzh,sub,1,3);
                    rq_count[toInt(sub)].kc=toInt(sub);
                    rq_count[toInt(sub)].count++;
                }
            }
            qsort(rq_count,1000,sizeof(Kc),cmp2);
            printf("Case %d: %d %s\n",i+1,a,date);
            for(int j=0;j<1000;j++){
                if(rq_count[j].count>0)
                    printf("%03d %d\n",rq_count[j].kc,rq_count[j].count);
                else{
                    if(j==0)    printf("NA\n");
                    break;
                }
            }
        }

    }
    free(L);
    return 0;
}

2021.11.12

  • 17
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值