首页 > 代码库 > 附加作业2

附加作业2

1. 作业题目:比较大小

    代码:

#include<stdio.h>
int main()
{
    int i,j,a[10],index,n,temp;
    printf("Enter n:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n-1;i++){
        index=i;
    for(j=i+1;j<n;j++)
        if(a[j]<a[i])
        index=j;
        temp=a[index];
        a[index]=a[i];
        a[i]=temp;
    }
    printf("After sorted:");
    for(i=0;i<n;i++){
        printf("%5d",a[i]);
    }
    printf("\n");
    return 0;
} 

测试截图:技术分享

2.作业题目:高速公路超速处罚

     代码:

#include<stdio.h>
int main()
{
    int number,i;
    printf("高速公路本车道限速:");
    scanf("%4d",&number);
    printf("\n");
    printf("车速:");
    scanf("%4d",&i);
    printf("\n");
    if(i<1.1*number){
        printf("正常!\n");
    }
    else if(i>1.1*number && i<1.5*number){
        printf("200元罚款!\n");
    }
    else{
        printf("吊销驾驶证!\n");
    }
    printf("\n");
    return 0;
} 

测试截图:技术分享

4.作业题目:统计学生成绩

     代码:

#include<stdio.h>
int main(void)
{
    int i,n;
    int A,B,C,D,E;
    A=B=C=D=E=0;
    double grade,total=0;
    printf("输入学生个数n:");
    scanf("%d",&n);

    double total_A = 0;
    double total_B = 0;
    double total_C = 0;
    double total_D = 0;
    double total_E = 0;
    
    for(i=1;i<=n;i++){
        printf("输入学生个数#%d:",i);
        scanf("%lf",&grade);
        total = total + grade;
        int switch_grade = grade / 10;
        switch(switch_grade){
            case 10:
            case 9:
            {
                printf("90~100\n");
                A++;
                total_A = total_A + grade;
                break;
            }
            case 8:
            {
                printf("80~89\n");
                B++;
                total_B = total_B + grade;
                break;
            }
            case 7:
            {
                printf("70~79\n");
                C++;
                total_C = total_C + grade;
                break;
            }
            case 6:
            {
                printf("60~69\n");
                D++;
                total_D = total_D + grade;
                break;
            }
            default:
            {
                printf("ERROR\n");
                E++;
                total_E = total_E + grade;
                break;
            }
        }
    }
    printf("所有学生的总成绩=%.2f\n",total);
    printf("每个等级的人数:\n  A-%d\n  B-%d\n  C-%d\n  D-%d\n  E-%d\n",A,B,C,D,E);
    return 0;
}

测试截图:技术分享

总结:

这一次有不会的,还需多加练习。

以前学过的还是要好好复习啊,切记/(ㄒoㄒ)/~~

附加作业2