20届 icoding 实验二

5-2: 24⼩时制-12⼩时制转换(24-HOUR TO 12-HOUR)

编写⼀个程序,要求⽤户输⼊ 24 ⼩时制的时间,将其转换为 12 ⼩时制的格式。

Enter a 24-hour time: 21:11
Equivalent 12-hour time: 9:11 PM

Enter a 24-hour time: 0:01
Equivalent 12-hour time: 12:01 AM

注意,不要把12:00显示为0:00。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int minuite, hour;
    printf("enter a 24-hour time:");
    scanf("%d:%d", &hour, &minuite);
    if (hour > 12) {
        hour -= 12;
        printf("%d:%.2dPM", hour, minuite);
    } else if (hour == 12)
        printf("12:%.2dPM", minuite);
    else if (hour == 0)
        printf("12:%.2dAM", minuite);
    else
        printf("%d:%.2dAM", hour, minuite);
    return 0;
}

5-4: ⻛速等级与描述(WIND SPEED)

下⾯是⽤于测量⻛⼒的蒲福⻛⼒等级的简化版,编写⼀个程序,要求⽤户输⼊⻛速(海⾥/⼩时),然后显示相应的描述。

速率(海⾥/⼩时)描述
⼩于 1Calm(⽆⻛)
1~3Light air(轻⻛)
4~27Breeze(微⻛)
28~47Gale(⼤⻛)
48~63Storm(暴⻛)
⼤于63Hurricane(飓⻛)

输出范例:Enter a wind speed: 1

Light air

Enter a wind speed: 27

Breeze

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x;
    printf("Enter a wind speed:");
    scanf("%d", &x);
    if (x < 1)
        printf("Calm");
    else if (x >= 1 && x <= 3)
        printf("Light air");
    else if (x >= 4 && x <= 27)
        printf("Breeze");
    else if (x >= 28 && x <= 47)
        printf("Gale");
    else if (x >= 48 && x <= 63)
        printf("Storm");
    else if (x > 63)
        printf("Hurricane");
    return 0;
}

5-6:通⽤产品代码(UPC, UNIVERSAL PRODUCTION CODE)

修改如下(4.1 节的UPC.C)程序,使其可以检测 UPC 的有效性。在⽤户输⼊UPC后,程序将输出 VALID 或 NOT VALID 。

输出范例:

Enter the first (single) digit: 0
Enter first group of five digits: 13800
Enter second group of five digits: 15173
Enter the last (single) digit: 5
VALID

Enter the first (single) digit: 0
Enter first group of five digits: 51500
Enter second group of five digits: 24128
Enter the last (single) digit: 7
NOT VALID

#include <stdio.h>

int main(void)
{
    int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total, x, y;

    printf("Enter the first (single) digit:");
    scanf("%1d", &d);

    printf("Enter the first group of five digits:");
    scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);

    printf("Enter the second group of five digits:");
    scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5);
    printf("Enter the last single digit:");
    scanf("%d", &y);
    first_sum = d + i2 + i4 + j1 + j3 + j5;
    second_sum = i1 + i3 + i5 + j2 + j4;
    total = 3 * first_sum + second_sum;
    x = 9 - ((total - 1) % 10);
    if (x == y)
        printf("VALID");
    else
        printf("NOT VALID");

    return 0;
}

5-10:将百分制转换为等级制 利⽤SWITCH语句编写⼀个程序,把⽤数字表示的成绩转化为字⺟表示的等级。 使⽤下⾯的等级评定规则:A为90100,B为8089,C为7079,D为069,F为0~59。如果成绩⾼于100或低于0显示出错信息。

输出范例:
Enter numerical grade: 84
Letter grade: B

Enter numerical grade: -1
Error, grade must be between 0 and 100.

提示:把成绩拆分成 2 个数字,然后使⽤ switch 语句判定⼗位上的数字

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int grade, x;
    printf("Enter numerical grade:");
    scanf("%d", &grade);
    if (grade < 0 || grade > 100)
        printf("Error,grade must be between 0and 100.");
    else {
        x = grade / 10 + 1;
        switch (x) {
        case 11:
        case 10:
            printf("A\n");
            break;
        case 9:
            printf("B\n");
            break;
        case 8:
            printf("C\n");
            break;
        case 7:
            printf("D\n");
            break;
        default:
            printf("F");
            break;
        }
    }
    return 0;
}

6-2:

最⼤公约数(GCD, GREATEST COMMON DIVISOR) 编写程序,要求⽤户输⼊两个整数,然后计算这两个整数的最⼤公约数(GCD, GREATEST COMMON DIVISOR)

输出范例:

Enter two integers: 12 28
Greatest common divisor: 4

Enter two integers:1 9
Greatest common divisor:1

提示:求最⼤公约数的经典算法 Euclid 算法如下:
分别让变量 m 和 n 存储两个整数。如果 n 为 0,那么停⽌操作,m 中的值是 GCD ;否则计算 m 除以 n 的余数,把 n 保存到 m 中,并把余数保存到 n 中;重复上述操作,每次都先判断n是否为 0 。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m, n, k;
    printf("enter two integers:");
    scanf("%d"  //:: 格式字符串与给定实参不匹配
          "%d",
        &m,
        &n);
    if (m < n) {
        k = m;
        m = n;
        n = k;
    } else if (n == 0) {
        m /= 2;
        printf("Greatest common divisor :%d", m);
        return 0;
    }

    while (n != 0) {
        k = m % n;
        m = n;
        n = k;
    }
    printf("Greatest common divisor :%d", m);
    return 0;
}

6-4:

股经纪⼈的佣⾦(BROKERAGE) 在下列程序( 5.2 节的 BROKER.C )中添加循环,以便⽤户可以输⼊多笔交易并且程序可以计算每次的佣⾦。程序在⽤户输⼊的交易额为 0 时终⽌。

输出范例:

Enter value of trade: 30000
Commission:$166.00

Enter value of trade: 20000
Commission:$144.00
Enter value of trade: 0

#include <stdio.h>

int main(void)
{
    float commission, value;

    printf("Enter value of trade: ");
    scanf("%f", &value);
    while (value != 0) {
        if (value < 2500.00f)
            commission = 30.00f + .017f * value;
        else if (value < 6250.00f)
            commission = 56.00f + .0066f * value;
        else if (value < 20000.00f)
            commission = 76.00f + .0034f * value;
        else if (value < 50000.00f)
            commission = 100.00f + .0022f * value;
        else if (value < 500000.00f)
            commission = 155.00f + .0011f * value;
        else
            commission = 255.00f + .0009f * value;

        if (commission < 39.00f)
            commission = 39.00f;

        printf("Commission: $%.2f\n\n", commission);
        scanf("%f", &value);
    }

    return 0;
}

6-6:

偶数平⽅(SQUARE OF EVEN NUMBER) 编写程序,提示⽤户输⼊⼀个数 N,然后显示出 1~N 的所有偶数的平⽅值。

输出范例:
Enter a number: 100
4
16
36
64
100

Enter a number: 50
4
16
36

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i, k;
    i = 1;
    printf("enter a number:");
    scanf("%d", &n);
    while (i <= n / 2) {
        k = 4 * i * i;
        printf("%d\n", k);
        i++;
    }
    return 0;
}

6-8:

日历(CALENDAR MONTH) 编写程序显示单⽉的⽇历。⽤户指定这个⽉的天数和该⽉起始⽇是星期⼏。

输出范例:

Enter number of days in month: 31

Enter starting day of the week(1=Sun, 7=Sat): 3

12345
6789101112
13141516171819
20212223242526
2728293031
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x, y, i = 1;
    printf("Enter number of days in month: ");
    scanf("%d", &x);
    printf("Enter starting day of the week(1=Sun, 7=Sat): ");
    scanf("%d", &y);
    printf("一  二	三	四	五	六	日\n");
    for (int n = 1; n < y; n++)
        printf("   ");
    while (i != x) {
        if (i < 10)
            printf("%d  ", i);
        else
            printf("%d ", i);
    }
    if (y % 7 == 0)
        printf("\n");
    i++;
    y++;

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值