返回顶部

收藏

一个简单的过年小孩放炮问题解法(C语言)

更多
#include <stdio.h>
#include <string.h>
#define NUM 100
int main()
{
    int a = NUM;
    int b = NUM;
    int c = NUM;
    int second = 0;
    int count = 0;
    int flag = 0;

    while(a > 0 || b > 0 || c > 0)
    {
        second++;
        if(1 == second)
        {
            a--;
            b--;
            c--;
            flag = 1;
            printf("a consume one in second %d\n", second);
            printf("b consume one in second %d\n", second);
            printf("c consume one in second %d\n", second);
            count++;
            printf("count = %d\n", count);
        }
        else
        {
            if((second - 1) % 1 == 0 && a > 0)
            {
                flag = 1;
                a--;
                printf("a consume one in second %d\n", second);
            }
            if((second - 1) % 2 == 0 && b > 0)
            {
                flag = 1;
                b--;
                printf("b consume one in second %d\n", second);
            }
            if((second - 1) % 3 == 0 && c > 0)
            {
                flag = 1;
                c--;
                printf("c consume one in second %d\n", second);
            }
            if(flag)
            {
                flag = 0;
                count++;
                printf("count = %d\n", count);
            }
        }

    }
    printf("sum timer : %d\n", second);
    printf("sum count : %d\n", count);
    return 0;
}

标签:放炮,算法,C语言

收藏

0人收藏

支持

0

反对

0

发表评论