c语言程序设计精髓 第三周练兵题

1日期显示(3分)

题目内容:

编写一个程序, 接收用户录入的日期信息并且将其显示出来. 其中, 输入日期的形式为月/日/年(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 year,month,day;
    printf("Enter a date (mm/dd/yy):\n");
    scanf("%d/%d/%d",&month,&day,&year);
    printf("You entered the date: %04d.%02d.%02d\n",year,month,day);
}

2产品信息格式化(3分)

题目内容:

编写一个程序, 对用户录入的产品信息进行格式化。

以下为程序的运行结果示例:

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 year,month,day,numb;
    float price;
    printf("Enter item number:\n");
    scanf("%d",&numb);
    printf("Enter unit price:\n");
    scanf("%f",&price);
    printf("Enter purchase date (mm/dd/yy):\n");
    scanf("%d/%d/%d",&month,&day,&year);
    printf("Item      Unit     Purchase\n");
    printf("%-9d$ %-9.2f%02d%02d%04d\n",numb,price,month,day,year);
}

3计算两个数的平方和(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;
    printf("Please input x and y:\n");
    scanf("%f,%f",&x,&y);
    printf("Result=%.2f\n",pow(x,2)+pow(y,2));

}

4逆序数的拆分计算(3分)

题目内容:

从键盘输入一个4位数的整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-1234,忽略负号,由1234分离出其千位1、百位2、十位3、个位4,然后计算4 * 1000+3 * 100+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>
int main()
{
    int x,y,a,b;
    printf("Input x:\n");
    scanf("%d",&x);
    x=abs(x);
    y=x/1000+10*(x/100%10)+100*(x/10%10)+1000*(x%10);
    printf("y=%d\n",y);
    a=y/100;
    b=y%100;
    printf("a=%d,b=%d\n",a,b);
    printf("result=%d\n",(int)(a*a+b*b));
}

5拆分英文名(3分)

题目内容:

从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含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);
    printf("%c%c%c\n",a-32,b,c);//
    printf("%c:%d\n",a,a-96);//
    printf("%c:%d\n",b,b-96);//
    printf("%c:%d\n",c,c-96);//
}

6计算体指数(3分)

题目内容:

从键盘输入某人的身高(以厘米为单位,如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>
int main()
{
    int a,b;
    float height;
    printf("Input weight, height:\n");
    scanf( "%d,%d",&a,&b);
    height = b/100.0;
    printf("weight=%d\n",a*2);//
    printf("height=%.2f\n",height);//
    printf("t=%.2f\n",a/(height*height));//
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值