第一题: 最少硬币问题(简单版)
? 假设有三种面值的硬币,分别为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语言 最少硬币问题内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!