pat乙级代码

寒假在家练pat的题目,已完结

目录

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

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

B 1002 写出这个数

#include <cstdio>
#include<cstring>
int main()
{
    char str[120];
    int a[10];
    char b[10][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    scanf("%s",str);
    int len=strlen(str);
    int temp,sum=0;
    for(int i=0;i<len;i++)
    {
        temp=str[i]-'0';
        sum+=temp;
    }
    int k=0;
   while(sum!=0)
   {
       a[k++]=sum%10;
       sum=sum/10;
   }
   for(int j=k-1;j>0;j--)
   {
       printf("%s ",b[a[j]]);
   }
 printf("%s",b[a[0]]);
    return 0;
}

B 1003 我要通过

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char str[12][110]={0};
int left;
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%s",&str[i]);
    }
    int k,t,j,p,len;
    int acount=0;
    for(j=0;j<n;j++)
    {
        len=strlen(str[j]);
        acount=0;
         for(i=0;i<len;i++)
    {
      if(str[j][i]=='P')
      {
          p=i;
      }
      else if(str[j][i]=='T')
      {
          t=i;
      }
      else if(str[j][i]=='A')
      {
          acount++;
      }

    }

    if((len-t-1)-p*(t-p-2)==p&&len==acount+2&&acount!=0)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    }
    return 0;
}

B 1004 成绩排名

#include<cstdio>
int main()
{
    struct student
    {
       char name[11];//字符数组最后一位需要预留给'\0',大小要比给的10大一位,所以取11位。为保险起见,以后数组值可以取大点。
       char id[11];
       int grade;
    }st[100];
    int n,maxs=0,mins=100;
    int p,q;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s%s%d",st[i].name,st[i].id,&st[i].grade);
        if(st[i].grade>maxs)
        {
           maxs=st[i].grade;
            p=i;
        }
            
        if(st[i].grade<mins)
        {
            mins=st[i].grade;
            q=i;
        }
           

    }
        printf("%s %s\n",st[p].name,st[p].id);
        printf("%s %s\n",st[q].name,st[q].id);
    return 0;
}

B 1005继续(3n+1)猜想

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,i,t,count=0,s[10010];
    int step=0;
    scanf("%d",&n);
    bool hash[10010]={false};
    for(i=0;i<n;i++)
    {
        scanf("%d",&s[i]);
        t=s[i];
    while(t!=1)
    {
        if(t%2==0)
        {
            t=t/2;
        }
        else
        {
            t=(3*t+1)/2;
        }
        hash[t]=true;
    }
    }
    sort(s,s+n,cmp);
     for(i=0;i<n;i++)
    {
        if(hash[s[i]]==false)
         count++;
    }
    for(i=0;i<n;i++)
    {
        if(hash[s[i]]==false)
        {
             printf("%d",s[i]);
              count--;//这里要求最后一个不要有空格输出,所以要有一个count计数,且count--的位置不能放if判断后
        if(count>0)
                printf(" ");
       
        }
    }
    return 0;
}

B 1006 换个格式输出整数

#include <cstdio>
int main()
{
    int i,n;
    scanf("%d",&n);
    for(i=0;i<n/100;i++)
    {
        printf("B");
    }
    for(i=0;i<n%100/10;i++)
    {
        printf("S");
    }
    for(i=1;i<=n%10;i++)
    {
        printf("%d",i);
    }
}

B 1007 素数对猜想

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
using namespace std;

bool isprime(int n)
{
    if(n<=1)
        return false;
    int sqr=(int)sqrt(1.0*n);
    for(int i=2;i<=sqr;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}

int main()
{
    int n,count=0;
    scanf("%d",&n);
    for(int i=3;i+2<=n;i+=2)
    {
        if(isprime(i)==true&&isprime(i+2)==true)
        {
            count++;
        }
    }
    printf("%d",count);

    return 0;
}

B 1008 数组元素循环右移问题

#include<cstdio>
int main()
{
    int n,m;
    int a[100];
    scanf("%d%d",&n,&m);//这一步千万别忘,因为m不一定小于n
    m=m%n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int j=n-m;j<n;j++)
    {
        printf("%d ",a[j]);
    }
    for(int j=0;j<n-m-1;j++)
    {
        printf("%d ",a[j]);
    }
     printf("%d",a[n-m-1]);
    
    return 0;
}

B 1009 说反话

注意这题代码要用c,c++会编译错误,因为gets函数只能用c,c++可能就要用string容器了

#include <stdio.h>
#include<string.h>
int main()
{
    char str[90];
    gets(str);//这里一开始用scanf("%s",str)怎么都不对,不能这么用,不然只能接受第一个单词
    int len=strlen(str);
    char a[90][100];
    int k=0,j=0;
    for(int i=0;i<len;i++)
    {
        if(str[i]!=' ')
        {
            a[j][k++]=str[i];
        }
        else
        {
            a[j][k]='\0';
             j++;
             k=0;
        }
       
    }
    for(int t=j;t>0;t--)
    {
        printf("%s ",a[t]);
    }
     printf("%s",a[0]);
    return 0;
}

B 1010 一元多项式求导

#include<cstdio>
int main()
{
    int a[20]={0};
    int count=0;
    int temp,k;
    while(~scanf("%d %d",&temp,&k))
    {
        a[k]=temp;
        if(temp==0||k==0)
         continue;
        if(count==0)
             printf("%d %d",a[k]*k,k-1);
        else
             printf(" %d %d",a[k]*k,k-1);
        count++;
    }
    if(count==0)
        printf("0 0");
    return 0;
}

B 1011 A+B和C

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

B 1012 数字分类

#include<cstdio>
using namespace std;

int main()
{
    int n,temp,p;
    int a,count=0;
    int A[5]={0};
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a);
        switch(a%5)
        {
        case 0:
            if(a%2==0)
            A[0]=A[0]+a;
            break;
        case 1:
            if(count%2==0)//这里同样需要一个count记录个数,不能直接用i来判断正负
                A[1]=A[1]+a;
            else
                A[1]=A[1]-a;
            count++;
              break;
        case 2:
            A[2]++;
            break;
        case 3:
            A[3]=A[3]+a;
            p++;
            break;
        case 4:
            if(a>A[4])
                A[4]=a;
            break;
        }

    }
     if(A[0]==0)
        printf("N ");
    else
        printf("%d ",A[0]);
     if(A[1]==0&&count==0)
        printf("N ");
    else
        printf("%d ",A[1]);
     if(A[2]==0)
        printf("N ");
     else
        printf("%d ",A[2]);
     if(A[3]==0)
        printf("N ");
    else
        printf("%.1f ",(double)A[3]/p);
    if(A[4]==0)
        printf("N");
    else
        printf("%d",A[4]);
}

B 1013 数素数

#include <cstring>
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=1000001;
int prime[maxn],num=0;
bool p[maxn]={0};
void Find_Prime(int n){
 for(int i=2;i<maxn;i++){
    if(p[i]==false){
        prime[num++]=i;
             if(num>=n) break;
        for(int j=i+i;j<maxn;j+=i){
            p[j]=true;
        }
    }
}
}
int main()
{
    int m,n,count=0;
    scanf("%d%d",&m,&n);

   Find_Prime(n);
   for(int i=m;i<=n;i++){
    printf("%d",prime[i-1]);
    count++;
    if(count%10!=0&&i<n)
    printf(" ");
        else printf("\n");
    }
        return 0;
}

B 1014 福尔摩斯的约会

#include <stdio.h>
#include<string.h>
int main()
{
    char str1[60],str2[60],str3[60],str4[60];
    scanf("%s",str1);
    scanf("%s",str2);
    scanf("%s",str3);
    scanf("%s",str4);
    int len1=strlen(str1);
    int len2=strlen(str2);
    int len3=strlen(str3);
    int len4=strlen(str4);
    if(len1>len2)
        len1=len2;
    int i;
    char day[7][5]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
    for(i=0;i<len1;i++)
    {
         if(str1[i]==str2[i]&&str1[i]>='A'&&str1[i]<='G')
         {
            printf("%s ",day[str1[i]-'A']);
          break;
         }

    }
    for(i++;i<len1;i++)
    {
        if(str1[i]==str2[i]&&str1[i]>='A'&&str1[i]<='N')
        {
            printf("%02d:",str1[i]-'A'+10);
            break;
        }

        else if(str1[i]==str2[i]&&str1[i]>='0'&&str1[i]<='9')
        {
             printf("%02d:",str1[i]-'0');
             break;
        }

    }
    if(len3>len4)
        len3=len4;
    int j;
    for(j=0;j<len3;j++)
    {
        if(str3[j]==str4[j])
        {
            if((str3[j]>='A'&&str4[j]<='Z')||(str3[j]>='a'&&str3[j]<='z'))
         {
            printf("%02d",j);
            break;
          }
        }

       
    }
    return 0;
}

B 1015 德才论

#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student
  {
      char id[10];
      int de,cai,sum;
      int flag;
  }stu[100010];
  bool cmp(student a,student b)
  {
      if(a.flag!=b.flag)
        return a.flag<b.flag;
      else if(a.sum!=b.sum)
        return a.sum>b.sum;
      else if(a.de!=b.de)
        return a.de>b.de;
      else
        return strcmp(a.id,b.id)<0;
  }
int main()
{
  int n,l,h,i;
  scanf("%d %d %d",&n,&l,&h);
  int pass=n;
  for(i=0;i<n;i++)
  {
      scanf("%s",stu[i].id);
      scanf("%d%d",&stu[i].de,&stu[i].cai);
      stu[i].sum=stu[i].de+stu[i].cai;
      if(stu[i].de<l||stu[i].cai<l)
      {
          stu[i].flag=5;
          pass--;
      }
      else if(stu[i].de>=h&&stu[i].cai>=h)
      {
          stu[i].flag=1;
      }
      else if(stu[i].de>=h&&stu[i].cai<h)
      {
         stu[i].flag=2;
      }
      else if(stu[i].de>=stu[i].cai)
      {
          stu[i].flag=3;
      }
      else
      {
          stu[i].flag=4;
      }
  }
  sort(stu,stu+n,cmp);
  printf("%d\n",pass);
  for(i=0;i<pass;i++)
  {
      printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
  }
    return 0;
}


B 1016 部分A+B(15)

#include<cstdio>
int main()
{
    long long a,b;
    int m,n;
    int i=0,j=0;
    scanf("%lld%d%lld%d",&a,&m,&b,&n);
    while(a!=0)
    {
        if(a%10==m)
            i=i*10+m;
        a=a/10;
    }
     while(b!=0)
    {
        if(b%10==n)
            j=j*10+n;
        b=b/10;
    }
    printf("%d",i+j);
    return 0;
}

A 1059 Prime Factors

这道题和数素数那题类似,都要得出素数表。可是输入这个数却测试结果失败的,求会的小伙伴,评论留言救下
在这里插入图片描述

#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=1000010;
int prime[maxn],num=0;
bool p[maxn]={0};
void Find_Prime(){
 for(int i=2;i<maxn;i++){
    if(p[i]==false){
        prime[num++]=i;
        for(int j=i+i;j<maxn;j+=i){
            p[j]=true;
        }
    }
}
}
struct factor{
 int x;
 int cnt;
 }fac[10];
int main()
{
    Find_Prime();
    int n,count=0;
    scanf("%d",&n);
    if(n==1) printf("1=1");
    else{
            printf("%d=",n);
            int sqr=(int)sqrt(1.0*n);

   for(int i=0;i<num&&prime[i]<=sqr;i++){
    if(n%prime[i]==0){
        fac[count].x=prime[i];
        fac[count].cnt=0;
        while(n%prime[i]==0){
            fac[count].cnt++;
            n/=prime[i];
        }
        count++;
    }
    if(n==1) break;
   }
   if(n!=1){
    fac[count].x=n;
    fac[count].cnt=1;
   }
   for(int i=0;i<count;i++){
    if(i>0) printf("*");
    printf("%d",fac[i].x);
    if(fac[i].cnt>1){
           printf("^%d",fac[i].cnt);
    }
   }
    }
        return 0;

}

B 1017 A除以B

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
using namespace std;

struct bign
{
    int d[1010];
    int len;
    bign()
    {
        memset(d,0,sizeof(d));
        len=0;
    }
};

bign change(char str[])
{
    bign a;
    a.len=strlen(str);
    for(int i=0;i<a.len;i++)
    {
        a.d[i]=str[a.len-i-1]-'0';
    }
    return a;
}

bign divide(bign a,int b,int& r)
{
    bign c;
    c.len=a.len;
    for(int i=a.len-1;i>=0;i--)
    {
        r=r*10+a.d[i];
        if(r<b)
            c.d[i]=0;
        else
        {
            c.d[i]=r/b;
            r=r%b;
        }
    }
    while(c.len-1>=1&&c.d[c.len-1]==0)
    {
        c.len--;
    }
    return c;
}

void print(bign a)
{
    for(int i=a.len-1;i>=0;i--)
        printf("%d",a.d[i]);
}

int main()
{
    char str1[1010],str2[1010];
    int b,r=0;
    scanf("%s%d",str1,&b);
    bign a=change(str1);
    print(divide(a,b,r));
    printf(" %d",r);
    return 0;
}

B 1018 锤子剪刀布

#include<cstdio>
int change(char c)
{
    if(c=='B')
        return 0;
    if(c=='C')
        return 1;
    if(c=='J')
        return 2;
}

void tochange(int m)
{
    if(m==0)
        printf("B");
    if(m==1)
        printf("C");
    if(m==2)
        printf("J");
}
int main()
{
    int n,q,p;
    int v1=0,f1=0,p1=0,v2=0,f2=0,p2=0;
    char a,b;
    int maxa=0,maxb=0,m1=0,m2=0;
    int A[3]={0};
    int B[3]={0};
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
         getchar();//吸收回车符,否则输入数据后会闪退
        scanf("%c %c",&a,&b);
        p=change(a);
        q=change(b);
        if((p+1)%3==q)
        {
            v1++;
            f2++;
            A[p]++;
        }
          if((q+1)%3==p)
        {
            v2++;
            f1++;
            B[q]++;
        }
          if(p==q)
        {
            p1++;
            p2++;
        }
    }
    printf("%d %d %d\n",v1,p1,f1);
    printf("%d %d %d\n",v2,p2,f2);
    for(int j=0;j<3;j++)
    {
        if(A[j]>maxa)
        {
            m1=j;
            maxa=A[j];
        }

        if(B[j]>maxb)
        {
             m2=j;
             maxb=B[j];
        }
        
    }
    tochange(m1);
        printf(" ");
        tochange(m2);

}

B 1019 数字黑洞

这题需要注意:算式中数字和运算符号之间都有空格
不然,容易报格式错误哦!

#include <cstring>
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
void to_array(int n,int num[]){
for(int i=0;i<4;i++){
    num[i]=n%10;
    n/=10;
}
}
int to_number(int num[]){
int sum=0;
for(int i=0;i<4;i++){
    sum=sum*10+num[i];
}
return sum;
}
int main()
{
    int n,MIN,MAX;
    scanf("%d",&n);
    int num[5];
    while(1){
        to_array(n,num);
        sort(num,num+4);
        MIN=to_number(num);
        sort(num,num+4,cmp);
        MAX=to_number(num);
        n=MAX-MIN;
        printf("%04d - %04d = %04d\n",MAX,MIN,n);
        if(n==0||n==6174)
            break;
    }
        return 0;
}


B 1020 月饼

这一篇代码在codebloks下运行没有问题,可是pat官网提交就说结果错误,希望有热心小伙伴指出错误之处。

#include<stdio.h>
#include<algorithm>
using namespace std;
struct mooncake{
double store;
double sell;
double price;
}cake[1010];
bool cmp(mooncake a,mooncake b){
return a.price>b.price;
}
int main()
{
    int  i,n;
    double D;
    scanf("%d%lf",&n,&D);
    for(i=0;i<n;i++)
        scanf("lf",&cake[i].store);

    for(i=0;i<n;i++)
        {scanf("lf",&cake[i].sell);
           cake[i].price=cake[i].sell/cake[i].store;
            }
sort(cake,cake+n,cmp);
double ans=0;
     for(i=0;i<n;i++){
    if(cake[i].store<=D)
           {

            D-=cake[i].store;
            ans+=cake[i].sell;
     }else{
         ans+=cake[i].price*D;
         break;
     }
       printf("%.2f\n ",ans);
       return 0;
    }
}

B 1021 个位数统计

#include <cstdio>
#include<cstring>
int main()
{
    char str[1000];
   while(~scanf("%s",str))
    {
         int temp;
    int len=strlen(str);
    int a[10]={0};
    for(int i=0;i<len;i++)
    {
        temp=str[i]-'0';
        a[temp]++;
    }
    for(int j=0;j<10;j++)
    {
        if(a[j]>0)
        printf("%d:%d\n",j,a[j]);
    }
    }

    return 0;
}

B 1022 D进制的A+B

#include <cstdio>
int main()
{
    int n,a,b,y,num=0;
    int z[40];
    scanf("%d%d%d",&a,&b,&n);
    y=a+b;
    do{
        z[num++]=y%n;
        y=y/n;
    }while(y!=0);
    for(int i=num-1;i>=0;i--)
    {
        printf("%d",z[i]);
    }
	return 0;
}

B 1023 组个最小数

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int i,s[10]={0};
    for(i=0;i<10;i++)
    {
        scanf("%d",&s[i]);
    }
    for(i=1;i<10;i++)
    {
        if(s[i]!=0)
        {
              printf("%d",i);
              s[i]--;
              break;
        }
    }
    for(i=0;i<10;i++)
    {
        while(s[i]!=0)
        {
            printf("%d",i);
            s[i]--;
        }
    }
    return 0;
}

B 1024 科学计数法

改了一天了,还是有一个案例没有通过

#include <stdio.h>
#include<string.h>
int main()
{
    char str[10010],c[10010];
    scanf("%s",str);
    int len=strlen(str);
    int i,t=0;
    if(str[0]=='-')
        printf("-");
    for(i=1;i<len;i++)
    {
        if(str[i]=='.')
        {
            continue;
        }
        else
        {
            if(str[i]!='E')
        {
              c[t++]=str[i];
        }
        else
        {
             break;
        }

        }
    }
    int j,ans=0;
    for(j=i+2;j<len;j++)
    {
        ans=ans*10+(str[j]-'0');
    }
    if(ans==0)
    {
          printf("%s",str);
    }
    else
    {
     if(str[i+1]=='-')
    {
        printf("0.");
        for(j=0;j<ans-1;j++)
        {
            printf("0");
        }
        printf("%s",c);
    }
    else
    {
        if(ans<i-3)
        {
            for(j=0;j<ans+1;j++)
        {
            printf("%c",c[j]);
        }
         printf(".");
         for(j;j<t;j++)
             printf("%c",c[j]);
        }
        else
        {
            printf("%s",c);
            for(j=0;j<ans-i+3;j++)
            {
                printf("0");
            }
        }
    }
    }
    return 0;
}

B 1025 翻转链表

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct Node
{
    int address,data,next;
    int order;
    Node()
    {
        order=maxn;
    }
}node[maxn];

bool cmp(Node a, Node b)
{
	return a.order<b.order;
}

int main()
{
    int begin,n,k,address;
    scanf("%d%d%d",&begin,&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    int p=begin,count=0;
    while(p!=-1)
    {
        node[p].order=count++;
        p=node[p].next;
    }
    sort(node,node+maxn,cmp);
    n=count;
    for(int i=0;i<n/k;i++)
    {
        for(int j=(i+1)*k-1;j>i*k;j--)
        {
            printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);
        }
        printf("%05d %d ",node[i*k].address,node[i*k].data);
        if(i<n/k-1)
        {
            printf("%05d\n",node[(i+2)*k-1].address);
        }
        else
        {
            if(n%k==0)
            {
                printf("-1\n");
            }
            else
            {
                printf("%05d\n",node[(i+1)*k].address);
                for(int i=n/k*k;i<n;i++)
                {
                     printf("%05d %d ",node[i].address,node[i].data);
                    if(i<n-1)
                    {
                       printf("%05d\n",node[i+1].address);
                    }
                    else
                    {
                        printf("-1\n");
                    }
                }

            }
        }
    }

    return 0;
}

B 1026 程序运行时间?

有一个测试用例显示答案错误,比较笨和暴力的做法,但是不知道错在哪?求留言指出

#include<cstdio>
int main()
{
    int m,n,clock;
    int second=0,minute=0,hour=0;
    scanf("%d%d",&m,&n);
    clock=(n-m)/100;
    if((m-n)%100>=50)
        clock++;
    second=clock%60;
    clock=clock/60;
    minute=clock%60;  
    clock=clock/60;
    hour=clock;
    printf("%02d:%02d:%02d",hour,minute,second);
    return 0;
}

B 1027 打印沙漏

#include <cstdio>
int main()
{
    int n,k=0;
    char c;
    int ans=0,count=0;
    scanf("%d %c",&n,&c);
    for(int i=3;ans<=(n-1)/2;i=i+2)
    {
        ans+=i;
        k=i;
        count++;
    }
    int j,p,t;
    for(j=k-2;j>=1;j=j-2)
    {
        for(p=0;p<((k-2)-j)/2;p++)
        printf(" ");
        for(t=0;t<j;t++)
        printf("%c",c);
        printf("\n");
    }
    for(int j=3;j<=k-2;j=j+2)
    {
        for(p=((k-2)-j)/2;p>0;p--)
            printf(" ");
        for(t=j;t>0;t--)
            printf("%c",c);
        printf("\n");
    }

    printf("%d",n-1-(ans-k)*2);
	return 0;
}

B 1028 人口普查

改了一天了,实在不知道哪不对

#include<cstdio>
#include<cstring>
int main()
{
    struct student
    {
       char name[10];
       int year;
       int months;
       int day;
    }young,old,man;
    young.year=2014;
    man.year=old.year=1814;
    man.months=young.months=old.months=9;
    man.day=young.day=5;
    old.day=7;
    int n,count=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s %d/%d/%d",man.name,&man.year,&man.months,&man.day);
        if(man.year<1814||man.year==1814&&man.months<9||man.year==1814&&man.months==9&&man.day<6||man.year>2014||man.year==2014&&man.months>9||man.year==2014&&man.months==9&&man.day>6)
			{
				continue;
			}
			else
            {
                 if(man.year>old.year||man.year==old.year&&man.months>old.months||man.year==old.year&&man.months==old.months&&man.day>old.day)
               {
                   old=man;
               }
               if(man.year<young.year||man.year==young.year&&man.months<young.months||man.year==young.year&&man.months==young.months&&man.day<young.day)
               {
                   young=man;
               }
                    count++;
            }



           }
        if(count==0)
        {
            printf("0\n");
        }
        else
        {
            printf("%d %s %s\n",count,old.name,young.name);
        }
    return 0;
}

B 1029 旧键盘

这题的出发点就是,坏了的键盘不会输出相应结果

#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    char str1[100],str2[100];
    scanf("%s",str1);
    scanf("%s",str2);
    int len1=strlen(str1);
    int len2=strlen(str2);
    bool hashs[128]={false};
    int i,j;
    for(i=0;i<len1;i++)
    {
        if(str1[i]>='a'&&str1[i]<='z')
               str1[i]-=32;
        for(j=0;j<len2;j++)
        {
            if(str2[j]>='a'&&str2[j]<='z')
               str2[j]-=32;
            if(str1[i]==str2[j])
            {
                 hashs[str1[i]]=true;
                 break;
            }

        }
    }
    for(i=0;i<len1;i++)
    {
        if(hashs[str1[i]]==false)
        {
             printf("%c",str1[i]);
             hashs[str1[i]]=true;
        }

    }
    return 0;
}


B 1030 完美数列

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int i,n,p,s[100010]={0};
    scanf("%d %d",&n,&p);
    for(i=0;i<n;i++)
    {
        scanf("%d",&s[i]);
    }
    sort(s,s+n);
    int k;
    int maxs=0;
    for(i=0;i<n;i++)
    {
        int k=upper_bound(s+i+1,s+n,(long long)s[i]*p)-s;
        maxs=max(maxs,k-i);
    }
    printf("%d\n",maxs);
    return 0;
}

B 1031 查验身份证

有一个测试用例怎么都过不去,心态都崩了

#include <cstdio>
#include<cstring>
int main()
{
    int n;
    char str[20];
    int w[20]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char m[12]={'1','0','X','9','8','7','6','5','4','3','2'};
    scanf("%d",&n);
    bool flag=true;
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        int ans=0;
        for(int j=0;j<17;j++)
        {
            if(str[j]>='0'&&str[j]<='9')
            {
                ans=ans+(str[j]-'0')*w[j];
            }
            else
            {
                 flag=false;
            }

        }

        if(m[ans%11]!=str[17]||flag==false)
        {
             printf("%s\n",str);
               flag=false;
        }
        else
        {
            flag=true;
        }
    }
    if(flag==true)
        printf("All passed");

    return 0;
}

B 1032 挖掘机技术哪家强

#include <cstdio>
#include <cstring>
int main()
{
	int n,t,temp,m=0;
	int a[100000]={0};
	scanf("%d",&n);
	for(int i=0;i<n;i++)
    {
      scanf("%d%d",&t,&temp);
      a[t]+=temp;
      if(t>m)
      m=t;
    }
    int k=0,maxn=0;
    for(int i=0;i<=m;i++)
    {
        if(a[i]>maxn)
        {
            maxn=a[i];
            k=i;
        }

    }
    printf("%d %d",k,maxn);
	return 0;
}

B 1033 旧键盘打字

这一题真的巨阴险,首先输入时候不能用scanf()函数,题目说第二个字符串不能为空串,却并没有说第一个不能为空串,所以scanf不能处理空串问题,如果scanf()读入的第一个就是’\n’,那么它会跳过这个符号往后读一个,如果后面还是’\n’,它还会继续跳过,直到遇见第一个非空格也非换行符的字符才开始读入,而getline()遇到’\n’会直接终止,读入为空则为空串。
然而,改了好久,最后一个测试样例还是不通过

#include <stdio.h>
#include<string.h>
#include<string>
#include <iostream>
using namespace std;
int main()
{
    char str1[100],str2[100];
    cin.getline(str1,100);
    cin.getline(str2,100);
    int len1=strlen(str1);
    int len2=strlen(str2);
    bool hashs[128]={true};
    memset(hashs,true,sizeof(hashs));
    int i,j;
    for(i=0;i<len1;i++)
    {
        if(str1[i]>='A'&&str1[i]<='Z')
               str1[i]+=32;
        hashs[str1[i]]=false;
    }
    for(j=0;j<len2;j++)
    {
        if(str2[j]>='A'&&str2[j]<='Z')
        {
            int low=str2[j]+32;
            if(hashs['+']==true&&hashs[low]==true)
            {
                printf("%c",str2[j]);
            }
        }
         else if(hashs[str2[j]]==true)
         {
                printf("%c",str2[j]);
         }
         
    }
    printf("\n");
    return 0;
}



B 1034 有理数四则运算

这题一定要注意格式,运算符前后都有空格的

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct fraction
{
    long long up,down;
}a,b;

long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
fraction reduction(fraction result)
{
    if(result.down<0)
    {
        result.up=-result.up;
        result.down=-result.down;
    }
    if(result.up==0)
    {
        result.down=1;
    }
    else
    {
        int d=gcd(abs(result.up),abs(result.down));
        result.up/=d;
        result.down/=d;
    }
    return result;
}

fraction add(fraction f1,fraction f2)
{
    fraction result;
    result.up=f1.up*f2.down+f2.up*f1.down;
    result.down=f1.down*f2.down;
    return reduction(result);
}

fraction minu(fraction f1,fraction f2)
{
    fraction result;
    result.up=f1.up*f2.down-f2.up*f1.down;
    result.down=f1.down*f2.down;
    return reduction(result);
}

fraction multi(fraction f1,fraction f2)
{
    fraction result;
    result.up=f1.up*f2.up;
    result.down=f1.down*f2.down;
    return reduction(result);
}
fraction divide(fraction f1,fraction f2)
{
    fraction result;
    result.up=f1.up*f2.down;
    result.down=f1.down*f2.up;
    return reduction(result);
}

void showresult(fraction r)
{
    r=reduction(r);
    if(r.up<0)
        printf("(");
    if(r.down==1)
        printf("%lld",r.up);
    else if(abs(r.up)>r.down)
    {
        printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
    }
    else
    {
        printf("%lld/%lld",r.up,r.down);
    }
    if(r.up<0)
        printf(")");
}
int main()
{
    scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down);
    showresult(a);
    printf(" + ");
    showresult(b);
    printf(" = ");
    showresult(add(a,b));
    printf("\n");

    showresult(a);
    printf(" - ");
    showresult(b);
    printf(" = ");
    showresult(minu(a,b));
    printf("\n");

    showresult(a);
    printf(" * ");
    showresult(b);
    printf(" = ");
    showresult(multi(a,b));
    printf("\n");

    showresult(a);
    printf(" / ");
    showresult(b);
    printf(" = ");
    if(b.up==0)
        printf("Inf");
    else showresult(divide(a,b));

    return 0;
}

B 1035 插入与归并

#include<stdio.h>
#include<algorithm>
using namespace std;
int origin[110],origin2[110],change[110];
int n;
bool issame(int a[],int b[])
{
    for(int i=0;i<n;i++)
    {
        if(a[i]!=b[i])
            return false;
    }
    return true;
}

bool showarray(int a[])
{
    for(int i=0;i<n-1;i++)
    {
        printf("%d ",a[i]);
    }
    printf("%d",a[n-1]);
}

bool insertsort()
{
    bool flag=false;
    for(int i=1;i<n;i++)
    {
        if(i!=1&&issame(origin2,change))
        {
            flag=true;
        }
        int temp=origin2[i],j=i;
        while(j>0&&origin2[j-1]>temp)
        {
            origin2[j]=origin2[j-1];
            j--;
        }
        origin2[j]=temp;
        if(flag==true)
        {
            return true;
        }
    }
    return false;
}

bool mergesort()
{
    bool flag=false;
    for(int step=2;step/2<n;step*=2)
    {
        if(step!=2&&issame(origin2,change))
            flag=true;
    for(int i=0;i<n;i+=step)
    {
        sort(origin2+i,origin2+min(i+step,n));
    }
    if(flag==true)
    {
        showarray(origin2);
        return true;
    }
}
}
int main()
{
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&origin[i]);
        origin2[i]=origin[i];
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&change[i]);
    }
    if(insertsort())
    {
        printf("Insertion Sort\n");
        showarray(origin2);
    }
    else
    {
        printf("Merge Sort\n");
        for(int i=0;i<n;i++)
        {
            origin2[i]=origin[i];
        }
        mergesort();
    }

    return 0;
}

B 1036 跟奥巴马一起编程

#include <cstdio>
int main()
{
    int n,i,j,k;
    char c;
    scanf("%d %c",&n,&c);
	for(i=0;i<n;i++)
    {
        printf("%c",c);
    }
    printf("\n");
    if(i%2==0)
        k=i/2;
    else
    k=i/2+1;
    for(i=0;i<k-2;i++)
    {

        printf("%c",c);
        for(j=0;j<n-2;j++)
        {
            printf(" ");
        }
         printf("%c\n",c);
    }
    for(i=0;i<n;i++)
    {
        printf("%c",c);
    }

	return 0;
}

B 1037 在霍格沃茨找零钱

#include <cstdio>
int main()
{
    int n,a,b,y,num=0;
    int a1,a2,a3,b1,b2,b3;
    scanf("%d.%d.%d %d.%d.%d",&a1,&a2,&a3,&b1,&b2,&b3);
    int price1=a3+a2*29+a1*17*29;
    int price2=b3+b2*29+b1*17*29;
    int price=price2-price1;
    if(price<0)//不能将负数直接带入后面的运算,切记
    {
        printf("-");
        price=-price;
    }
   
    printf("%d.%d.%d",price/(29*17),(price/29)%17,price%29);
}

B 1038 统计同成绩学生

#include <stdio.h>
#include<string.h>
int main()
{
    int n,m;
    int score[101]={0};
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&m);
        score[m]++;
    }
    int t,k;
    scanf("%d",&t);
    for(i=0;i<t-1;i++)
    {
        scanf("%d",&k);
        printf("%d ",score[k]);
    }
    scanf("%d",&k);
    printf("%d",score[k]);
    return 0;
}

B 1039 到底买不买

#include <stdio.h>
#include<string.h>
int main()
{
    int n,m=0;
    char str1[10010],str2[10010];
    scanf("%s",str1);
    scanf("%s",str2);
    int len1=strlen(str1);
    int len2=strlen(str2);
    bool hashs[10010]={false};
    int i,j;
    for(i=0;i<len1;i++)
    {

        for(j=0;j<len2;j++)
        {
            if(str1[i]==str2[j]&&hashs[j]==false)
            {
                hashs[j]=true;
                break;
            }
        }
        if(j==len2)
            m++;

    }
        int t=len2;
        for(i=0;i<len2;i++)
        {
            if(hashs[i]==true)
                t--;
        }
        if(t>0)
        {
            printf("No %d",t);
        }
        else
        {
            printf("Yes %d",m);
        }
    return 0;
}



B 1040 有几个PAT?

注释部分是调试用的,最开始for循环检测字符‘p’(应该是大写P),导致结果一直出错,所以,要细心哦。

#include <cstring>
#include<cstdio>
#include<algorithm>
#include <iostream>
using namespace std;
const int MAXN=100010;
const int MOD=1000000007;
char str[MAXN];
int leftNUMP[MAXN]={0};
int main()
{
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<len;i++){
        if(i>0)
        {
            leftNUMP[i]=leftNUMP[i-1];
        }
        if(str[i]=='P')
        {
            leftNUMP[i]++;
    //          printf("%d\n",leftNUMP[i]);
        }
        }

        int ans=0;
        int rightNUMT=0;
    for(int i=len-1;i>=0;i--)
        {
        if(str[i]=='T')
        {
            rightNUMT++;
        }else if(str[i]=='A'){
       // printf("%d\n",leftNUMP[i]);
       // printf("%c\n",str[i]);
        ans=(ans+leftNUMP[i]*rightNUMT)%MOD;
        }
        }
        printf("%d\n",ans);
        return 0;
}


B 1041 考试座位号

#include<cstdio>
int main()
{
    struct student
    {
        long long id;
        int seat;
    }st[1000];
    int n,m,q,s,temp;
    long long p;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld%d%d",&p,&q,&s);
        st[q].id=p;
        st[q].seat=s;
    }
    scanf("%d",&m);
    for(int j=0;j<m;j++)
    {
        scanf("%d",&temp);
        printf("%lld %d\n",st[temp].id,st[temp].seat);
    }
    return 0;
}

B 1042 字符统计

#include <stdio.h>
#include<string.h>
int main()
{
    int n,max=0;
    char str[10010];
    gets(str);
    int len=strlen(str);
    int hashs[26]={0};
    int i,k;
    for(i=0;i<len;i++)
    {
        if(str[i]>='A'&&str[i]<='Z')
            str[i]+=32;
        if(str[i]>='a'&&str[i]<='z')
        {
             hashs[str[i]-'a']++;
        }
    }
    for(i=0;i<26;i++)
    {
        if(max<hashs[i])
        {
             max=hashs[i];
             k=i;
        }

    }
        printf("%c %d",'a'+k,max);
    return 0;
}



B 1043 输出PATest

#include <stdio.h>
#include<string.h>
int main()
{
    int n=0,max=0;
    char str[10010];
    gets(str);
    int len=strlen(str);
    int hashs[6]={0};
    char c[7]={'P','A','T','e','s','t'};
    int i,k,j;
    for(i=0;i<len;i++)
    {
        for(j=0;j<6;j++)
        {
            if(str[i]==c[j])
            {
                hashs[j]++;
                n++;
            }
        }
    }
    i=0;
  while(n>0)
    {
        for(i=0;i<6;i++)
        {
            if(hashs[i]>0)
            {
             printf("%c",c[i]);
             hashs[i]--;
             n--;
            }
        }
    }
    return 0;
}



B 1044 火星数字

注意使用getline()函数,如果前面用过scanf()函数,需要getchar()函数吸收一下换行符,不然geiline()读入的是空字符串。
或者使用scanf("%d%*c",&n);//读取一个整数后,丢弃紧跟在整数后边的一个字符

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<map>
using namespace std;

string low[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string high[13]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};

string numtostr[170];
map<string, int> strtonum;

void init()
{
    for(int i=0;i<13;i++)
    {
        numtostr[i]=low[i];
        strtonum[low[i]]=i;
        numtostr[i*13]=high[i];
        strtonum[high[i]]=i*13;
    }
    for(int i=1;i<13;i++)
    {
        for(int j=1;j<13;j++)
        {
            string str=high[i]+" "+low[j];
            numtostr[i*13+j]=str;
            strtonum[str]=i*13+j;
        }
    }
}

int main()
{
   init();
   int n;
   scanf("%d%*c",&n);//读取一个整数后,丢弃紧跟在整数后边的一个字符
   while(n--)
   {
       string str;
       getline(cin,str);
       if(str[0]>='0'&&str[0]<='9')
       {
           int num=0;
           for(int i=0;i<str.length();i++)
           {
               num=num*10+(str[i]-'0');
           }
           cout<<numtostr[num]<<endl;
       }
       else
       {
           cout<<strtonum[str]<<endl;
       }
   }
    return 0;
}

B 1045 快速排序

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=0x3fffffff;
int str[maxn];
int leftmax[maxn];
int rightmin[maxn];
int c[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&str[i]);
    }
    leftmax[0]=0;
    rightmin[n-1]=INF;
    for(i=1;i<n;i++)
    {
       leftmax[i]=max(leftmax[i-1],str[i-1]);
    }
    for(i=n-2;i>=0;i--)
    {
       rightmin[i]=min(rightmin[i+1],str[i+1]) ;
    }
    int count=0;
    int j=0;
     for(i=0;i<n;i++)
    {
        if(str[i]>leftmax[i]&&str[i]< rightmin[i])
        {
            c[j++]=str[i];        }
    }
     printf("%d\n",j);
     for(i=0;i<j;i++)
    {
        printf("%d",c[i]);
        if(i<j-1)
        printf(" ");
    }
    printf("\n");
    return 0;
}

B 1046 划拳

#include<cstdio>
int main()
{
    int n,m1=0,m2=0;
    int a1,a2,b1,b2,ans;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d%d",&a1,&a2,&b1,&b2);
        ans=a1+b1;
        if(a2==ans&&b2!=ans)//甲猜中,并且乙没有猜中,不能丢了乙没有中这个条件
            m2++;
        if(b2==ans&&a2!=ans)
            m1++;
    }
    printf("%d %d",m1,m2);
    return 0;
}

B 1047 编程团体赛

#include <stdio.h>
#include<string.h>
int main()
{
    int i,n,c,s,k,t,max=0;
    scanf("%d",&n);
    int score;
    int hash[10010]={0};
    for(i=0;i<n;i++)
    {
        scanf("%d-%d %d",&k,&c,&s);
        hash[k]+=s;
    }
    for(i=0;i<10010;i++)
    {
       if(max<hash[i])
       {
           max=hash[i];
           t=i;
       }
    }
  printf("%d %d",t,max);
    return 0;
}



B 1048 数字加密

#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    char str1[110],str2[110],str3[110];
    scanf("%s %s",str1,str2);
    int len1=strlen(str1);
    int len2=strlen(str2);
    char c[13]={'0','1','2','3','4','5','6','7','8','9','J','Q','K'};
    reverse(str1,str1+len1);
    reverse(str2,str2+len2);
    int len=max(len1,len2);
    int i,ans=0;
    for(i=0;i<len;i++)
    {
        int x=i<len1?str1[i]-'0':0;//这一步预处理可以记下来当模板
		int y=i<len2?str2[i]-'0':0;
        if(i%2==0)
        {
            ans=(x+y)%13;


        }
        else
        {
            ans=y-x;
            if(ans<0)
                ans=ans+10;

        }
          str3[i]=c[ans];
    }
    for(i=len-1;i>=0;i--)
        printf("%c",str3[i]);
    return 0;
}

B 1049 数列的片段和

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
double k,sum=0;
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%lf",&k);
        sum+=i*(n+1-i)*k;
    }
   printf("%.2f",sum);
    return 0;
}

B 1050 螺旋矩阵

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int m[10010][10010];
int a[10010];

bool cmp(int a, int b)
{
	return a>b;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    if(n==1)
    {
        printf("%d",a[0]);
        return 0;
    }
    sort(a,a+n,cmp);
    int hang=(int)ceil(sqrt(1.0*n));
    while(n%hang!=0)
    {
        hang++;
    }
    int lie=n/hang,now=0;
    int i=1,j=1;
    int u=1,d=hang,l=1,r=lie;
    while(now<n)
    {
        while(now<n&&j<r)
        {
            m[i][j]=a[now++];
            j++;
        }
         while(now<n&&i<d)
        {
            m[i][j]=a[now++];
            i++;
        }
         while(now<n&&j>l)
        {
            m[i][j]=a[now++];
            j--;
        }
         while(now<n&&i>u)
        {
            m[i][j]=a[now++];
            i--;
        }
        u++;
        d--;
        l++;
        r--;
        i++;
        j++;
        if(now==n-1)
        {
            m[i][j]=a[now++];
        }
    }
        for(i=1;i<=hang;i++)
        {
            for(j=1;j<=lie;j++)
            {
                printf("%d",m[i][j]);
                if(j<lie)
                    printf(" ");
                else
                    printf("\n");
            }
        }
    return 0;
}

B 1051 负数乘法

#include <iostream>
#include <cmath>
#include<stdio.h>
using namespace std;
int main(){
	double r1, p1, r2, p2;
	cin >> r1 >> p1 >> r2 >> p2;
	double a = (r1 * r2) * cos(p1 + p2);
	double b = (r1 * r2) * sin(p1 + p2);
	if(fabs(a) < 0.001)
		a = 0;
	if(fabs(b) < 0.001)
		b = 0;
	if(b < 0)
		printf("%.2lf-%.2lfi", a, fabs(b));
	else
		printf("%.2lf+%.2lfi", a, b);
	return 0;
}

B 1052 卖个萌

该代码借鉴柳神,自己根本写不出。stc的用法了解的太浅了

#include<vector>
#include<iostream>
using namespace std;
vector<vector<string> > v;//二维变长数组
int main()
{
	
	for(int i=0;i<3;i++)
	{
		string s;
		getline(cin,s);
		vector<string> row;//数据压入堆中
		int j=0,k=0;
		while(j<s.length())
		{
			if(s[j]=='[')
			{
				while(k++<s.length())
				{
					if(s[k]==']')
					{
						row.push_back(s.substr(j+1,k-j-1));//获取字符串中一段 
						break;
					}
				}
			}
			j++;		
		}
		v.push_back(row);
	}	
	
	int n;
	cin >> n;
	while(n--)
	{
		int a,b,c,d,e;
		cin >> a >> b >> c >> d >> e;
		if(a>v[0].size()||b>v[1].size()||c>v[2].size()||d>v[1].size()||e>v[0].size()||a<1||b<1||c<1||d<1||e<1)	
		{
			cout << "Are you kidding me? @\\/@"<< endl;
		}
		else
		{
			cout << v[0][a-1] << "(" << v[1][b-1]  << v[2][c-1] << v[1][d-1] << ")" <<v[0][e-1]<< endl;
		}
	}
	 
} 

B 1053 住房空置率

#include<vector>
#include<iostream>
#include<stdio.h>
using namespace std;
//float v[10010];
int main()
{
	int n,d;
	float e,v;
	scanf("%d%f%d",&n,&e,&d);
	int i,j,daycount,kn=0,k=0;
	for(i=0;i<n;i++)
	{
		int day;
		scanf("%d",&day);
		daycount=0;
		for(j=0;j<day;j++)
		{
		    scanf("%f",&v);
		    if(v<e)
            {
                daycount++;
            }
		}
		if(daycount>day/2)
        {
            if(day>d)
            {
                k++;
            }
            else
            {
                kn++;
            }
        }
   }
   printf("%.1f%% %.1f%%",kn*100.0/n,k*100.0/n);
   return 0;

}

B 1054 求平均值

stringstream ss(s)来实现字符串与浮点数的转换

#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<sstream>
#include<algorithm>
#include <ctype.h>
using namespace std;
int n,num=0;		//num统计个数
double ans,t;
int main()
{
	string s;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		int cnt=0,pos=0,flag=true;
		for(int j=0;j<s.size()&&flag;j++){
			if(!((isdigit(s[0]))||s[0]=='-'))//第一个位置只能是数字和符号,如果出现字母,就是不合法
				flag=false;
			if(s[j]=='.') {		//统计小数点个数以及位置
				cnt++;
				pos=j;
			}
		}
		stringstream ss(s);    //使用字符串流把字符串转成浮点数
		ss>>t;
		if(!flag||cnt>1||abs(t)>1000||(cnt&&s.size()-1-pos>2)){	//数字超过1000或小数点存在且位数超过2位均非法
			cout<<"ERROR: "<<s<<" is not a legal number\n";
			 continue;
		}
		ans+=t;
		num++;
	}
	if(num==0) printf("The average of 0 numbers is Undefined\n");
	else if(num==1) printf("The average of 1 number is %.2f\n",ans);
	else printf("The average of %d numbers is %.2f\n",num,ans/num);
	return 0;
}

B1056 组合数

#include <iostream>
#include <cmath>
#include <map>
using namespace std;
const int maxn=20;
int str[maxn];
int main(){
	int n,s;
	bool flag=false;
	cin >> n ;
	int i,j,sum=0;
	for(i = 0; i < n; i++){
			cin >>str[i];
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(str[i]!=str[j])
            {
                sum+=str[i]*10+str[j];
            }
        }
    }
        cout<<sum;
	return 0;
}

B 1057 数零壹

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
using namespace std;
int z[1010];

int main()
{
    string str;
    getline(cin,str);
    int i,sum=0,num=0;
    for(i=0;i<str.size();i++)
    {
        str[i] = tolower(str[i]);
        if(isalpha(str[i]))
           sum+=(str[i]-'a')+1;
    }
    while(sum!=0)
    {
        z[num++]=sum%2;
        sum=sum/2;
    }
    int a=0,b=0;
    for(i=num-1;i>=0;i--)
    {
        if(z[i]==0)
            a++;
        else
            b++;
    }
    printf("%d %d",a,b);
	return 0;
}

B 1058 选择题

第二个测试用例不通过,18分

#include<iostream>
#include<string>
#include<cmath>
#include<stdio.h>
using namespace std;
struct number {
	double mark;
	int nc;
	int ny;
	int ncount;        //用于记录每个选项错误次数
	int pd[101] = { };

}s[101];

int judge(int a[100], int b[100] ,int t,int k) {    //a为正确选项,b为输入选项
	int count = 0;    //正确计数器
	for (int i = 0; i < s[t].nc; i++) {
		if (a[i]&&b[i]) {            //如果输入为正确,正确计数器+1
			count++;
		}
	}
	//printf("%d--%d--%d\n",count,s[k].ny,s[k].nc);
	if (count == s[t].ny&&s[t].ny==k)
    {
        return s[t].mark;
    }//全部正确
     else
    {
        s[t].ncount++;
        return 0;
    }                            //返回得分
}
int main() {
	int n, m, k;
	int sum;
	char ch;
	int t[101];        //用t记录输入的选项
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s[i].mark >> s[i].nc >> s[i].ny;
		for (int j = 0; j < s[i].ny; j++) {
			cin >> ch;
			s[i].pd[ch - 'a'] = 1;
		}
	}
	for (int i = 0; i < n; i++) {
		sum = 0;
		for (int j = 0; j < m; j++) {
			cin >> ch >> k;
			for (int i = 0; i < 101; i++) t[i] = 0;    //t置为空
			for (int h = 0; h < k; h++) {
				cin >> ch;
				t[ch - 'a']++;
			}
			sum+=judge(s[j].pd, t,j,k);
			cin >> ch;
		}
		printf("%d\n", sum);
	}
	int max = 0;
	for (int i = 0; i < m; i++) {        //寻找最大值
			max = s[i].ncount> max ? s[i].ncount : max;
	}
	cout << max << " ";
	int count=0;
	int error[101];
	if (max) {                        //存在最大值(即有错题)
		for (int i = 0; i < m; i++) {
				if (s[i].ncount== max) {
					error[count++]=i+1;
				}
		}
		for(int j=0;j<count;j++)
        {
            printf("%d",error[j]);
            if(j<count-1)
                printf(" ");
        }
	}
	else
		cout << "Too simple";
	return 0;
}

B 1061 c语言竞赛

#include<iostream>
#include<string>
#include<cmath>
#include<stdio.h>
#include<math.h>
using namespace std;

int str[10010]={0};
bool flag[10010]={false};

bool isPrime(int n)
{
    if(n<=1)
        return false;
    int sqr=(int)sqrt(1.0*n);
    for(int i=2;i<=sqr;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}

int main() {
	int n, temp, k,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&temp);
        str[temp]=i;
    }
    scanf("%d",&k);
    for(i=0;i<k;i++)
    {
        scanf("%d",&temp);
        if(str[temp]==1&&flag[temp]==false)
        {
            printf("%04d: Mystery Award\n",temp);
            flag[temp]=true;
        }
        else if(isPrime(str[temp])&&flag[temp]==false)
        {
            printf("%04d: Minion\n",temp);
            flag[temp]=true;
        }
        else if(str[temp]==0)
        {
            printf("%04d: Are you kidding?\n",temp);
        }
        else if(flag[temp]==true)
        {
            printf("%04d: Checked\n",temp);
        }
        else
        {
            printf("%04d: Chocolate\n",temp);
            flag[temp]=true;
        }
    }
	return 0;
}

B 1060 爱丁顿数

第三个测试用例显示超时

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;

int main()
{
    int hashs[10010]={0};
    int n,m,i;
    int maxn=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&m);
        hashs[m]++;
        if(m>maxn)
            maxn=m;
    }
    int sum=0;
    for(i=maxn-1;i>=0;i--)
    {
        hashs[i+1]+=sum;
        if(i<=hashs[i+1])
        {
            printf("%d",i);
            break;
        }
        else
        {
            sum=hashs[i+1];
        }
    }
    return 0;
}

B 1061 判断题

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct score
{
    int full;
    int answer;
}person[110];

int main()
{
    int n,m,i,j;
    int maxn=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%d",&person[i].full);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&person[i].answer);
    }
    int k,sum;
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<m;j++)
        {
            scanf("%d",&k);
        if(k==person[j].answer)
          sum+=person[j].full;
        }
        printf("%d\n",sum);
    }
    return 0;
}

B 1062 最简分数

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;

int gcd(int a,int b)
{
    return !b?a:gcd(b,a%b);
}

int main()
{
    int n,k,i,a1,a2,b1,b2;
    scanf("%d/%d %d/%d %d",&a1,&b1,&a2,&b2,&k);
    double t1=a1*1.0/b1;
    double t2=a2*1.0/b2;
    double maxn=max(t1,t2);
    double minn=min(t1,t2);
    int c[1001];
    int j=0;
   for(int i=1;i<k;i++)
		{
			double t=i*1.0/k;
			if(gcd(i,k)==1&&t>minn&&t<maxn)
			{
				c[j++]=i;
				//printf("%d\n",i);
			}
		}
       for(int i=0;i<j;i++)
    {
       printf("%d/%d",c[i],k);
       if(i<j-1)
        printf(" ");
    }
    return 0;
}

B 1063 计算谱半径

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;

int main()
{
    int n,x,y;
    double m,maxn=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			m=sqrt((x*x+y*y)*1.0);
          if(m>maxn)
            maxn=m;
		}
    printf("%.2f",maxn);
    return 0;
}

B 1064 朋友数

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;

int main()
{
    int n,x,y,sum,maxn=0;
    char str[10010];
    int hash[100]={0};
    memset(str,0,sizeof(hash));
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
		{
			scanf("%s",str);
			sum=0;
			int len=strlen(str);
			for(int j=0;j<len;j++)
            {
                sum+=str[j]-'0';
            }

            hash[sum]++;

            if(sum>maxn)
                maxn=sum;
		}
    int count=0;
    for(i=0;i<=maxn;i++)
    {
        if(hash[i]>0)
        {
            count++;
        }
    }
    int m=0;
    printf("%d\n",count);
     for(i=0;i<=maxn;i++)
    {
        if(hash[i]>0)
        {
            printf("%d",i);
            m++;
             if(m<count)
            printf(" ");
        }

    }
    return 0;
}

B 1065 单身狗

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1000010;
int p[maxn],t[maxn];
bool attend[maxn];
int main()
{
	int n,a,b,m,j;
	vector<int> ans,temp;
    scanf("%d",&n);
    fill(p,p+maxn,-1);
		memset(attend,0,sizeof(attend));
		for(int i=0;i<n;++i)
		{
			scanf("%d %d",&a,&b);//有配偶的话值不为0,而为配偶的id
			p[a]=b;
			p[b]=a;
		}
		scanf("%d",&m);
		for(int i=0;i<m;++i)
		{
			scanf("%d",&t[i]);
			if(p[t[i]]==-1)
				ans.push_back(t[i]);
			else
			{
				//如果这个人有配偶
				temp.push_back(p[t[i]]);//把他的配偶id登记在名单temp上
			}
		}
		for(int i=0;i<temp.size();++i)
		{
		    for(j=0;j<m;j++)//遍历所有到现场客人
            {
                if(temp[i]==t[j])
            {
                break;
            }
            }
			if(j==m)//名单上没他,他肯定是单身狗啊
				ans.push_back(p[temp[i]]);//插入到单身狗名单
		}
		sort(ans.begin(),ans.end());
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();++i)
		{
			printf("%05d",ans[i]);
			if(i!=ans.size()-1)
				printf(" ");
			else
				printf("\n");
		}
	return 0;
}

B 1066 图像过滤

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10010;
int p[maxn][maxn];
int main()
{
	int m,n,a,b,k;
    scanf("%d%d%d%d%d",&m,&n,&a,&b,&k);
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&p[i][j]);
            if(p[i][j]>=a&&p[i][j]<=b)
            {
                p[i][j]=k;
            }
        }
    }
      for(i=0;i<m;i++)
    {
        for(j=0;j<n-1;j++)
        {
            printf("%03d ",p[i][j]);
        }
        printf("%03d\n",p[i][j]);
    }
	return 0;
}

B 1067 试密码

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include<iostream>
using namespace std;
const int maxn=10010;
char str[maxn];
char c[maxn];
int main()
{
	int m,n,a,b,k;
    scanf("%s %d",str,&n);
    int i,j;
    getchar();
    while(cin.getline(c,maxn))
    {
        int lenc=strlen(c);
        if(c[0]=='#')
            break;
        else
        {
            if(n>=0&&strcmp(c,str)==0)
            {
                printf("Welcome in\n");
                break;
            }
            else if(n>=0&&strcmp(c,str)!=0)
            {
                printf("Wrong password: %s\n",c);
                n--;
            }
            if(n<=0)
            {
                printf("Account locked\n");
                break;
            }
        }
    }
	return 0;
}

B 1068 万花从中一点红

此题参考一位博主的答案,对于四周八个数比较的处理,甚是巧妙。

#include <iostream>
#include <cmath>
#include <map>
using namespace std;
int picture[1005][1005] = {0}, sub1, sub2, num;
map<int, int> match;
int main(){
	int M, N, tol, cnt = 0, f = 0;
	cin >> M >> N >> tol;
	for(int i = 1; i < N+1; i++){
		for(int j = 1; j < M+1; j++){
			cin >> picture[i][j];
			match[picture[i][j]]++;
		}
			
	}
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			int flag = 0;
			if(match[picture[i][j]] != 1)
				continue;
			for(int k = i-1; k <= i+1; k++){
				for(int l = j-1; l <= j+1; l++){
					if(k == i && l == j)
						continue;
					if((abs(picture[i][j] - picture[k][l]) <= tol)){
						flag = 1;
						break;
					}
				}
				if(flag)	break;
			}
			if(!flag){
				sub1 = i;
				sub2 = j;
				num = picture[i][j];
				cnt++;
			}
		}
	}
	if(!cnt)	cout << "Not Exist";
	else if(cnt > 1)	cout << "Not Unique";
	else	cout << "(" << sub2 << ", " << sub1 << "): " << num; 
	return 0;
}

B 1069 微博转发抽奖

18分,两个测试样例没有通过

#include <iostream>
#include <cmath>
#include <map>
using namespace std;
const int maxn=1010;
string str[maxn];
map<string,int> hashs;
int main(){
	int m, n,s;
	bool flag=false;
	cin >> m >> n >>s ;
	for(int i = 1; i < m+1; i++){
			cin >>str[i];
	}
	for(int j=s;j<=m;j=j+n)
    {
        while(hashs[str[j]]>0&&j<=m)
        {
            j++;
        }
        if(hashs[str[j]]==0)
        {
             cout<<str[j]<<endl;
             flag=true;
             hashs[str[j]]++;
        }       
    }
	if(flag==false)
        cout<<"keep gong...";
	return 0;
}

B 1070 结绳

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int i,s[10010];
    for(i=0;i<n;i++)
    {
        scanf("%d",&s[i]);
    }
    sort(s,s+n);
    double sum=s[0];
    for(i=1;i<n;i++)
    {
        sum=(sum+s[i])/2;
    }
    printf("%d",(int)sum);
    return 0;
}

B 1071 小赌怡情

此题不难,但是如果没有想到比较巧妙的比较方法,就会很繁琐。if(n1<n2==b)也是参考某博主的方法,确实巧妙

#include <cstdio>
 
int main()
{
	int n1,n2,b,T,t,k;
	scanf("%d %d",&T,&k);
	for(int i=0;i<k;++i)
	{
		scanf("%d %d %d %d",&n1,&b,&t,&n2);
		if(t>T)
		{
			printf("Not enough tokens.  Total = %d.\n",T);
		}
		else
		{
			if(n1<n2==b)
			{
				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;
}

B 1072 开学寄语

#include <iostream>
#include<stdio.h>
using namespace std;
const int maxn=10010;
int check[maxn];
int c[maxn];
bool hasht[maxn]={false};
struct person
{
    char str[6];
    int k;
    int package[12];
}people[maxn];

int main()
{
    int n,m,temp,p,t=0,onum=0,pnum=0;
    scanf("%d %d",&n,&m);
    int i,j;
    for(i=0;i<m;i++)
    {
        scanf("%d",&temp);
        hasht[temp]=true;
    }
    for(i=0;i<n;i++)
    {
        scanf("%s %d",people[i].str,&people[i].k);
        t=0;
        for(j=0;j<people[i].k;j++)
        {
           scanf("%d",&people[i].package[j]);
           if(hasht[people[i].package[j]]==true)
           {
               c[t++]=people[i].package[j];
               onum++;
           }
        }
       // printf("%d\n",t);
        if(t>0)
        {
             printf("%s: ",people[i].str);
             pnum++;
         for(p=0;p<t-1;p++)
        {
            printf("%04d ",c[p]);
        }
         printf("%04d\n",c[p]);
        }


    }
    printf("%d %d\n",pnum,onum);
    return 0;
}

B 1073 多选题常见计分法

参考某博主的答案,我反正不会

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
struct number {
	double mark;//满分值
	int nc;//选项个数
	int ny;//正确选项个数
	int ncount[101] = {0};        //用于记录每个选项错误次数
	int pd[101] = { 0 };
 
}s[101];
double judge(int a[100], int b[100] ,int k) {    //a为正确选项,b为输入选项
	int count = 0;    //正确计数器
	int yn = 0;        //0为答对,1为答错
	for (int i = 0; i < s[k].nc; i++) {
		if (b[i] && a[i]) {            //如果输入为正确,正确计数器+1
			count++;
		}
		else if (a[i] && !b[i]) {      //没有输入的正确选项
			s[k].ncount[i]++;
		}
		else if (!a[i] && b[i]) {      //输入了一个错误的选项
			s[k].ncount[i]++;
			yn = 1;
		}
	}
	if (yn)
		return 0;
	else if (count != s[k].ny)    //部分正确
		return s[k].mark / 2;
	else if (count == s[k].ny)    //全部正确
		return s[k].mark;         //返回得分
}
int main() {
	int n, m, k;
	double sum;
	char ch;
	int t[101];        //用t记录输入的选项
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> s[i].mark >> s[i].nc >> s[i].ny;
		for (int j = 0; j < s[i].ny; j++) {
			cin >> ch;
			s[i].pd[ch - 'a'] = 1;
		}
	}
	for (int i = 0; i < n; i++) {
		sum = 0;
		for (int j = 0; j < m; j++) {
			cin >> ch >> k;//ch用于消除输入的左括号
			for (int i = 0; i < 101; i++) t[i] = 0;    //t置为空
			for (int h = 0; h < k; h++) {
				cin >> ch;
				t[ch - 'a']++;
			}
			sum+=judge(s[j].pd, t,j);
			cin >> ch;//ch用于消除输入的右括号
		}
		printf("%0.1lf\n", sum);
	}
	int max = 0;    
	for (int i = 0; i < m; i++) {        //寻找最大值
		for (int j = 0; j < s[i].nc; j++)
			max = s[i].ncount[j] > max ? s[i].ncount[j] : max;
	}
	if (max) {                        //存在最大值(即有错题)
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < s[i].nc; j++)
				if (s[i].ncount[j] == max) {
					cout << max << " " << i + 1 << "-" << (char)(j + 'a') << endl;
				}
		}
	}
	else
		cout << "Too simple";
	return 0;
}

B 1074 宇宙无敌加法器

最后一个测试用例过不掉,19分

#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int s[25]={0},a1[25]={0},b1[25]={0};
int convert(int a[],char str[])
{
	int len=strlen(str);
	int i,j=0;
    while(str[j]=='0')
    {
        j++;
    }
	for(i=len-1;i>=j;--i)
	{
		a[len-1-i]=str[i]-'0';

	}
	return len-j;
}
int main()
{
	char sca[25],a[25],b[25];
	scanf("%s %s %s",sca,a,b);
	int n=convert(s,sca);
	int an=convert(a1,a);
	int bn=convert(b1,b);
    //printf("%d %d",an,bn);
	int ans[25]={0};
	int carry=0,sum;
	int len=max(an,bn);
	//printf("%d",len);
	int i,j;
	for(i=0;i<len;++i)
	{
		if(s[i]==0)
			s[i]=10;
       // printf("%d\n",s[i]);
		carry=a1[i]+b1[i]+carry;
		ans[i]=carry%s[i];
		carry/=s[i];
	}
	if(carry!=0)
    {
        ans[i]=carry;
    }
    else
    {
        i--;
    }
	for(j=i;j>=0;--j)
	{
			printf("%d",ans[j]);
	}
	return 0;
}

B 1075 链表元素分类

此题与1025反转链表有点相似,因为那个题目一直在纠结排序,其实不用纠结排序,直接定义三个数组,分别顺序遍历原链表,将满足条件的地址存储下来,那么不合法的链表元素就会自动忽略,反而简单了。

#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=100010;
struct Node
{
    int address,data,next;
    int order;
}list[maxn];

int main()
{
    int begin,n,k,address;
    vector<int> v[3];
    scanf("%d%d%d",&begin,&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&address);
        scanf("%d%d",&list[address].data,&list[address].next);
        list[address].address=address;
    }
    int p=begin,count=0;
    while(p!=-1)
    {
        int data = list[p].data;
        if (data < 0)
            v[0].push_back(p);
        else if (data >= 0 && data <= k)
            v[1].push_back(p);
        else
            v[2].push_back(p);
        p = list[p].next;


    }
    int flag=0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < v[i].size(); j++) {
            if (flag == 0) {
                printf("%05d %d ", v[i][j], list[v[i][j]].data);
                flag = 1;
            } else {
                printf("%05d\n%05d %d ", v[i][j], v[i][j], list[v[i][j]].data);
            }
        }
    }
    printf("-1");
}

B 1076 Wifi密码

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;
    string str;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
      getline(cin,str);
       for(int j=0;j<str.length();j++)
       {
           if(str[j]=='T')
            printf("%d",str[j-2]-'A'+1);
       }
    }
   return 0;
}

B 1077 互评成绩计算

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
	int n,m,g1,t;
	int score[110];
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;++i)
	{
		scanf("%d",&g1);
        int p=0;
		for(int j=1;j<n;++j)
		{
			scanf("%d",&t);
			if(t>=0&&t<=m)
				score[p++]=t;
		}
		sort(score,score+p);
		double sum=0.0;
		for(int j=1;j<p-1;++j)
			sum+=score[j];
		sum=(sum/(p-2)+g1)/2;
		printf("%.0f\n",round(sum));
	}
	return 0;
}

B 1078 字符串压缩与解压

#include <iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
    char t;
    cin >> t;
    getchar();//需要用getchar()将前面的换行符读取,再使用getline()
    string s, num;
    getline(cin, s);
    int cnt = 1;
    if (t == 'D') {
        for (int i = 0; i < s.length(); i++)
            {
            if (s[i] >= '0' && s[i] <= '9')
            {
                num += s[i];
            } else {
                if (num.length() > 0)
                    cnt = stoi(num);
                while(cnt--) 
                 cout << s[i];
                cnt=1;
                num = "";
            }
        }
    } else {
        char pre = s[0];
        for (int i = 1; i < s.length(); i++) {
            if (s[i] == pre) {
                cnt++;
            } else {
                if (cnt >= 2) cout << cnt;
                cout << pre;
                cnt = 1;
                pre = s[i];
            }
        }
        if (cnt >= 2) cout << cnt;
        cout << pre;
    }
    return 0;
}

B 1079 延迟的回文数

#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int add(char a[],char b[],int len)
{
    char str3[1010];
    int i,carry=0;
    reverse(a,b+len);
    reverse(a,b+len);
    for(i=0;i<len;i++)
    {
        int x=a[i]-'0';
		int y=b[i]-'0';
        int temp=x+y+carry;
        a[i]=temp%10+'0';
        carry=temp/10;
    }
    if(carry!=0)
    {
        a[i]=carry+'0';
        len++;
    }
    reverse(a,a+len);
    return len;
}

int main()
{
    char str1[10010],str2[10010];
    scanf("%s",str1);
    int i;
    for(i=0;i<10;i++)
    {
        int len1=strlen(str1);
        strcpy(str2,str1);
        reverse(str2,str2+len1);
        if(strcmp(str1,str2)==0)
        {
            printf("%s is a palindromic number.",str1);
            break;
        }
        else
        {
            printf("%s + %s = ",str1,str2);
            len1=add(str1,str2,len1);
            printf("%s\n",str1);
        }
    }
    if(i==10)
    {
        printf("Not found in 10 iterations.");
    }

    return 0;
}

B 1080 MOOC期终成绩

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include<string.h>
using namespace std;
struct Student {
	string stuNum;
	int Gp;
	int Gmid;
	int Gfinal;
	int G;
	bool flag;	//编程成绩 期中 期末 总评
};

bool cmp(Student a, Student b)
{
    if(a.G!=b.G)
		return a.G>b.G;
    else
	return a.stuNum<b.stuNum;
}

int main()
{
	int P, M, N,i,temp,j,index=1;
	string stuNum;
	cin >> P >> M >> N;
	vector<Student> stus;
	map<string, int> idx;
	for (i = 0; i < P; i++)
	{
		cin >> stuNum >> temp;
		if (temp >= 200)
		{
			stus.push_back(Student{ stuNum ,temp,-1,-1,0 });
			idx[stuNum] = index++;	//比真正的下标大1
		}

	}
	for (i = 0; i < M; i++)
	{
		cin >> stuNum >> temp;
		if (idx[stuNum])
			stus[idx[stuNum] - 1].Gmid = temp;
	}
	for (i = 0; i < N; i++)
	{
		cin >> stuNum >> temp;
		if (idx[stuNum])
		{
			stus[idx[stuNum] - 1].Gfinal = temp;
			if (stus[idx[stuNum] - 1].Gmid > stus[idx[stuNum] - 1].Gfinal)
				stus[idx[stuNum] - 1].G = round(stus[idx[stuNum] - 1].Gmid*0.4 + stus[idx[stuNum] - 1].Gfinal*0.6);
			else
				stus[idx[stuNum] - 1].G = stus[idx[stuNum] - 1].Gfinal;
		}
	}
	sort(stus.begin(), stus.end(), cmp);
	for (i = 0; i < stus.size(); i++)
	{
		if (stus[i].G >= 60)
		{
			cout << stus[i].stuNum << " " << stus[i].Gp << " " << stus[i].Gmid << " " << stus[i].Gfinal << " " << stus[i].G << endl;
		}
	}
    return 0;
}



B 1081 检查密码

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
using namespace std;

int main()
{
	int n,p,m,t;
	string str;
	cin>>n;
	getchar();//在接收完n后要getchar()读取一下换行符才能用getline,否则换行符会被读进getline中
		for(int i=0;i<n;++i)
		{
			getline(cin,str);//字符串里面可能会有空格,所以不能直接用cin,要用getline接收一行字符
			int len=str.length();
			if(len<6)
				printf("Your password is tai duan le.\n");
			else
			{
				int num=0;
				int alp=0;
				int flag=0;
				for(int j=0;j<len;++j)
				{
					if(!isalnum(str[j])&&str[j]!='.')
					{
						flag=1;
						break;
					}
					else
					{
						if(isdigit(str[j]))
							++num;
						else if(isalpha(str[j]))
							++alp;
					}
				}
				if(flag==1)
					printf("Your password is tai luan le.\n");
				else
					if(num==0)
						printf("Your password needs shu zi.\n");
					else if(alp==0)
						printf("Your password needs zi mu.\n");
					else
						printf("Your password is wan mei.\n");
			}
		}
	return 0;
}

B 1082 射击比赛

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
using namespace std;

int people[10010];

int main()
{
	int n,x,y,temp,ans,maxn=0,minn=1000010;
	int p,q;
	cin>>n;
	int i;
    for(i=0;i<n;i++)
    {
        cin>>temp >> x>> y;
        ans=x*x+y*y;
        if(ans>maxn)
        {
            maxn=ans;
            p=temp;
        }
        if(ans<minn)
        {
            minn=ans;
            q=temp;
        }
    }
    cout<<q <<p;
	return 0;
}

B 1083 是否存在相等的差

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
#include<cmath>
using namespace std;

int card[10010];
int hashs[10010]={0};

int main()
{
	int n,x,y,temp,maxn=0;
	cin>>n;
	int i;
    for(i=1;i<=n;i++)
    {
        cin>>card[i];
        temp=abs(i-card[i]);
        hashs[temp]++;
        maxn=max(maxn,temp);
    }
    for(i=maxn;i>=0;i--)
    {
        if(hashs[i]>1)
            {
                 printf("%d %d\n",i,hashs[i]);
            }
    }
}

B 1084 外观数列

#include<iostream>
using namespace std;
int main()
{
	string s;
	int n;
	cin>>s>>n;
	for(int j=0;j<n-1;j++){
		string ans;
		char pre = s[0];
		int cnt = 0;
		for(int i = 0;i < s.length();i++){
			if(s[i] == pre)	cnt++;
			else{
				ans+=pre;
				ans+=cnt+'0';
				pre=s[i];
				cnt = 1;
			}
		}
		if(cnt > 0){//这一步不能丢
			ans+=pre;
			ans+=cnt+'0';
		}
		s = ans;
	}
	cout << s ;
	return 0;
}

B 1085 PAT单位排行

#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

struct school
{
	string name;
	int pnum;
	double score;
	int rank;
	school()
	{
		pnum=0;
		score=0.0;
	}
};
bool cmp(school a,school b)
{
	if(a.score!=b.score)
		return a.score>b.score;
	if(a.pnum!=b.pnum)
		return a.pnum<b.pnum;
	return a.name<b.name;
}


int main()
{
    map<string,school> mp; 
	int n,tscore;
	string temp,sch;
	scanf("%d",&n);
	int i;
    for(i=0;i<n;i++)
    {
        cin>>temp>>tscore>>sch;
        for(int j=0;j<sch.length();j++)
        {
            sch[j]=tolower(sch[j]);
        }
       if(mp.count(sch)==0)
       {
            school a;
			a.name=sch;
            mp[sch]=a;
       }
       ++mp[sch].pnum;
       if(temp[0]=='T')
			mp[sch].score+=tscore*1.5;
       else if(temp[0]=='A')
			mp[sch].score+=tscore;
       else
			mp[sch].score+=tscore/1.5;
		}
		vector<school> s;
		for(map<string,school>::iterator it=mp.begin();it!=mp.end();++it)
		{
			it->second.score=(int)it->second.score;//要对求出的所有分数取整,用迭代器
			s.push_back(it->second);//将处理好的值压入vect数组进行排序,
			//map这种容器是用红黑树实现的,元素本身有序,故不允许用sort排序
		}
		sort(s.begin(),s.end(),cmp);
		printf("%d\n",s.size());
		for(int i=0;i<s.size();++i)
		{
			s[i].rank=i+1;
			if(i!=0&&s[i].score==s[i-1].score)
				s[i].rank=s[i-1].rank;
			printf("%d %s %d %d\n",s[i].rank,s[i].name.c_str(),(int)s[i].score,s[i].pnum);
		}

	return 0;
}

B 1086 就不告诉你

#include<iostream>
#include<string>
using namespace std;
int main()
{
	long a,b;
	cin>>a>>b;
	string s=to_string(a*b);
    int i,j;
    for(j=s.length()-1;j>=0;j--)
    {
        if(s[j]!='0')
        break;
    }
	for(i=j;i>=0;i--)
    {
        cout<<s[i];
    }
	return 0;
}

B 1087 有多少不同的值

#include<iostream>
#include<string>
using namespace std;
const int maxn=100010;

int hashs[maxn];

int main()
{
	int n,temp;
	cin>>n;
	int i;
	for(i=1;i<=n;i++)
    {
        temp=i/2+i/3+i/5;
        hashs[temp]++;
    }
    int sum=0;
    for(i=0;i<maxn;i++)
    {
        if(hashs[i]>0)
            sum++;
    }
    cout<<sum;
	return 0;
}

B 1088 三人行

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
#include<math.h>
using namespace std;

void compare(int m,int t)
{
    if(m>t)
        printf("Cong");
    else if(m==t)
        printf("Ping");
    else
        printf("Gai");


}

int main()
{
    int m,x,y,a,b;
    double c;
    cin>>m>>x>>y;
    for(a=99;a>9;a--)
    {
        char str[3];
        sprintf(str,"%d",a);
        char r=str[0];
        str[0]=str[1];
        str[1]=r;
        sscanf(str,"%d",&b);
        int d=b-a;
        if(d<0)
            d=-d;
        if(d*y==x*b)
        {
            break;
        }
    }
    if(a==9)
        printf("No Solution");
    else
    {
        c=b*1.0/y;
        //printf(" %d %d %d %f\n",m,a,b,c);
        printf("%d ",a);
        compare(a,m);
        printf(" ");
        compare(b,m);
        printf(" ");
        if(c>m)
          printf("Cong");
        else if(c==m)
          printf("Ping");
        else
          printf("Gai");
    }
	return 0;
}

B 1089 狼人杀-简单版

该题拿到手,完全没有思路,感觉就是博弈。
`但是实际上就是暴力穷举法,题目中的输入和条件都化成限制,来一个个测试是否成立。
此代码参考某博主的,只是加了详细备注。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
	int n;	
	cin>>n;
	vector<int> v(n+1);//首先将数据存起来,存入vector型数组v(n+1)
	for(int i=1;i<=n;i++)	
        cin>>v[i];
	for(int i=1;i<=n;i++)
	{//由于遍历的顺序是从1~n,自然会满足最小序列的要求。
		for(int j=i+1;j<=n;j++){
			vector<int> lie,a(n+1,1);//n+1个数组元素的值都是1,即假设所有人都是好人
			a[i]=a[j]=-1;//值为1表示是好人,为-1表示是狼人,令i,j为狼人,接下来看是否符合假设
			for(int k=1;k<=n;k++)
				if(v[k]*a[abs(v[k])]<0)
					//自己说别人是狼人(v[k]<0),然而别人并不是狼人(a[abs(v[k])] == 1)
					//自己说别人不是狼人(v[k]>0),然而别人就是狼人(a[abs(v[k])] == -1)
					//即:v[k]*a[abs(v[k])] < 0
				  lie.push_back(k) ;   //如果有人说谎,压入lie中
			if(lie.size() ==2&&a[lie[0]]+a[lie[1]]==0) //两个说谎者且其中一个为狼人
			{                                       
				cout<<i<<" "<<j;
				return 0;//这里用于直接结束程序
			}
		}	
	}
	cout<<"No Solution";
	return 0;
}

B 1090 危险品装箱

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
 
int main()
{
	int n,m,a,b;
	map<int ,vector<int> > mp;//这一题和1065单身狗很类似,不同的是这里不是一对一,而是一对多,
	map<int ,int > mp1;
	while(~scanf("%d %d",&n,&m))
	{
		for(int i=0;i<n;++i)//初始给定的不相容对数条件
		{
			scanf("%d %d",&a,&b);
			mp[a].push_back(b);//每个物品都有个不相容队列
			mp[b].push_back(a);
		}
 
		for(int i=0;i<m;++i)
		{
			int f=0;
			mp1.clear();
			vector<int> ans;
			scanf("%d",&a);
			for(int j=0;j<a;++j)
			{
				scanf("%d",&b);
				if(mp.count(b)!=0)//如果b物品存在不相容队列
				{
					for(int k=0;k<mp[b].size();++k)
						mp1[mp[b][k]]=1;//将其不相容队列中的元素存到mp1[]数组中
					ans.push_back(b);//将b作为备检查物品,暂时存在ans中,以待后续检查
				}
			}
			for(int j=0;j<ans.size();++j)//将待检查物品全部拿出(即所有存在不相容性的物品)
				if(mp1.count(ans[j])!=0)//如果该物品在前面暂存所有不相容队列的mp[]中,该物品的不相容物品存在这一堆中
				{
					f=1;
					break;
				}
			printf("%s\n",f?"No":"Yes");
		}
	}
	return 0;
}

B 1091 N-自守数

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
#include<math.h>
using namespace std;

int main()
{
    int m,k;
    cin>>m;
    int i,j;
    for(i=0;i<m;i++)
    {
        cin>>k;
        for(j=1;j<10;j++)
        {
            int p=j*k*k;
            if(k>=100&&p%1000==k)
            {
                printf("%d %d\n",j,p);
                break;
            }
            else if(k>=10&&k<100&&p%100==k)
            {
                printf("%d %d\n",j,p);
                break;
            }
            else if(k<10&&p%10==k)
            {
                 printf("%d %d\n",j,p);
                 break;
            }
        }
        if(j==10)
            printf("No\n");
    }
	return 0;
}

B 1092 最好吃的月饼

#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
#include<math.h>
using namespace std;

int main()
{
    int n,m,temp;
    int p[1010]={0};
    cin>>n>>m;
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=1;j<=n;j++)
        {
            cin>>temp;
            p[j]+=temp;
        }
    }
    int maxn=0;
    for(i=1;i<=n;i++)
    {
        if(p[i]>maxn)
            maxn=p[i];
    }
    cout<<maxn<<endl;
    int a[1010];
    int k=0;
    for(i=1;i<=n;i++)
    {
        if(p[i]==maxn)
        {
            a[k++]=i;
        }
    }
    for(i=0;i<k-1;i++)
    {
        printf("%d ",a[i]);
    }
    printf("%d",a[i]);
	return 0;
}

B 1093 字符串A+B

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include<iostream>
using namespace std;
const int maxn=1000010;
char p[maxn],t[maxn];
int attend[maxn]={0};
int main()
{
	cin.getline(p,maxn);
	cin.getline(t,maxn);
    int lenp=strlen(p);
    int lent=strlen(t);
    for(int i=0;i<lenp;++i)
    {
        if(attend[p[i]]==0)
        {
            cout<<p[i];
            attend[p[i]]=1;
        }
    }
   for(int i=0;i<lent;++i)
    {
        if(attend[t[i]]==0)
        {
            cout<<t[i];
            attend[t[i]]=1;
        }
    }
	return 0;
}

B 1095 解码PAT准考证

这题算是最复杂的一题了,下面代码是我看的诸多答案中思路最清晰的一个了。
这一题,超时问题的解决方法是:
用unordered_map,会缩短查找时间
for循环里只能用printf和scanf进行输入输出

#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>    //用map测试点3会超时
#include <algorithm>

using namespace std;

struct student {
    string id, r, a, d, num;   //分别为准考证号,级别,考场编号,考试日期,考生编号
    int g;  // 分数
};

struct room_num {
    string a;//考场号
    int c;//考试人数
};

vector <student> stu;

bool cmp1(student x, student y) {
    if (x.g != y.g) {
        return x.g > y.g;
    }
    else {
        return x.id < y.id;
    }
}
bool cmp2(room_num x, room_num y) {
    if (x.c != y.c) {
        return x.c > y.c;
    }
    else {
        return x.a < y.a;
    }
}

void command1(string tmp, int &flag) {
    sort(stu.begin(), stu.end(), cmp1);
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].r == tmp) {
            flag = 1;
            printf("%s %d\n", stu[i].id.c_str(), stu[i].g);
        }
    }
}
void command2(string tmp, int &flag) {
    int c = 0, sum = 0;
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].a == tmp) {
            flag = 1;
            c++;
            sum += stu[i].g;
        }
    }
    if (flag) {
        printf("%d %d\n", c, sum);
    }
}
void command3(string tmp, int &flag) {
    unordered_map <string, int> mp;
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].d == tmp) {
            flag = 1;
            mp[stu[i].a]++;
        }
    }
    vector <room_num> rn;
    for (unordered_map <string, int> :: iterator it = mp.begin(); it != mp.end(); it++) {
        room_num tmp;
        tmp.a = it -> first;
        tmp.c = it -> second;
        rn.push_back(tmp);
    }
    sort(rn.begin(), rn.end(), cmp2);
    for (int i = 0; i < rn.size(); i++) {
        printf("%s %d\n", rn[i].a.c_str(), rn[i].c);
    }
}
int main() {
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        char s[14];
        student tmp;
        scanf("%s%d", s, &tmp.g);
        tmp.id = s;
        tmp.r = tmp.id.substr(0, 1);   //note
        tmp.a = tmp.id.substr(1, 3);
        tmp.d = tmp.id.substr(4, 6);
        tmp.num = tmp.id.substr(10, 3);
        stu.push_back(tmp);
    }
    for (int i = 0; i < M; i++) {
        int c, flag = 0;
        char s[7];
        string str;
        scanf("%d%s", &c, s);
        printf("Case %d: %d %s\n", i + 1, c, s);
        str = s;
        switch(c) {
            case 1:
                command1(str, flag); break;
            case 2:
                command2(str, flag); break;
            default:
                command3(str, flag); 
        }
        if (flag == 0) {
            printf("NA\n");
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值