哈工大C语言程序设计精髓第三周

由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。

第3周编程题在线测试

1. 学分绩计算


题目内容
已知某大学期末考试学分绩的计算公式为:学分绩 =(工科数学 * 5 + 英语 * 1.5 + 线性代数 * 3.5) / 10
请编程从键盘按顺序输入某学生的工科数学、英语和线性代数成绩,计算并输出其学分绩。
以下为程序的运行结果示例:
Input math1, English and math2: 80,70,100↙
Final score = 85.50
输入提示信息:“Input math1, English and math2:”
输入格式: “%d,%d,%d”
输出格式:“Final score = %.2f\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int math1, English, math2;
    printf("Input math1, English and math2:");
    scanf("%d,%d,%d", &math1, &English, &math2);
    printf("Final score = %.2f\n", (math1*5+English*1.5+math2*3.5)/10);
    return 0;
}

2. 一尺之捶,日取其半


题目内容
我国古代著作《庄子》中记载道:“一尺之捶,日取其半,万世不竭”。其含义是:对于一尺的东西,今天取其一半,明天取其一半的一半,后天再取其一半的一半的一半总有一半留下,所以永远也取不尽。请编写一个程序,使其可以计算出一条长为m的绳子,在n天之后剩下的长度。
运行结果示例1:
Input length and days: 12,5↙
length=0.37500
运行结果示例2:
Input length and days: 57.6,7↙
length=0.45000
输入提示信息:“Input length and days:”
输入格式: “%f,%d”
输出格式:“length=%.5f\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int  days;
    float length;
    printf("Input length and days:");
    scanf("%f,%d", &length, &days);
    length =  pow(0.5, days) * length;
    printf("length=%.5f\n", length);
    return 0;
}

3. 网购打折商品V1.0


题目内容
某网上购物网站对用户实行优惠,买家购物货款p越多,则折扣越多。今天正值该网站优惠折扣日,买家可以获得8%的折扣。请编程从键盘输入买家购物货款p,计算并输出买家折扣后实际应付出的价钱。
:程序中的数据类型为float。
程序的运行结果示例1:
Input payment p: 300↙
price = 276.0
程序的运行结果示例2:
Input payment p: 1299.8↙
price = 1195.8
输入提示信息:“Input payment p:”
输入格式: “%f”
输出格式:“price = %.1f\n” (注:等号左右均有空格)

代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    float  p, price;
    printf("Input payment p:");
    scanf("%f", &p);
    price = 0.92 * p;
    printf("price = %.1f\n", price);
    return 0;
}

4. 计算时间差V1.0


题目内容
编程从键盘任意输入两个时间(例如4时55分和1时25分),计算并输出这两个时间之间的间隔。要求不输出时间差的负号。
程序的运行结果示例1:
Input time one(hour, minute): 4,55↙
Input time two(hour, minute): 1,25↙
3 hour 30 minute
程序的运行结果示例2:
Input time one(hour, minute): 1,56↙
Input time two(hour, minute): 3,25↙
1 hour 29 minute
输入提示信息:“Input time one(hour, minute):”
“Input time two(hour, minute):”
输入格式:"%d,%d"
输出格式:"%d hour %d minute\n"

代码实现:

#include <stdio.h>
int main()
{
	int h1,h2,m1,m2,s,t1,t2;
    printf("Input time one(hour, minute):");
    scanf("%d,%d",&h1,&m1);
    printf("Input time two(hour, minute):");
    scanf("%d,%d",&h2,&m2);
    s=(h1*60+m1)-(h2*60+m2);
    if(s<0)    
    	s=-s;
    t1=s/60;
    t2=s%60;
    printf("%d hour %d minute\n",t1,t2);
	return 0;
 }

第三周练兵区——编程题

1. 日期显示


题目内容
编写一个程序, 接收用户录入的日期信息并且将其显示出来. 其中, 输入日期的形式为月/日/年(mm/dd/yy), 输出日期的形式为年月日(yy.mm.dd)。
以下为程序的运行结果示例:
Enter a date (mm/dd/yy):
12/03/2015↙
You entered the date: 2015.12.03
输入格式: “%d/%d/%d”
输出格式:
输入提示信息:“Enter a date (mm/dd/yy):\n”
输出格式:“You entered the date: %04d.%02d.%02d\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int mm, dd, yy ;
    printf("Enter a date (mm/dd/yy):\n");
    scanf("%d/%d/%d", &mm, &dd, &yy);
    printf("You entered the date: %04d.%02d.%02d\n", yy, mm, dd);
    return 0;
}

2. 产品信息格式化


题目内容
编写一个程序, 对用户录入的产品信息进行格式化。
以下为程序的运行结果示例:
Enter item number:
385↙
Enter unit price:
12.5↙
Enter purchase date (mm/dd/yy):
12/03/2015↙
Item Unit Purchase
385 $ 12.50 12032015
输入格式:
产品编号输入格式:"%d"
产品价格输入格式:"%f"
购买日期输入格式:"%d/%d/%d"
输出格式:
产品编号输入提示信息:“Enter item number:\n”
产品价格输入提示信息:“Enter unit price:\n”
购买日期输入提示信息:“Enter purchase date (mm/dd/yy):\n”
格式化输出的表头信息:“Item Unit Purchase\n”
输出格式:"%-9d$ %-9.2f%02d%02d%04d\n"

代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int number, date, mm, dd, yy;
    float price;
    printf("Enter item number:\n");
    scanf("%d", &number);
    printf("Enter unit price:\n");
    scanf("%f", &price);
    printf("Enter purchase date (mm/dd/yy):\n");
    scanf("%d/%d/%d", &mm, &dd, &yy);
    printf("Item      Unit     Purchase\n");
    printf("%-9d$ %-9.2f%02d%02d%04d\n", number, price, mm, dd, yy);
    return 0;
}

3. 计算两个数的平方和


题目内容
从键盘读入两个实数,编程计算并输出它们的平方和,要求使用数学函数pow(x,y)计算平方值,输出结果保留2位小数。 程序中所有浮点数的数据类型均为float。
提示:使用数学函数需要在程序中加入编译预处理命令 #include <math.h>
以下为程序的运行结果示例:
Please input x and y:
1.2,3.4↙
Result=13.00
输入格式: “%f,%f”
输出格式:
输入提示信息:“Please input x and y:\n”
输出格式:“Result=%.2f\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    float x, y, c1, c2, c;
    printf("Please input x and y:\n");
    scanf("%f,%f", &x, &y);
    c1 = pow(x, 2);
    c2 = pow(y, 2);
    c = c1 +c 2;
    printf("Result=%.2f\n", c);
    return 0;
}

4. 逆序数的拆分计算


题目内容
从键盘输入一个4位数的整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-1234,忽略负号,由1234分离出其千位1、百位2、十位3、个位4,然后计算4 1000+3100+2*10+1 = 4321,并输出4321。再将得到的逆序数4321拆分为两个2位数的正整数43和21,计算并输出拆分后的两个数的平方和的结果。
以下是程序的运行结果示例:
Input x:
-1234↙
y=4321
a=43,b=21
result=2290
输入提示信息:“Input x:\n”
输入格式: “%d”
输出格式:
逆序数输出格式:“y=%d\n”
逆序数拆分后的输出格式:“a=%d,b=%d\n”
平方和的输出格式:“result=%d\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int x, y, a, b, c, d, e;
    printf("Input x:\n");
    scanf("%d", &x);
    x = fabs(x);
    a=x/1000,b=(x%1000)/100,c=(x%100)/10,d=x%10;
    y=d*1000+c*100+b*10+a;
    printf("y=%d\n",y);
    printf("a=%d,b=%d\n",d*10+c,b*10+a);
    e = pow((d*10+c),2)+pow((b*10+a),2);
    printf("result=%d\n",e);
    return 0;
}

5. 拆分英文名


题目内容
从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含3个字母。如: tom),编写程序在屏幕上输出该同学的英文名,且首字母大写(如: Tom)。同时输出组成该英文名的所有英文字符在26个英文字母中的序号。
以下为程序的运行结果示例:
Input your English name:
tom↙
Tom
t:20
o:15
m:13
输入提示信息:“Input your English name:\n”
输入格式: “%c%c%c”
输出格式:
首字母大写的英文姓名的输出格式:"%c%c%c\n"
姓名中每个字母在26个英文字母中的序号的输出格式:"%c:%d\n"

代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a,b,c;
    printf("Input your English name:\n");
    scanf("%c%c%c",&a,&b,&c);
    a=a-32;
    printf("%c%c%c\n",a,b,c);
    a=a+32;
    printf("%c:%d\n",a,a-96);
    printf("%c:%d\n",b,b-96);
    printf("%c:%d\n",c,c-96);
    return 0;
}

6. 计算体指数


题目内容
从键盘输入某人的身高(以厘米为单位,如174cm)和体重(以公斤为单位,如70公斤),将身高(以米为单位,如1.74m)和体重(以斤为单位,如140斤)输出在屏幕上,并按照以下公式计算并输出体指数,要求结果保留到小数点后2位。程序中所有浮点数的数据类型均为float。
假设体重为w公斤,身高为h米,则体指数的计算公式为:
以下是程序的运行结果示例:
Input weight, height:
70,174↙
weight=140
height=1.74
t=23.12
输入提示信息:“Input weight, height:\n” (注意:在height和逗号之间有一个空格)
输入格式: “%d,%d”
输出格式:
体重输出格式:“weight=%d\n”
身高输出格式:“height=%.2f\n”
体指数输出格式:“t=%.2f\n”

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a,b;
    float t,c;
    printf("Input weight, height:\n");
    scanf("%d,%d",&a,&b);
    printf("weight=%d\n",2*a);
    c=(float)b/100;
    printf("height=%.2f\n",c);
    t=a/(pow(c,2));
    printf("t=%.2f\n",t);
    return 0;
}
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值