美文网首页算法笔记学习笔记
《算法笔记》3.1小节——入门模拟->简单模拟

《算法笔记》3.1小节——入门模拟->简单模拟

作者: 木子李_0961 | 来源:发表于2020-04-07 11:18 被阅读0次

    @[TOC]

    Contest100000575 - 《算法笔记》3.1小节——入门模拟->简单模拟

    1814 Problem A 剩下的树

    来自 http://codeup.cn/contest.php?cid=100000575

    
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int tree[10005] = {0};
    
    int main()
    {
        int L,M;
        while(scanf("%d%d",&L, &M) != EOF)
        {
            if(L==0&&M==0)
                return 0;
            else
            {
                for(int i=0;i<=L;i++)
                {
                    tree[i] = 1;
                }
                int sum = L+1;
                while(M--)
                {
                    int left,right;
                    scanf("%d%d",&left, &right);
                    for(int i=left;i<=right;i++)
                    {
                        tree[i] = 0;
                    }
                }
                for(int j=0;j<=L;j++)
                {
                    if(tree[j] == 0)
                        sum--;
                }
                printf("%d\n",sum);
            }
        }
        return 0;   
    } 
    

    1817 Problem B A+B

    来自 http://codeup.cn/contest.php?cid=100000575

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    using namespace std;
    
    char A[15],B[15];
    long trans(char *arr,int length)
    {
        long num=0,j=1;
        for(int i=length-1;i>=0;i--)
        {
            if(arr[i]>='0' && arr[i]<='9')
            {
                num = num + (arr[i] - '0')*j;
                j*=10;
            }
        }
        if(arr[0]=='-')
            num = -num;
        return num;
    }
    int main()
    {
        long a,b;
        while(scanf("%s%s",A, B) != EOF)
        {
            int lenA = strlen(A);
            int lenB = strlen(B);
            a = trans(A,lenA);
            b = trans(B,lenB);
            printf("%ld\n",a+b);
        }
        return 0;   
    } 
    

    1906 Problem C 特殊乘法

    来自 http://codeup.cn/contest.php?cid=100000575

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    using namespace std;
    char a[15],b[15];
    int num1[15],num2[15];
    void trans(char *arr,int *num)
    {
        int len = strlen(arr);
        for(int i=0;i<len;i++)
        {
            num[i] = arr[i] - '0';
        }
    }
    int main()
    {
        while(scanf("%s%s",a, b) != EOF)
        {
            int mul=0;
            trans(a,num1);
            trans(b,num2);
            int lena = strlen(a);
            int lenb = strlen(b);
            for(int i=0;i<lena;i++)
            for(int j=0;j<lenb;j++)
            {
                mul += (num1[i]*num2[j]);
            }
            printf("%d\n",mul);
        }
        return 0;   
    } 
    
    
    

    2036 Problem D 比较奇偶数个数

    来自 http://codeup.cn/contest.php?cid=100000575

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            int  num;
            int countodd=0;
            int counteven=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d",&num);
                if(num % 2 == 0)
                {
                    counteven++;
                }
                else
                {
                    countodd++;
                }
            }
            if(counteven > countodd)
            {
                printf("%s\n","NO");
            }
            else
                printf("%s\n","YES");
        }
        return 0;   
    } 
    
    

    6116 Problem E Shortest Distance (20)

    来自 http://codeup.cn/contest.php?cid=100000575

    6128 Problem F A+B和C (15)

    来自 http://codeup.cn/contest.php?cid=100000575

    
    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        int T;
        scanf("%d",&T);
        int num=1;
        while(T--)
        {
            long long a,b,c;
            scanf("%ld%ld%ld",&a, &b, &c);
            
            if(a + b > c)
            {
                printf("Case #%d: true\n",num); 
            }
            else
            {
                printf("Case #%d: false\n",num); 
            } 
            num++;
        }
        return 0;   
    } 
    
    
    

    6129 Problem G 数字分类 (20)

    来自 http://codeup.cn/contest.php?cid=100000575

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    #include <cmath>
    using namespace std;
    
    int A[5][1005];
    int num[1005];
    int main()
    {
        int N;
        while(scanf("%d",&N) != EOF)
        {
            int A1_even=0,A2_jiaocuo=0,A3_count=0,A4_count=0,A5_max=0;
        //  int A3_count = 0,A4_count = 0;
            float A4_avg = 0;
            float A4_sum = 0;
            int flag=0;
            int A2_exist = 0;
            for(int i=0;i<N;i++)
            {
                int num;
                cin>>num;
                switch(num%5)//switch语句正合适 
                {
                    case 0:
                        if(num % 2==0)
                        {
                            A1_even += num;
                        }
                        break;
                    case 1:
                        A2_jiaocuo+=(num * pow((double)(-1),flag));
                        flag++;
                        A2_exist = 1;
                        break;
                    case 2:
                        A3_count++;
                        break;
                    case 3:
                        A4_count++;
                        A4_sum += num;
                        break;
                    case 4:
                        if(A5_max < num)
                            A5_max = num;
                        break;
                    default:
                        break;  
                }
            //  cout<<flag<<endl;
            }
            A4_avg = A4_sum / A4_count;
            
            if(A1_even != 0)
                printf("%d ",A1_even);
            else
            {
                printf("N ");
            }
        //********************************A2大坑  
            if(A2_exist ==1)
            {
                printf("%d ",A2_jiaocuo);
            }   
            else
            {
                printf("N ");
            }
            
            if(A3_count != 0)
                printf("%d ",A3_count);
            else
            {
                printf("N ");
            }
            
            if(A4_count != 0)
                printf("%0.1lf ",A4_avg);
            else
            {
                printf("N ");
            }
            //行末不得有多余空格 
            if(A5_max != 0)
                printf("%d",A5_max);
            else
            {
                printf("N");
            }
            //cout<<endl;
            printf("\n");
        }
        return 0;   
    } 
    /*
    A1~A5里有些并不是看他们本身是否为零
    而是看符合他们要求的数字是否存在
    比如A2就是看是否有被5除后余1的数字,如果有,那么无论A2最后的结果如何,都是输出A2本身的值而不是N
    
    */
    
    

    6170 Problem H 部分A+B (15)

    来自 链接: [link] http://codeup.cn/contest.php?cid=100000575

       #include <iostream>
        #include <stdlib.h>
        #include <cstring>
        using namespace std;
        char A[15],B[15];
        //法一竟然GG,不晓得要闹哪样
        /* 
        int main()
        {
            char a,b;
            while(scanf("%s%s%s%s",&A, &a, &B, &b) != EOF)
            {
                int lena = strlen(A);
                int lenb = strlen(B);
                int numa=0,numb=0;
                int weight = 1,weightb = 1;
                for(int i=0;i<lena;i++)
                {
                    if(A[i] == a)
                    {
                        numa = numa * 10 + (a - '0');
                    }
                }
                for(int j=0;j<lenb;j++)
                {
                    if(B[j] == b)
                    {
                        numb = numb * 10 + (b - '0'); 
                    }
                }
                printf("%d\n",numa+numb);
            }
            return 0;   
        } 
        */
        //就简单模拟,字符串遍历,匹配相应的数,按照位数累积即可
        int main()
        {
            int a,b;
            while(scanf("%s%d%s%d",&A, &a, &B, &b) != EOF)
            {
                int lena = strlen(A);
                int lenb = strlen(B);
                int numa=0,numb=0;
                int weight = 1,weightb = 1;
                for(int i=0;i<lena;i++)
                {
                    if((A[i]-'0') == a)
                    {
                        numa = numa * 10 + a;
                    }
                }
                for(int j=0;j<lenb;j++)
                {
                    if((B[j]-'0') == b)
                    {
                        numb = numb * 10 + b; 
                    }
                }
                printf("%d\n",numa+numb);
            }
            return 0;   
        }  
    

    6172 Problem I 锤子剪刀布 (20)

    来自 http://codeup.cn/contest.php?cid=100000575

    //题目较为繁琐,根据题意模拟
    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    /*
    思路:每次输入进行比较。甲负的次数就是乙赢的次数,不用额外记录。最后输出甲乙获胜最多的手势,因为要考虑解不唯一,所以我采用把结果枚举。按字典序,J次数必须大于B和C,C次数必须大于B,可以大于等于B,B大于等于B、J就行。
    
    注意:scanf会把'\n'读入,所以可能输入五组数据,就跳出结果了,要用getchar()来吸收。另外,判断要用if-else,不能用多个if,而没有else,这样会记录次数出现错误。
    --------------------- 
    作者:Wonz5130 
    来源:CSDN 
    原文:https://blog.csdn.net/Wonz5130/article/details/79844683 
    版权声明:本文为博主原创文章,转载请附上博文链接!
    */
    using namespace std;
    
    int main()
    {
        int jiasu=0,jiafa=0,jiaeq=0;
        int jiac=0,jiaj=0,jiab=0,yic=0,yij=0,yib=0;
        int N;
        scanf("%d",&N);
        while(N--)
        {
            getchar();
            char a,b;
            scanf("%c %c",&a,&b);
            
                if(a == b)
                    jiaeq++;
                //甲赢 
                else if(a == 'C' && b =='J')
                {
                    jiasu++;
                    jiac++;
                }
                else if(a == 'J' && b=='B')
                {
                    jiasu++;
                    jiaj++;
                }
                else if(a == 'B' && b=='C')
                {
                    jiasu++;
                    jiab++;
                }
                //乙赢 
                else if(a == 'J' && b=='C')
                {
                    jiafa++;
                    yic++;
                }
                else if(a == 'B' && b=='J')
                {
                    jiafa++;
                    yij++;
                }
                else if(a == 'C' && b=='B')
                {
                    jiafa++;
                    yib++;
                }
            //  cout<<jiasu<<endl;
        }
        printf("%d %d %d\n",jiasu,jiaeq,jiafa);
        printf("%d %d %d\n",jiafa,jiaeq,jiasu);
        //会有多种情况,判断字母序输出
        if(jiaj > jiab && jiaj > jiac)
        {
            printf("%c ",'J');
        }
        else if(jiac>jiab)
        {
            printf("%c ",'C');
        }
        else
        {
            printf("%c ",'B');
        }
        if(yij > yib && yij > yic)
        {
            printf("%c ",'J');
        }
        else if(yic>yib)
        {
            printf("%c ",'C');
        }
        else
        {
            printf("%c ",'B');
        }
        printf("\n");
        return 0;   
    } 
    

    错误记录:
    1.关于字符的控制台输入回车直接算一行了,截图就是本应输入10个数,没有getchar()只输入5个书

        scanf("%d",&N);
        getchar();
        char a,b;
        scanf("%c %c",&a,&b);
    
    错误记录1

    2.格式化输入输出没有搞清楚

    printf("%d %d %d\n",&jiasu, &jiaeq, &jiafa);
    printf("%d %d %d\n",&jiafa, &jiaeq, &jiasu);
    

    出错截图:


    出错截图2

    相关文章

      网友评论

        本文标题:《算法笔记》3.1小节——入门模拟->简单模拟

        本文链接:https://www.haomeiwen.com/subject/ozygphtx.html