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

@[TOC]

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

1814 Problem A 剩下的树

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


#include 
#include 
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 
#include 
#include 
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 
#include 
#include 
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

2036 Problem D 比较奇偶数个数

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

#include 
#include 
#include 
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 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 
#include 
#include 
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 
#include 
#include 
#include 
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>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<

6170 Problem H 部分A+B (15)

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

   #include 
    #include 
    #include 
    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

6172 Problem I 锤子剪刀布 (20)

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

//题目较为繁琐,根据题意模拟
#include 
#include 
#include 
/*
思路:每次输入进行比较。甲负的次数就是乙赢的次数,不用额外记录。最后输出甲乙获胜最多的手势,因为要考虑解不唯一,所以我采用把结果枚举。按字典序,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< 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小节——入门模拟->简单模拟)