下面代码的功能是将百分制成绩转换为 5 分制成绩,具体功能是: 如果用户输入的是 非法
字符或者不在合理区间内的数据 (例如输入的是 a,或者 102 ,或-45 等),则程序输出 Input error! ,
并允许用户 重新输入 ,直到输入合法数据为止 ,并将其转换为 5 分制输出。目前程序存在 错误 ,
请将其修改正确。并按照下面给出的运行示例检查程序。
#include
#include
int main()
{
char score[100];
int flag = 0, i, s;
char grade;
printf("Please input score:\n");
while (1)
{
flag=0;
scanf("%s", score);
for (i = 0; i
{
if (score[i] >= '0' && score[i] <= '9')
{
continue;
}
else
{
flag = 1;
break;
}
}
s = atoi(score);
if (s < 0 || s > 100 || flag == 1)
{
printf("Input error!\n");
printf("Please input score:\n");
continue;
}
else{
break;
}
}
s = atoi(score);
if (s >= 90)
{
grade = 'A';
}
else if (s >= 80)
{
grade = 'B';
}
else if (s >= 70)
{
grade = 'C';
}
else if (s >= 60)
{
grade = 'D';
}
else
{
grade = 'E';
}
printf("grade: %c\n", grade);
return 0;
}
编程计算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 的值。
#include
#include
int main()
{
int n,a,i,j;
double p=0,q=0;
printf( "Input a,n:\n" );
scanf( "%d,%d" ,&a,&n);
for (i=1;i<=n;i++)
{
for (j=0,p=0;j
{
p=p+a*pow(10,j);
}
q=p+q;
}
printf( "sum=%.0f\n" ,q);
return 0;
}
搬砖问题( 4 分)
题目内容:
n 块砖( 27
各需多少人?请用穷举法编程求解 ,n 的值要求从键盘输入。 输出结果按照男人数量升序给出(见下面示例
3)。
#include "stdio.h"
main()
{
int a, b, c;
long n, i, t, s = 0;
printf("Input n(27
scanf("%d", &n);
for (a = 0; 4 * a <= n; a++)
for (b = 0; 4 * a + 3 * b <= n; b++)
for (c = 0; 4 * a + 3 * b + c / 2 <= n; c += 2)
if (4 * a + 3 * b + c / 2 == n && c%2 == 0 &&a+b+c==36)
{
printf("men=%d,women=%d,children=%d\n", a, b, c);
}
}
编程输出某年某月有多少天(考虑到闰年)。( 5 分)
题目内容:
从键盘输入一个年份和月份,输出该月有多少天( 考虑闰年 ),用 switch 语句编程。
#include
int main()
{ int year,month,day;
printf( "Input year,month:\n" );
scanf( "%d,%d" ,&year,&month);
switch (month)
{
case 1: day=31; break ;
case 2: day=28; break ;
case 3: day=31; break ;
case 4: day=30; break ;
case 5: day=31; break ;