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"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main( )
{
int i, j, len;
printf("Please input a capital:\n");
char c;
scanf("%c", &c);
len=c-'A';
for(i=0; i<=len; ++i){
for(j=0; j<=len-i; ++j){
printf(" ");
}
for(j=0; j<=i; ++j){
printf("%c",'A'+j);
}
for(j=i-1; j>=0; --j){
printf("%c", 'A'+j);
}
printf("\n");
}
return 0;
}
2循环嵌套的应用(4分)
题目内容:
编写程序产生如下输出:
F
FE
FED
FEDC
FEDCB
FEDCBA
输入格式: 无
输出格式:"%c"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main( )
{
printf("F\nFE\nFED\nFEDC\nFEDCB\nFEDCBA\n");
return 0;
}
提交答案本次得分/总分:4.00/4.00分
3利用泰勒级数计算sinx的值(4分)
题目内容:
利用泰勒级数计算sinx的值,要求最后一项的绝对值小于,并统计出此时累加了多少项。请用“利用前项来计算后项”的方法计算累加项,不要使用pow函数编写程序。程序中所有实数的数据类型都是double类型。
程序的运行结果示例1:
Input x:
3↙
sin(x)=0.141,count=9
程序的运行结果示例2:
Input x:
10↙
sin(x)=-0.544,count=18
输入提示信息:“Input x:\n”
输入格式: “%lf”
输出格式:“sin(x)=%.3f,count=%d\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
const double EPS = 1e-5;
double fact( double x ){
if(x<EPS) return 1;
else return fact(x-1)*x;
}
int main( )
{
double term=0, sin=0, x, sign=1;
int cnt=0;
printf("Input x:\n");
scanf( "%lf", &x);
term=x;
while(fabs(term)>=EPS){
sin+=term;
++cnt;
sign=-1;
term=term*sign*x*x/(2*cnt)/(2*cnt+1);
}
printf( "sin(x)=%.3f,count=%d\n" , sin, cnt+1);
return 0;
}
提交答案本次得分/总分:4.00/4.00分
4计算100~200之间的所有素数之和(4分)
题目内容:
计算100~200之间的所有素数之和,判别一个数是否是素数请用给定的函数实现。
函数原型:int fun(int m);
说明:
参 数:m 是要进行判断的数;
返回值:若数 m 是素数,则返回值为1;否则返回值为0。
输入格式: 无
输出格式: “sum=%d\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
const double EPS = 1e-5;
int IsPrime(int n){
if(n<=1) return 0;
else {
int i, s=sqrt(n);
for(i=2; i<=s; ++i){
if(n%i==0) return 0;
}
return 1;
}
}
int main( )
{
int i=100, sum=0;
for( i=100; i<=200; ++i){
if(IsPrime(i)){
sum+=i;
}
}
printf("sum=%d\n", sum);
return 0;
}
提交答案本次得分/总分:4.00/4.00分
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”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
C
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
const double EPS = 1e-5;
int IsPrime(int n){
if(n<=1) return 0;
else {
int i, s=sqrt(n);
for(i=2; i<=s; ++i){
if(n%i==0) return 0;
}
return 1;
}
}
int main( )
{
printf("Please enter min,max:\n");
int Min, Max, v, res;
scanf("%d,%d", &Min, &Max);
do{
printf( "Please enter an integer [%d..%d]:\n", Min, Max);
res = scanf("%d", &v);
char ch=getchar();
while(ch!='\n' && ch!=EOF) ch=getchar();
}while(v<Min || v>Max || res!=1);
printf("The integer you have entered is:%d\n", v);
return 0;
}
用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
2
用例2通过 2ms 140kb
2
提交答案本次得分/总分:4.00/4.00分
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前面有一个空格)
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
C
#include<stdio.h>
int main()
{
int score, res;
char grade;
do{
printf("Please input score:\n");
res=scanf("%d", &score);
if(score < 0 || score > 100 || res!=1){
printf("Input error!\n");
}
char ch=getchar();
while(ch!='\n' && ch!=EOF) ch=getchar();
}while(score < 0 || score > 100 || res!=1);
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通过 2ms 148kb
1
用例2通过 2ms 256kb
1
用例3通过 2ms 256kb
1
用例4通过 2ms 256kb
1
用例5通过 1ms 256kb
1
提交答案本次得分/总分:5.00/5.00分
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”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
C
#include<stdio.h>
int main()
{
int i=0, n, a, term;
long sum=0 ;
printf("Input a,n:\n");
scanf("%d,%d", &a, &n);
term=a;
for(i=0; i<n; ++i){
sum+=term;
term=term*10+a;
}
printf( "sum=%ld\n", sum);
return 0;
}
用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
2
用例2通过 2ms 256kb
1
用例3通过 2ms 256kb
1
提交答案本次得分/总分:4.00/4.00分
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”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
C
#include<stdio.h>
int main()
{
printf( "Input n(27<n<=77):\n");
int i, j, k, n;
scanf("%d", &n);
for(i=0; i<n; ++i){
for(j=0; j<n; ++j){
k=(36-i-j);
if((k%2==0) && i*4+j*3+(k)/2==n){
printf("men=%d,women=%d,children=%d\n",i, j, k);
}
}
}
return 0;
}
用例测试结果 运行时间 占用内存 提示 得分
用例1通过 15ms 256kb
2
用例2通过 2ms 256kb
1
用例3通过 2ms 256kb
1
提交答案本次得分/总分:4.00/4.00分
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"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
C
#include<stdio.h>
int IsLeapYear(int n){
if(n%4==0 && n%100!=0 || n%400==0) return 1;
else return 0;
}
int d[2][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};
int main()
{
printf( "Input year,month:\n" );
int y, m;
scanf("%d,%d", &y, &m);
if(m<=0 || m>12){
printf("Input error!\n" );
} else {
printf("%d days\n", d[IsLeapYear(y)][m-1]);
}
return 0;
}
用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
1
用例2通过 2ms 256kb
1
用例3通过 2ms 256kb
1
用例4通过 2ms 256kb
1
用例5通过 2ms 256kb
1
提交答案本次得分/总分:5.00/5.00
894

被折叠的 条评论
为什么被折叠?



