C語言基礎應用處理學生打分 計算時間 最少硬幣問題詳細過程

第一題: 最少硬幣問題(簡單版)

💬 假設有三種面值的硬幣,分別為10、5、1。接收一個整數作為金額數,計算要達到該金額數,每個面值的硬幣最少需要多少枚。

輸出結果演示:

🔑 參考答案:

#include <stdio.h>
 
typedef struct StructrueMoneyBox {
    int n10;
    int n5;
    int n1;
} MoneyBox;
 
int main(void) {
    MoneyBox change = {0, 0, 0};
    int face_value[4] = {10, 5, 1};
    int money = 0;
    int i = 0;
 
    printf("請輸入金額: ");
    scanf("%d", &money);
 
    do {
        if (money < face_value[i]) {
            i++;
        }
        switch (i) {
            case 0: 
                change.n10 = money / face_value[i];
                break;
            case 1: 
                change.n5 = money / face_value[i];
                break;
            case 2: 
                change.n1 = money / face_value[i];
                break;
        }
        money = money % face_value[i];
    } while (money > 0);
 
    printf("10: %d\n", change.n10);
    printf("5: %d\n", change.n5);
    printf("1: %d\n", change.n1);
 
    return 0;
}

 🚩 運行結果:

第二題:計算已經過去瞭多久

編寫一個秒表程序來計算已經過去瞭多長時間。

利用下列結構體實現:

並要求使用下面兩個變量來解決問題:

當程序運行時,取當前時間並保存到 previtimePtr 變量中。

此後,程序將無限地接受命令,直到用戶輸入“quit” 程序才會停止。

如果用戶輸入“check”,則輸出存儲的上一個時間與當前時間的差。

(其他命令不采取任何操作)

提示:取現行時間的方法

#include <stdio.h>
#include <time.h>
 
int main(void) {
 
    time_t timer;
    struct tm *t;
 
    timer = time(NULL);
    t = localtime(&timer);
 
    return 0;
}

輸出結果演示:

🔑 參考答案:

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
 
typedef struct{
	int hours;
	int minutes;
	int seconds;
}Time;
 
int main(void){
	Time* prevTimePtr;
	Time currTime;
	time_t timer;
	struct tm *t;
	int i = 1, a, b, c;
	char buf[80];
 
	timer = time(NULL);
	t = localtime(&timer);
 
	prevTimePtr = (Time*)malloc(sizeof(Time));
	prevTimePtr->hours = t->tm_hour;
    prevTimePtr->minutes = t->tm_min;
    prevTimePtr->seconds = t->tm_sec;
 
	while(1){
		printf("system> ");
		fgets(buf, 80, stdin);
		if(!strcmp(buf, "quit\n")) break;
		
        if(!strcmp(buf, "check\n")){
			timer = time(NULL);
			t = localtime(&timer);
 
			currTime.hours = t->tm_hour;
		    currTime.minutes = t->tm_min;
		    currTime.seconds = t->tm_sec;
 
			printf("\n\tCheck Point #%02d\n", i++);
			printf("\tCurrent Time = %02d : %02d : %02d\n",
					currTime.hours, currTime.minutes, currTime.seconds);
			printf("\tPrevious Time = %02d : %02d : %02d\n",
					prevTimePtr->hours, prevTimePtr->minutes, prevTimePtr->seconds);
			
			c = currTime.seconds - prevTimePtr->seconds;
			b = currTime.minutes - prevTimePtr->minutes;
			a = currTime.hours - prevTimePtr->hours;
 
			if(c < 0){
                c+=60;
                b--;
            }
			if(b < 0){
                b+=60;
                a--;
            }
 
			printf("\tElasped Time = %02d : %02d : %02d\n\n", a,b,c);
 
			*prevTimePtr = currTime;
		}
	}
 
    return 0;
}

🚩 運行結果:

第三題:學生成績自動打分

💬 編寫一個管理學生成績,並帶有計算學生成績功能的小程序。

使用下面的結構來存儲學生信息:

例如,上述結構中一個學生的值如下:

Grades student1 = {2601053, "Natalie Lewis", {30.5, 65.9, 69.6}};

程序啟動時,在文本文件(“input.txt”)中讀取信息。(這裡請手動創建該文件!)

文件內容如下:

26
2601053 / Natalie Lewis / 30.5 / 65.9 / 69.6
2026018 / Chloe Christian / 54.1 / 43.4 / 35.4
2016317 / Emolys Evans / 31.6 / 20.4 / 26.3
2961329 / Sophia Allen / 63.5 / 37.9 / 56.8
2016787 / Robert Melton / 44.1 / 54.2 / 32.7
2011661 / Siaomos Barton / 92.2 / 15.4 / 33.7
8166559 / Sebastian Henderson / 55.1 / 38.2 / 0.9
2061185 / Richard Cerney / 63.6 / 98.6 / 97.0
2017454 / Elizabeth Calvin / 7.4 / 0.0 / 26.4
2026196 / Madison Frost / 49.3 / 64.3 / 73.3
2015758 / Emily Stevgenson / 51.6 / 89.2 / 34.0
2016776 / Navy Taylor / 8.6 / 36.4 / 52.3
2062103 / Vincent Newton / 38.7 / 79.0 / 24.0
2501762 / Owen Ingram / 40.6 / 85.1 / 91.1
2042142 / Olyen Massey / 72.1 / 62.0 / 97.3
2027039 / Emma Thomas / 30.1 / 25.1 / 27.3
2051341 / Peter Pitts / 47.9 / 71.8 / 74.9
2071368 / Xygret Latimer / 67.2 / 9.7 / 79.1
2023762 / Michael Grahan / 6.4 / 29.0 / 17.5
2024545 / Mandon Austin / 98.8 / 28.5 / 48.9
2091176 / Violet Smith / 4.8 / 93.9 / 3.9
2802841 / Samuel Davis / 95.6 / 54.2 / 31.8
2027062 / Sam Mackey / 83.6 / 26.1 / 97.5
2061555 / Miguel Parr / 77.2 / 88.1 / 99.5
2028836 / Oscar Hamersley / 67.7 / 43.4 / 91.7
2026582 / MeanSon King / 97.2 / 88.3 / 100.0

文本的第一行顯示瞭學生人數 n

從第二行開始,按照“id/name/mid/final/proj”的順序給出瞭學生的信息。

通過根據所有學生的成績計算排名來確定學生的最終成績:

① 總分評判規則: total_point = mid * 0.3 + final * 0.3 + prog * 0

② 評分評判規則: 成績前 30% 為 A,前 70% 為 B,其餘為 C

③ 總分小於 30(total_point < 30) 的學生,無視排名給 F。

輸入的成績范圍限制在 0~100分,學生姓名長度限制在40個字符以內。

僅對給定的文本文件內的學生進行評分,輸出文件的位置隨意。

輸出效果演示:

🔑 參考答案:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct{
    float mid;
    float final;
    float proj;
}Points;
 
typedef struct{
    int studentId;
    char name[40];
    Points subject;
    char grade[3];
    float total_point;
}Grades;
 
int main(void){
    int cnt, i, j, A_temp, B_temp;
    float a_grade, b_grade;
    char name_temp[30];
    Grades* grade;
    Grades gtemp;
    FILE *fp = fopen("input.txt", "r");
    if(fp == NULL){
        printf("未找到文件!\n");
        return 1;
    }
 
    fscanf(fp, "%d", &cnt);
 
    A_temp = cnt * 0.3;
    B_temp = cnt * 0.7;
 
    grade = (Grades*)malloc(sizeof(Grades) * cnt);
 
    for(i = 0; i < cnt; i++){
        fscanf(fp, "%d / %s %s / %f / %f / %f\n", &grade[i].studentId, grade[i].name, name_temp, &((grade[i].subject).mid), &(grade[i].subject).final, &(grade[i].subject).proj);
        strcat(grade[i].name, " ");
        strcat(grade[i].name, name_temp);
        grade[i].total_point = (grade[i].subject).mid * 0.3 + (grade[i].subject).final * 0.3 + (grade[i].subject).proj * 0.4;
    }
 
    for(i = 0; i < cnt - 1; i++){
        for(j = 0; j < cnt - i - 1; j++){
            if(grade[j].total_point < grade[j + 1].total_point){
                gtemp = grade[j];
                grade[j] = grade[j + 1];
                grade[j + 1] = gtemp;
            }
        }
    }
 
    a_grade = grade[A_temp].total_point;
    b_grade = grade[B_temp].total_point;
 
    printf("Cut off for A grade: %f (rank %d)\n", a_grade, A_temp);
    printf("Cut off for B grade: %f (rank %d)\n", b_grade, B_temp);
 
    for(i = 0; i < cnt; i++){
        if(grade[i].total_point < 30){
            strcpy(grade[i].grade, "F");
        }
        else if(a_grade <= grade[i].total_point){
            strcpy(grade[i].grade, "A");
        }
        else if(b_grade <= grade[i].total_point && grade[i].total_point < a_grade){
            strcpy(grade[i].grade, "B");
        }
        else if(grade[i].total_point < b_grade){
            strcpy(grade[i].grade, "C");
        }
    }
 
    printf("\n-- Student List --\n");
    for(i = 0; i < cnt; i++){
        printf("    Id : %d\n", grade[i].studentId);
        printf("    Name : %s\n", grade[i].name);
        printf("    Grade(mid) : %.02f\n", (grade[i].subject).mid);
        printf("    Grade(final) : %.02f\n", (grade[i].subject).final);
        printf("    Grade(project) : %.02f\n", (grade[i].subject).proj);
        printf("    Grade : %s (%.02f, rank %d)\n", grade[i].grade, grade[i].total_point, i + 1);
        printf("\n");
    }
 
    return 0;
}

🚩 運行結果:

Cut off for A grade: 65.870003 (rank 7)
Cut off for B grade: 42.570000 (rank 18)

— Student List —
Id : 2026582
Name : MeanSon King
Grade(mid) : 97.20
Grade(final) : 88.30
Grade(project) : 100.00
Grade : A (95.65, rank 1)

Id : 2061555
Name : Miguel Parr
Grade(mid) : 77.20
Grade(final) : 88.10
Grade(project) : 99.50
Grade : A (89.39, rank 2)

Id : 2061185
Name : Richard Cerney
Grade(mid) : 63.60
Grade(final) : 98.60
Grade(project) : 97.00
Grade : A (87.46, rank 3)

Id : 2042142
Name : Olyen Massey
Grade(mid) : 72.10
Grade(final) : 62.00
Grade(project) : 97.30
Grade : A (79.15, rank 4)

Id : 2501762
Name : Owen Ingram
Grade(mid) : 40.60
Grade(final) : 85.10
Grade(project) : 91.10
Grade : A (74.15, rank 5)

Id : 2027062
Name : Sam Mackey
Grade(mid) : 83.60
Grade(final) : 26.10
Grade(project) : 97.50
Grade : A (71.91, rank 6)

Id : 2028836
Name : Oscar Hamersley
Grade(mid) : 67.70
Grade(final) : 43.40
Grade(project) : 91.70
Grade : A (70.01, rank 7)

Id : 2051341
Name : Peter Pitts
Grade(mid) : 47.90
Grade(final) : 71.80
Grade(project) : 74.90
Grade : A (65.87, rank 8)

Id : 2026196
Name : Madison Frost
Grade(mid) : 49.30
Grade(final) : 64.30
Grade(project) : 73.30
Grade : B (63.40, rank 9)

Id : 2024545
Name : Mandon Austin
Grade(mid) : 98.80
Grade(final) : 28.50
Grade(project) : 48.90
Grade : B (57.75, rank 10)

Id : 2802841
Name : Samuel Davis
Grade(mid) : 95.60
Grade(final) : 54.20
Grade(project) : 31.80
Grade : B (57.66, rank 11)

Id : 2601053
Name : Natalie Lewis
Grade(mid) : 30.50
Grade(final) : 65.90
Grade(project) : 69.60
Grade : B (56.76, rank 12)

Id : 2015758
Name : Emily Stevgenson
Grade(mid) : 51.60
Grade(final) : 89.20
Grade(project) : 34.00
Grade : B (55.84, rank 13)

Id : 2071368
Name : Xygret Latimer
Grade(mid) : 67.20
Grade(final) : 9.70
Grade(project) : 79.10
Grade : B (54.71, rank 14)

Id : 2961329
Name : Sophia Allen
Grade(mid) : 63.50
Grade(final) : 37.90
Grade(project) : 56.80
Grade : B (53.14, rank 15)

Id : 2011661
Name : Siaomos Barton
Grade(mid) : 92.20
Grade(final) : 15.40
Grade(project) : 33.70
Grade : B (45.76, rank 16)

Id : 2062103
Name : Vincent Newton
Grade(mid) : 38.70
Grade(final) : 79.00
Grade(project) : 24.00
Grade : B (44.91, rank 17)

Id : 2026018
Name : Chloe Christian
Grade(mid) : 54.10
Grade(final) : 43.40
Grade(project) : 35.40
Grade : B (43.41, rank 18)

Id : 2016787
Name : Robert Melton
Grade(mid) : 44.10
Grade(final) : 54.20
Grade(project) : 32.70
Grade : B (42.57, rank 19)

Id : 2016776
Name : Navy Taylor
Grade(mid) : 8.60
Grade(final) : 36.40
Grade(project) : 52.30
Grade : C (34.42, rank 20)

Id : 2091176
Name : Violet Smith
Grade(mid) : 4.80
Grade(final) : 93.90
Grade(project) : 3.90
Grade : C (31.17, rank 21)

Id : 8166559
Name : Sebastian Henderson
Grade(mid) : 55.10
Grade(final) : 38.20
Grade(project) : 0.90
Grade : F (28.35, rank 22)

Id : 2027039
Name : Emma Thomas
Grade(mid) : 30.10
Grade(final) : 25.10
Grade(project) : 27.30
Grade : F (27.48, rank 23)

Id : 2016317
Name : Emolys Evans
Grade(mid) : 31.60
Grade(final) : 20.40
Grade(project) : 26.30
Grade : F (26.12, rank 24)

Id : 2023762
Name : Michael Grahan
Grade(mid) : 6.40
Grade(final) : 29.00
Grade(project) : 17.50
Grade : F (17.62, rank 25)

Id : 2017454
Name : Elizabeth Calvin
Grade(mid) : 7.40
Grade(final) : 0.00
Grade(project) : 26.40
Grade : F (12.78, rank 26)

參考資料:

Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

百度百科[EB/OL]. []. https://baike.baidu.com/.

📌 筆者:王亦優

📃 更新: 2021.12.14

❌ 勘誤: 無

📜 聲明: 由於作者水平有限,本文有錯誤和不準確之處在所難免,本人也很想知道這些錯誤,懇望讀者批評指正

到此這篇關於C語言應用解決最少硬幣問題詳細過程的文章就介紹到這瞭,更多相關C語言 最少硬幣問題內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: