算法课堂 - 第一次作业

题目过于简单就不写分析了

第一题

#include <stdio.h>
#include <stdlib.h>
int main()
{
    long a = 0, b = 0;//注意精度问题这在ACM中通常是个坑点
    while (~scanf_s("%ld %ld", &a, &b))
    {
        printf("%ld", a + b);   //虽然这里可以压行但是刚开始要养成好习惯!!
    }
    system("pause");
    return 0;
}

第二题

#include <stdio.h>
#include <stdlib.h>
int a[100][100] = { 0 };
int main()
{
    int x = 0;a[0][1] = 1;//初始化
    while (~scanf_s("%d", &x))
    {
        for (int i = 1; i <= x; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }
    }
    system("pasue");
    return 0;
}

第三题

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d*%d=%d\t", i, j, i*j);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

第四题

#include <stdio.h>
#include <stdlib.h>
int func(int n)    //函数实例
{
    int ans = 0;
    while (n != 1)
    {
        if (n % 2 == 0)
        {
            n = n / 2;
            ans++;
        }else{
            n = (3 * n + 1) / 2;
            ans++;
        }
    }
    return ans;
}
int main()
{
    //使用函数可以使main函数中代码不出现冗余,各个模块分工明确减少错误。
    //增强了代码的可读性和可维护性
    int n = 0;
    scanf_s("%d", &n);
    printf("%d\n", func(n));
    system("pause");
    return 0;
}

第五题

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char s[4];
    gets_s(s);
    for (int i = 0; i < 3; i++)
    {
        if (s[i] <= '9' && s[i] >= '1')
        {
            for (int j = 1; j <= s[i] - '0'; j++)
            {
                if (i == 0)printf("B");        //忍不住压了行 为了美观
                if (i == 1)printf("S");
                if (i == 2)printf("G");
            }
        }
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/11748423.html
今日推荐