1绘制金字塔(4分)
题目内容:
要求用户从键盘输入一个大写字母,使用嵌套循环产生像下面这样的金字塔图案:
A
ABA
ABCBA
ABCDCBA
程序运行结果示例1:
Please input a capital:
D↙
____A
___ABA
__ABCBA
_ABCDCBA
程序运行结果示例2:
Please input a capital:
F↙
______A
_____ABA
____ABCBA
___ABCDCBA
__ABCDEDCBA
_ABCDEFEDCBA
(说明:上面运行结果示例中,每行字母前面的下划线"_"代表屏幕上实际输出的是空格,最后一行前面有一个空格,倒数第二行有两个空格,以此类推。)
输入提示信息:“Please input a capital:\n”
输入格式: “%c”
输出格式:"%c"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char c;
int line;
printf("Please input a capital:\n");
scanf(" %c",&c);
line=c-'A'+1;
for(int i=0;i<line;i++){
for(int j=0;j<line-i;j++){
printf("%c",'_');
}
for(int j='A';j<='A'+i;j++){
printf("%c",j);
}
for(int j='A'+i-1;j>'A'-1;j--){
printf("%c",j);
}
if(i != line-1)
printf("%c",'\n');
}
return 0;
}
2循环嵌套的应用(4分)
题目内容:
编写程序产生如下输出:
F
FE
FED
FEDC
FEDCB
FEDCBA
输入格式: 无
输出格式:"%c"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char c='F';
for(int i='F';i>='A';i--){
for(int j='F';j>=i;j--){
printf("%c",j);
}
printf("\n");
}
return 0;
}
3利用泰勒级数计算sinx的值(4分)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double getItem(int n,double x);
int main()
{
double x,sum=0,item,absItem,ss;
printf("Input x:\n");
scanf("%lf",&x);
int n = 0;
do{
item=getItem(n,x);
sum=sum+item;
n++;
if(item<0){
absItem=- item;
}else{
absItem= item;
}
}while(absItem - 0.00001>0);
printf("sin(x)=%.3f,count=%d\n",sum,n);
return 0;
}
double getItem(int n,double x){
//符号判断
int flag = n%2==1?-1:1;
double fenzi=1;
double fenmu=1;
//确定分母分子
int s = 2*n+1;
for(int i=1;i<=s;i++){
fenmu*=x;
fenzi*=i;
}
return flag*fenmu/fenzi;
}
4计算100~200之间的所有素数之和(4分)
题目内容:
计算100~200之间的所有素数之和,判别一个数是否是素数请用给定的函数实现。
函数原型:int fun(int m);
说明:
参 数:m 是要进行判断的数;
返回值:若数 m 是素数,则返回值为1;否则返回值为0。
输入格式: 无
输出格式: “sum=%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int fun(int m);
int main()
{
int sum = 0;
for(int i=100;i<200;i++){
if(fun(i)){
sum+=i;
}
}
printf( "sum=%d\n",sum);
return 0;
}
int fun(int m){
for(int i = 2; i<=sqrt(m);i++){
if(m%i==0) return 0;
}
return 1;
}
5编程实现一个输入指定范围内的整数的函数(4分)
题目内容:
编程实现一个输入指定范围内的整数的函数getint,其完整的函数原型为:int getint(int min, int max);,它负责接收用户的输入进行验证,保证接收的一定是一个介于min和max之间([min, max]区间内)的一个整数并最后返回该整数。如果用户输入不合法,则会提示继续输入,直到输入合法时为止。要求编写完整的程序并测试你所写的getint函数。
程序的运行结果示例:
Please enter min,max:
3,100↙
Please enter an integer [3…100]:
-2↙
Please enter an integer [3…100]:
0↙
Please enter an integer [3…100]:
116↙
Please enter an integer [3…100]:
58↙
The integer you have entered is:58
输入提示信息:“Please enter min,max:\n”
"Please enter an integer [%d..%d]:\n"
输入格式:
输入数据区间的最小值和最大值:"%d,%d"
输入指定范围内的整数: “%d”
输出格式:“The integer you have entered is:%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int fun(int m);
int main()
{
int max,min,indig,ret;
printf("Please enter min,max:\n");
scanf("%d,%d",&min,&max);
do{
fflush(stdin);
printf("Please enter an integer [%d..%d]:\n",min,max);
ret = scanf("%d",&indig);
}while(ret != 1 || indig < min || indig> max);
printf("The integer you have entered is:%d\n",indig);
return 0;
}
6程序改错v2.0(5分)
题目内容:
下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。
#include<stdio.h>
int main()
{
int score;
char grade;
printf("Please input score:");
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
grade = 'A’;
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}
程序运行结果示例1:
Please input score:
a↙
Input error!
Please input score:
-12↙
Input error!
Please input score:
230↙
Input error!
Please input score:
92↙
grade: A
程序运行结果示例2:
Please input score:
88↙
grade: B
程序运行结果示例3:
Please input score:
73↙
grade: C
程序运行结果示例4:
Please input score:
65↙
grade: D
程序运行结果示例5:
Please input score:
27↙
grade: E
输入提示信息:“Please input score:\n”
输入格式: “%d”
输出格式:
输入错误时的提示信息:“Input error!\n”
输出格式:“grade: %c\n” (注意:%c前面有一个空格)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int score,ret;
char grade;
do{
printf("Please input score:\n");
ret = scanf("%d", &score);
if(ret != 1){
printf("Input error!\n");
fflush(stdin);
}
}while(ret != 1 || score < 0 || score > 100);
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade: %c\n", grade);
return 0;
}
7编程计算a+aa+aaa+…+aa…a(n个a)的值(4分)
题目内容:
编程计算 a+aa+aaa+…+aa…a(n个a)的值,n和a的值由键盘输入。例如,当n=4,a=2,表示计算2+22+222+2222的值。
程序运行结果示例:
Input a,n:
2,4↙
sum=2468
输入提示信息:“Input a,n:\n”
输入格式: “%d,%d”(先输入a,后输入n)
输出格式: “sum=%ld\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a,b;
long sum=0,item=0,c=1;
printf("Input a,n:\n");
scanf("%d,%d", &a,&b);
for(int i=0;i<b;i++){
item= a * c +item;
sum+=item;
c=c *10;
}
printf("sum=%ld\n", (long)sum);
return 0;
}
8搬砖问题(4分)
题目内容:
n块砖( 27<n<=77 ),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男人、女人和小孩各需多少人?请用穷举法编程求解,n的值要求从键盘输入。输出结果按照男人数量升序给出(见下面示例3)。
程序的运行结果示例1:
Input n(27<n<=77):
28↙
men=0,women=4,children=32
程序的运行结果示例2:
Input n(27<n<=77):
36↙
men=3,women=3,children=30
程序的运行结果示例3:
Input n(27<n<=77):
60↙
men=2,women=14,children=20
men=7,women=7,children=22
men=12,women=0,children=24
输入提示: “Input n(27<n<=77):\n”
输入格式: “%d”
输出格式:“men=%d,women=%d,children=%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a,d;
long sum=0,item=0,c=1;
printf("Input n(27<n<=77):\n");
scanf("%d", &d);
for(int i=0;i<=17;i++){
for(int j=0;j<=36;j++){
a = 36-i-j;
if(4*i+0.5*j+3*a==d && a>=0){
printf("men=%d,women=%d,children=%d\n", i,a,j);
}
}
}
return 0;
}
9编程输出某年某月有多少天(考虑到闰年)(5分)
题目内容:
从键盘输入一个年份和月份,输出该月有多少天(考虑闰年),用switch语句编程。
程序运行结果示例1:
Input year,month:
2015,3↙
31 days
程序运行结果示例2:
Input year,month:
2015,4↙
30 days
程序运行结果示例3:
Input year,month:
2016,2↙
29 days
程序运行结果示例4:
Input year,month:
2014,2↙
28 days
程序运行结果示例5:
Input year,month:
2015,13↙
Input error!
输入提示信息:“Input year,month:\n”
输入格式: “%d,%d”
输出格式:
输入错误时的提示信息:“Input error!\n”
输出格式:
"31 days\n"
"30 days\n"
"29 days\n"
"28 days\n"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int year,month,flag,days;
int a[][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
printf("Input year,month:\n");
scanf("%d,%d", &year,&month);
if(month<1 ||month>12){
printf("Input error!\n");
}
if((year%4==0 && year%100 !=0) || year % 400 ==0){
days= a[1][month-1];
}else{
days = a[0][month-1];
}
if(days == 28){
printf("28 days\n");
}
if(days == 29){
printf("29 days\n");
}
if(days == 30){
printf("30 days\n");
}
if(days == 31){
printf("31 days\n");
}
return 0;
}