哈工大C语言--SSE改错01
改错
- *百鸡问题
- *构成三角形
- *斐波那契数列
- *数组排序
- *华氏转摄氏
- *数列之积
- *int-char-float
- *最大公约数
- *字符大小
- *构成三角形
- *和、差、积、商和余
- *有关输入输出问题。
- *100~200素数
- **删除字符串s中所出现的与变量c相同的字符
- *折半查找
- *数列之积
- *阶梯跨步
- *判断素数
- *奥运会国名顺序
- *输出"China
- *泰勒级数求sinx
- *求阶乘
- *由小到大输出两实数
*百鸡问题
我国古代的《张丘建算经》中有这样一道著名的百鸡问题:
“鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。百钱买百鸡,问鸡翁、母、雏各几何?” 其意为:公鸡每只5元,母鸡每只3元,小鸡3只1元。用100元买100只鸡,问公鸡、母鸡和小鸡各能买多少只?
已知本题的解有4个。
下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。
注意:将修改后的完整的源程序写在答题区内。对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。
当且仅当错误全部改正,且程序运行结果调试正确,才给加5分,如果只改正了部分错误,则不加分。
**输出格式要求:见以下代码。
#include <stdio.h>
main()
{
int x, y, z;
for (x = o; x <= 20; x++);
{
for (y = o; y <= 33; y++);
{
z = 100 – x - y;
if (5x + 3y + z/3 = 100)
{
printf("x=%d, y=%d, z=%d\n", x, y, z);
}
}
}
}
', '
注意半角减号和全角横线的区别;
小鸡不足三只也要算1元。', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
int x, y, z;
for (x = 0; x <= 20; x++)
{
for (y = 0; y <= 33; y++)
{
z = 100 - x - y;
if (5*x + 3*y + z/3 == 100 && z%3==0)
{
printf("x=%d, y=%d, z=%d\n", x, y, z);
}
}
}
}
', 30),
(72, 2, '2', 'Q207', 'Q207.(5分)
*构成三角形
下面程序用于输入三角形的三条边a,b,c,判断它们能否构成三角形,若能则指出是何种三角形:等边三角形、等腰三角形、直角三角形、等腰直角三角形,一般三角形。
注意:请将修改正确后的完整源程序拷贝粘贴到答题区内。
对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。
当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。
改错时不能改变程序原有的意图,不能改变函数原型。
**输入输出格式要求:不要改变以下代码的输入输出格式。
', '
竟然没有答案。。。。大神谁满分了填一下吧。。。', '这个是十分的。不用谢', '.。。。本宝宝只能默默把十分的填在这里。。。那代码太丑。。。
#include <stdio.h>
main()
{
float a, b, c;
int flag;
scanf("%f,%f,%f",&a,&b,&c);
if (a+b>c && b+c>a && a+c>b)
{
if (a==b & b==c & c==a)
{
printf("等边");
flag = 0;
}
else if (a==b || b==c || c==a)
{
printf("等腰");
flag =0;
}
if(fabs(a*a+b*b-c*c)<0.1 || fabs(a*a+c*c-b*b)<0.1 || fabs(c*c+b*b-a*a)<0.1)
{
printf("直角");
flag=0;
}
if(flag)
{
printf("一般");
}
printf("三角形\n");
}
else
{
printf("不是三角形\n");
}
}
', '', '', '', '', '
#include <stdio.h>
#include <math.h>
main()
{
float a, b, c;
printf("Input a,b,c:");
scanf("%f,%f,%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
{
//if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
if (floor(a * a + b * b - c * c + 0.5) == 0 || floor(a * a + c * c - b * b + 0.5) == 0 || floor(b * b + c * c - a * a + 0.5) == 0)
{
if (a == b || b == c || a == c) printf("等腰直角三角形");
else printf("直角三角形");
}
else if (a == b && a == c) printf("等边三角形");
else if (a == b || b == c || a == c) printf("等腰三角形");
else printf("一般三角形");
}
else printf("不是三角形");
}
', 81),
(225, 105, '105', 'Q182', '
*斐波那契数列
#include <stdio.h>
unsigned long Factorial(unsigned int n);
{
if (n < 0)
{
printf("data error!");
return 0;
}
else if (n==0 && n==1)
{
return 1;
}
else
{
return n * Factorial(n-1);
}
}
main()
{
int n;
unsigned long x;
printf("Input n:\n");
scanf("%d", n);
x = Factorial(n);
printf("%d!=%ld\n", n, x);
}
', '这道题的标答是空的,无论如何也不可能对(只写int main() {return 0;}也一样)。 遇到就跳过吧
', '"%d"', '"%d!=%ld\n"', '5', '5!=120 ', '
这是一道改错题,这里只说明程序错在哪里,不能帮你过SSE上的这道题', '
-
unsigned long Factorial(unsigned int n);末尾分号去掉
-
else if (n==0 && n==1)中的&&换为||
-
scanf("%d", n);少了& ', '
#include <stdio.h>
unsigned long Factorial(unsigned int n)
{
if (n < 0)
{
printf("data error!");
return 0;
}
else if (n==0 || n==1)
{
return 1;
}
else
{
return n * Factorial(n-1);
}
}
main()
{
int n;
unsigned long x;
printf("Input n:\n");
scanf("%d", &n);
x = Factorial(n);
printf("%d!=%ld\n", n, x);
}
', 18),
(251, 105, '105', 'Q1162', '
*数组排序
在下面给出的程序中,共有5处错误(包括语法错误和逻辑错误),请找出其中的错误,并改正之。本程序的功能是对a数组中的元素按从大到小的顺序排序。
#include <stdio.h>
#define N 10;
main()
{
int a[N]={2,17,8,3,24,53,82,1,29,101};
int i,j,k,t;
for(i=0;i<9;i++)
{ k=i;
for(j=i+1;j<10;j++)
if(a[j]<a[k]) k=j;
if(k=i)
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
for(i=0;i<9;i++)
printf("%d ",a[i]);
}
', '
标准答案与题目要求有出入,标答只打印了排序后数组前九个数,该数组有10个元素。
想要通过,只打印前九个就行。', '无', '"%d "', '无', '
101 82 53 29 24 17 8 3 2 ', '
两个连着的将a[j] 改成 a[k]似乎算两个错误,不然这题标答只改了4个错误。', '
#include <stdio.h>
#define N 10
main()
{
int a[N]={2,17,8,3,24,53,82,1,29,101};
int i,j,k,t;
for(i=0;i<9;i++)
{ k=i;
for(j=i+1;j<10;j++)
if(a[j]>a[k]) k=j;
if(k!=i)
{
t=a[k];
a[k]=a[i];
a[i]=t;
}
}
for(i=0;i<9;i++)
printf("%d ",a[i]);
}
', '
错误:
define后面有";"(#define N 10; => #define N 10)
a[j]<a[k]只能按由小到大排序(if(a[j]<a[k]) => if(a[j]>a[k]))
if(k=i)反了(if(k=i) => if(k!=i))
t=a[j];a[j]=a[i]; => t=a[k];a[k]=a[i];', 11),
(252, 105, '105', 'Q176', '', '
这题没有标答,答案是空的。
本指导提供改错后的程序代码', '"%d"', '"%d!=%ld\n"', '4', '
Input n: 4!=24', '', '空', '
#include <stdio.h>
unsigned long Factorial(unsigned int n)
{
if (n < 0)
{
printf("data error!");
return 0;
}
else if (n==0 || n==1)
{
return 1;
}
else
{
return n * Factorial(n-1);
}
}
main()
{
int n;
unsigned long x;
printf("Input n:\n");
scanf("%d", &n);
x = Factorial(n);
printf("%d!=%ld\n", n, x);
}
', 25),
(43, 1, '1', 'Q1014', 'Q1014.(10分)
*华氏转摄氏
此程序是输入一个华氏温度与摄氏温度的转化问题。阅读程序,找出其中的错误,并改正之。
#include <stdio.h>
main()
{
double F,c;
scanf("%f",F);
c=5/9(F-32);
printf(''F=%2,2f\n'',F,''c=%2,2\n'',C);
}
', '坑,输出的等号两边有空格', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
double F, c;
scanf("%lf", &F);
c = 5.0 / 9 * (F - 32);
printf("F = %2.2lf\n", F);
printf("c = %2.2lf\n", c);
}
', 54);
(44, 1, '1', 'Q2127', 'Q2127.(10分)
*数列之积
利用pi/2=2/1 *2/3 *4/3 *4/5 *6/5 6/7... 的前100项之积,编程计算p的值。
下面程序中存在比较隐蔽的错误,
请通过分析和调试程序,发现并改正程序中的错误。
注意:将修改后的完整的源程序写在答题区内。
当且仅当错误全部改正,且程序运行结果调试正确,
才得满分,如果只改正了部分错误,则不加分。
#include <stdio.h>
main()
{
double term, result;
int n;
for (n=2, n<=100, n++)
{
term = (n * n) / ( n - 1) * ( n + 1);
result = result * term;
}
printf("result=%f\n", 2 * result);
}
', '
规律貌似不太好看,加括号、拆一下就好看了', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
double term, result=1.0;
int n;
for (n=2; n<=100; n+=2)
{
term = ((double)(n * n)) / (( n - 1) * ( n + 1));
result = result * term;
}
printf("result=%f\n", 2 * result);
}
', 58),
(57, 1, '1', 'Q1128', 'Q1128.(10分)
*int-char-float
由用户输入三个数据,算法如下,请改正程序中的错误,使它能得出正确的结果。
用户输入:12a4.2,
程序输出:
The input integer is : 12
The input character is : a
The input float is : 4.200000
#include <stdio.h>
main()
{
int i;
char ch;
float f;
printf("Please input:\n");
scanf("%d %c%f", &i, ch, &f);
printf("The input integer is : %d \nThe input character is : %c\n", i, ch);
printf("The input float is : %f", f);
}
', '注意%d %c%f的用法', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
int i;
char ch;
float f;
printf("Please input:\n");
scanf("%d%c%f", &i, &ch, &f);
printf("The input integer is : %d \nThe input character is : %c\n", i, ch);
printf("The input float is : %f", f);
}
', 73),
(64, 1, '1', 'Q1135', '(10分)
*最大公约数
根据如下性质,设计函数MaxCommonFactor(),计算两个正整数的最大公约数。
性质1:当a>b时,计算a与b的公约数等价于计算a-b与b的公约数。
性质2:当a<b时,计算a与b的公约数等价于计算b-a与b的公约数。
性质3:当a=b时,a与b的公约数等于a或b。
请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
int MaxCommonFactor(int a, int b);
main()
{
int a, b, x;
printf("Input a,b:");
scanf("%d,%d", a, b);
x = MaxCommonFactor(int a, int b);
if (x < 0) printf("Input Error!\n");
printf("%d\n", x);
}
int MaxCommonFactor(int a, int b)
{
if (a <= 0 || b <= 0)
return -1;
while if (a != b);
{
if (a > b)
return a - b;
else if (b > a)
return b - a;
else return a;
}
}
', '
性质2是错的,应为:性质2:当a<b时,计算a与b的公约数等价于计算b-a与a的公约数。', '', '', '', '', '', '', '
#include <stdio.h>
int MaxCommonFactor(int a, int b);
main()
{
int a, b, x;
printf("Input a,b:");
scanf("%d,%d", &a, &b);
x = MaxCommonFactor(a, b);
if (x < 0) printf("Input Error!\n");
printf("%d\n", x);
}
int MaxCommonFactor(int a, int b)
{
int t;
if (a <= 0 || b <= 0)
return -1;
while (a != b)
{
if (a > b)
a = a - b;
else
{
t = a;
a = b - a;
b = t;
}
}
return a;
}
', 16),
(76, 7, '7', 'Q1130', '
*字符大小
比较两个字符的大小,按由小到大输出。运行时输入数据:34↙ 请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
main()
{
char t, c1, c2;
getchar(c1), getchar(c2);
if (c1 > c2)
t = c1;
c1 = c2;
c2 = t;
printf("%c,%c", c1, c2);
}
', '', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
char t, c1, c2;
c1 = getchar();
c2 = getchar();
if (c1 > c2)
{
t = c1;
c1 = c2;
c2 = t;
}
printf("%c,%c", c1, c2);
}
', 51),
(78, 7, '7', 'Q1134', '
*构成三角形
下面程序用于输入三角形的三条边a,b,c,判断它们能否构成三角形,若能则指出是何种三角形:等腰三角形、直角三角形、一般三角形。请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
#define LIMIT = 1e-1;
main()
{
float a, b, c;
int flag = 1;
scanf("%d, %d, %d", a, b , c);
if (a + b > c && b + c > a && a + c > b)
{
if (fabs(a - b) <= LIMIT | fabs(b - c) <= LIMIT | fabs(c - a) <= LIMIT)
{
printf("等腰");
flag = 0;
}
else if (fabs(a * a + b * b - c * c) <= LIMIT
&& fabs(a * a + c * c - b * b) <= LIMIT
&& fabs(c * c + b * b - a * a) <= LIMIT)
{
printf("直角");
flag = 0;
}
if (!flag)
{
printf("一般");
}
printf("三角形\n");
}
else
{
printf("不是三角形\n");
}
}
', '', '', '', '', '', '', '', '
#include <stdio.h>
#include "math.h"
#define LIMIT 1e-1;
main()
{
float a, b, c;
int flag = 1;
scanf("%f, %f, %f", &a, &b, &c);
if (a + b > c && b + c > a && a + c > b)
{
if ((a == b) || (b == c) || (c == a) )
{
printf("等腰");
flag = 0;
}
else if ((a * a + b * b == c * c)
|| (a * a + c * c == b * b)
|| (c * c + b * b == a * a))
{
printf("直角");
flag = 0;
}
if (flag)
{
printf("一般");
}
printf("三角形\n");
}
else
{
printf("不是三角形\n");
}
}
', 65),
(88, 1, '1', 'Q1116', 'Q1116.(10分)
*和、差、积、商和余
求输入两个数的和、差、积、商和余数。请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
main()
{
float a, b;
float sum, minus, product, quotient;
int remainder;
printf("\n请输入两个数:\n");
scanf("%f\n%f", a, &b);
sum = a + b;
minus = a - b;
product = a * b;
quotient = a / b;
remainder = a % b;
printf("和为:%.2f\n", sum);
printf("差为:%.2f\n", minus);
printf("积为:%.2f\n", product);
printf("商为:%.2f\n", quotient);
printf("余数为:%d\n", remainder);
}
', '浮点数不能直接取模,要转化成int', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
float a, b;
float sum, minus, product, quotient;
int remainder;
printf("\n请输入两个数:\n");
scanf("%f%f", &a, &b);
sum = a + b;
minus = a - b;
product = a * b;
quotient = a / b;
remainder = (int)a % (int)b;
printf("和为:%.2f\n", sum);
printf("差为:%.2f\n", minus);
printf("积为:%.2f\n", product);
printf("商为:%.2f\n", quotient);
printf("余数为:%d\n", remainder);
}
', 59),
(100, 2, '2', 'Q1126', 'Q1126.(10分)
*有关输入输出问题。
输入为:
12a↙
b↙
运行结果为:1,2,a,b,123.300000,65535
请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
main()
{
int b;
unsigned short a = 65535;
short k = a;
char c, d;
int f, g;
b = (1234.0 - 1) / 10;
scanf("%c", &c);
scanf("%c", &d);
scanf("%d", &f);
scanf("%d", &g);
printf("%c,%c,%c,%c,%f,%d", c, d , f, g, b, k);
}
', '抄答案吧,那个减0.000003是我实在过不去了做的。。。', '', '', '', '', '', '
#include <stdio.h>
main()
{
double b;
unsigned short a = 65535;
unsigned short k = a;
char c, d;
char f, g;
b = (1234.0 - 1) / 10;
scanf("%c", &c);
scanf("%c", &d);
scanf("%c", &f);
getchar();
scanf("%c", &g);
printf("%c,%c,%c,%c,%f,%u", c, d , f, g, b, k);
}
', '
#include <stdio.h>
main()
{
float b;
unsigned short a = 65535;
unsigned short k = a;
char c, d;
int f, g;
b = (1234.0 - 1) / 10;
scanf("%c", &c);
scanf("%c", &d);
scanf("%c", &f);
scanf(" %c", &g);
printf("%c,%c,%c,%c,%f,%d", c, d , f, g, b-0.000003, k);
return 0;
}
', 86),
(119, 1, '1', 'Q2146', 'Q2146.(10分)
*100~200素数
求100~200间的全部素数(即质数),要求每行输出10个素数。
下面程序中存在比较隐蔽的错误,
请通过分析和调试程序,发现并改正程序中的错误。
注意:请将修改正确后的完整源程序拷贝粘贴到答题区内。 对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。 当且仅当错误全部改正,且程序运行结果调试正确,才得满分。', '
答案有错,第一行就输出了一个回车', '', '', '', '', '', '
#include <stdio.h>
main()
{
int m,k,i,n;
for(m=101;m<=200;m+=2)
{
if(n%10==0)
printf("\n");
k=sqrt(m);
for(i= 1;i<=k;i++)
if(m%i==0)
continue;
if(i==m%10)
{
printf("%d ",m);
n++;
}
}
}
', '', 4),
(148, 7, '7', 'C6447', '
**删除字符串s中所出现的与变量c相同的字符
下面程序的Squeeze函数的功能是删除字符串s中所出现的与变量c相同的字符。
注意:请将修改正确后的完整源程序上传。
对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。
当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。
**输入输出格式要求:不要增加输入提示信息;
按原题要求,先输入字符串s,回车换行后再输入字符c (c为任意字符)。
#include <stdio.h>
void Squeeze(char s[], char c);
main()
{
char a[80],c;
scanf("%s",a);
scanf("%c",c);
Squeeze(char a[],char c);
printf("%s\n",a);
}
void Squeeze(char s[], char c);
{
int i,j;
for (i=0; s[i]!=''\0''; i++);
{
if (s[i] != ''c'')
{
s[j] = s[i];
j++;
}
s[i]=''\0'';
}
}
', '后两组用例中有空格,所以应该用gets。要求删除的字符也有空格。
建议使用scanf("%c",&c).', '', '', '', '', '', '', '
#include <stdio.h>
void Squeeze(char s[], char c);
int main()
{
char a[80],c;
gets(a);
scanf("%c",&c);
Squeeze(a,c);
printf("%s\n",a);
return 0;
}
void Squeeze(char s[], char c)
{
int i,j = 0;
for (i=0; s[i] !=''\0''; i++)
{
if (s[i] != c)
{
s[j] = s[i];
j++;
}
}
s[j]=''\0'';
}
', 61);
(156, 2, '2', 'Q2135', '(10分)
*折半查找
下面程序采用折半查找法在10个有序数中寻找某个数,如果找到, 输出其所在数组中下标的位置,否则,输出没找到。
注意:
- 请将修改正确后的完整源程序拷贝粘贴到答题区内。
- 对于没有错误的语句,请不要修改, 修改原本正确的语句也要扣分。
- 当且仅当错误全部改正,且程序运行结果调试正确,才得满分。
', '#', '#', '#', '#', '#', '#', '
#include <stdio.h>
int main( )
{
int up = 9, low = 0, mid, found = 0, find;
int a[10] = {1, 5, 6, 9, 11, 17, 25, 34, 38, 41};
scanf("%d", &find);
printf("\n");
while (up >= low && !found)
{
mid = (up + low) / 2;
if (a[mid] == find)
{
found = 1;
}
else if (a[mid] > find)
up = mid - 1;
else
low = mid + 1;
}
if (found)
printf("found number is %dth.\n", mid);
else
printf("no found.\n");
return 0;
}
', '', 11),
(163, 75, '75', 'Q1246', '
下面程序用于输入三角形的三条边a,b,c,判断它们能否构成三角形,若能则指出是何种三角形:等腰三角形、直角三角形、一般三角形。(找出其中的3处错误并改正之)
#include <stdio.h> #include <math.h>
main() { float a, b, c; int flag = 1;
scanf("%d,%d,%d", &a, &b , &c);
if (a+b>c && b+c>a && a+c>b)
{
if (fabs(a-b)<=0.1||fabs(b-c)<=0.1||fabs(c-a)<=0.1)
{
printf("等腰三角形\n");
flag = 0;
}
else if (fabs(a*a+b*b-c*c)<=0.1
||fabs(a*a+c*c-b*b)<=0.1
||fabs(c*c+b*b-a*a)<=0.1)
{
printf("直角三角形\n");
flag = 0;
}
if (!flag)
{
printf("一般三角形\n");
}
}
else
{
printf("不是三角形\n");
}
}
', '', '', '', '', '', '', '', '
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
int flag = 1;
scanf("%f,%f,%f", &a, &b , &c);
if (a+b>c && b+c>a && a+c>b)
{
if (fabs(a-b)<=0.1||fabs(b-c)<=0.1||fabs(c-a)<=0.1)
{
printf("等腰三角形\n");
flag = 0;
}
else if (fabs(a*a+b*b-c*c)<=0.1
||fabs(a*a+c*c-b*b)<=0.1
||fabs(c*c+b*b-a*a)<=0.1)
{
printf("直角三角形\n");
flag = 0;
}
if (flag)
{
printf("一般三角形\n");
}
}
else
{
printf("不是三角形\n");
}
return 0;
}
', 48),
(182, 91, '91', 'Q1239', '
*数列之积
下面程序用于利用π/2 = 2/1 * 2/3 * 4/3 * 4/5 * 6/5 * 6/7 *... 前200项之积,编程计算π找出以下程序中的3处错误,并改正之。
#include<stdio.h>
main()
{
double term, result;
int n;
for (n = 2; n <= 100; n = n + 2)
{
term =( n * n)/( n - 1) * ( n + 1);
result = result * term;
}
printf("result = %f\n", 2*result);
}
', '答案是按计算前100项来的。。', '', '', '', '', '', '
#include <stdio.h>
main()
{
double term, result=1;
int n;
for (n = 2; n <= 100; n = n + 2)
{
term =(double)( n * n)/(( n - 1) * ( n + 1));
result = result * term;
}
printf("result = %f\n", 2*result); }', '
#include<stdio.h>
main()
{
double term, result=1.000000, n;
for (n = 2; n <= 100; n = n + 2)
{
term = n * n / ( n - 1) / ( n + 1);
result = result * term;
}
printf("result = %f\n", 2 * result);
}
', 22);
(201, 75, '75', 'Q1695', '(10分)
*阶梯跨步
爱因斯坦曾出过这样一道数学题:有一条长阶梯,若每步跨2阶,最后剩下1阶;若每步跨3阶,最后剩下2阶;若每步跨5阶,最后剩下4阶;若每步跨6阶,则最后剩下5阶;只有每步跨7阶,最后才正好1阶不剩。
参考例6.15程序,编写计算这条阶梯共有多少阶的程序如下所示,其中存在一些语法和逻辑错误,请找出并改正之,然后上机运行程序并写出程序的运行结果。
#include <stdio.h>
int main()
{
int x = 1, find = 0;
while(!find)
{
if (x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0)
{
printf("x = %d\n", x);
find = 1;
}
x++;
}
return 0;
}
', 53),
(290, 175, '175', 'Q1157', '
*判断素数
阅读以下程序,找出其中的5处错误,并改正之。
#include <math.h>
int isprime(int m)
main()
{
int n,flag;
printf(“Input n:”);
scanf(“%d”, n);
flag=isprime(n);
if(flag)
printf(Yes!\n”)
else
printf(“No!\n”);
}
/* 函数名: isprime
函数功能:判断m是否为素数
入口参数:整型数m
返回值: 返回值为1时,表示m是素数;
返回值为0时,表示m不是素数 */
int isprime(int *m)
{
int i;
if(m=1)
return 0; /*1不是素数,所以返回0值*/
for(i=2; i<=sqrt(m); i++)
{
if(m%i==0) return 0;
}
return 1;
}
', '', '', '', '', '', '', '', '
#include <stdio.h>
#include <math.h>
int isprime(int m) ;
main()
{
int n,flag;
printf("Input n:");
scanf("%d", &n);
flag=isprime(n);
if(flag)
printf("Yes!\n");
else
printf("No!\n");
}
/* 函数名: isprime
函数功能:判断m是否为素数
入口参数:整型数m
返回值: 返回值为1时,表示m是素数;
返回值为0时,表示m不是素数 */
int isprime(int m)
{
int i;
if(m==1) return 0; /*1不是素数,所以返回0值*/
else for(i=2; i<=sqrt(m); i++)
{
if(m%i==0) return 0;
}
return 1;
}
', 12),
(322, 175, '175', 'Q202', '(5分)
*奥运会国名顺序
请用指针数组编程实现按奥运会参赛国的国名在字典中的顺序对其入场次序进行排序。
假设参赛国不超过150个,参赛国的国名不超过9个字符。
下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。
#include <stdio.h>
#define MAX_LEN 10
#define N 150
void SortString(char *ptr[], int n);
main()
{
int i, n;
char *pStr[N];
printf("How many countries?\n");
scanf("%d",&n);
printf("Input their names:\n");
for (i=0; i<n; i++)
{
gets(pStr[i]);
}
SortString(pStr[i], n);
printf("Sorted results:\n");
for (i=0; i<n; i++)
{
puts(pStr[i]);
}
}
void SortString(char *ptr[], int n)
{
int i, j;
char temp;
for (i=0; i<n-1; i++)
{
for (j = i+1; j<n; j++)
{
if (ptr[j] < ptr[i]);
{
temp = ptr[i];
ptr[j] = ptr[i];
ptr[j] = temp;
}
}
}
}
注意:
请将修改正确后的完整源程序拷贝粘贴到答题区内。
对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。
当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。
改错时不能改变程序原有的意图,也不要改变代码的输入输出格式。
经教师手工核对后,如果未使用指针数组做函数参数编程,那么即使运行结果正确也不给分。', '', '', '', '', '', '', '
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80
#define N 150
void SortString(char *ptr[], int n);
main()
{
int i, n;
char *pStr[N], str[N][MAX_LEN];
printf("How many countries?\n");
scanf("%d", &n);
for (i = 0; i < N; i++)
pStr[i] = str[i];
printf("Input their names:\n");
for (i = 0; i < n; i++)
{
scanf("%s", pStr[i]);
}
SortString(pStr, n);
printf("Sorted results:\n");
for (i = 0; i < n; i++)
{
puts(pStr[i]);
}
}
void SortString(char *ptr[], int n)
{
int i, j;
char *temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(ptr[j], ptr[i]) < 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
', '', 14),
(341, 175, '175', 'Q1159', '
下面程序的功能是将数组元素倒置 ,找出其中的4处错误并改正之。
#include "stdio.h"
#define M 5;
main()
{ int a[M]={1,2,3,4,5};
int i,j,*t;
i=0;j=M-1;
while(i)
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j++
}
for(i=0;i<M;i++) printf("%d",(a+i));
}', '', '', '', '', '', '', '', '#include "stdio.h"
#define M 5
main()
{ int a[M]={1,2,3,4,5};
int i,j,*t;
i=0;j=M-1;
while(i!=j)
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j--;
}
for(i=0;i<M;i++) printf("%d",*(a+i));//4
}
', 9),
(342, 175, '175', 'Q1120', '(10分)
*输出"China
指针和字符数组的应用,用五种方法输出字符串“China”。请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
main()
{
int i = 0;
char str[6] = {''C'', ''h'', ''i'', ''n'', ''a'', ''\0''}, str1[6] , *ptr, *ptr1, *ptr2 , str2[5];
while (str[i] != ''\0'')
{
putchar(*str);
str++;
}
scanf("%s", ptr);
puts(ptr);
ptr1 = str;
puts(ptr1);
ptr2 = "China";
puts(ptr2);
str2 = "China";
printf("%s", str2);
}
', '', '', '', '', '', '', '', '
#include <stdio.h>
main()
{
int i = 0;
char str[6] = {''C'', ''h'', ''i'', ''n'', ''a'', ''\0''}, str1[6] , *ptr, *ptr1, *ptr2 , str2[5];
while (str[i] != ''\0'')
{
putchar(*(str+i));
i++;
}
ptr = str1;
scanf("%s", ptr);
puts(ptr);
ptr1 = str;
puts(ptr1);
ptr2 = "China";
puts(ptr2);
strcpy(str2, "China");
printf("%s", str2);
}
', 7),
(343, 176, '176', 'Q1321', '
*泰勒级数求sinx
利用泰勒级数sin(x)≈ x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...
计算sin(x) 的值。要求最后一项的绝对值小于10-5,并统计出此时累加了多少项。
#include <math.h>
#include <stdio.h>
main()
{
int n = 1,count = 1;
float x;
double sum , term;
printf("Input x: ");
scanf("%d", &x);
sum = x;
term = x;
do
{
term = -term*x*x/(n+1)*(n+2);
sum = sum + term;
n++;
count++;
}while (fabs(term) <= 1e-5);
printf("sin(x) = %f, count = %d\n", sum, count);
}
', '', '', '', '', '', '改错题 玛德跟答案一样只能得5分 气的我哟。', '
#include <math.h>
#include <stdio.h>
main()
{
int n = 1,count = 1;
float x;
double sum , term;
printf("Input x: ");
scanf("%f", &x);
sum = x;
term = x;
do
{
term = -term*x*x/((n+1)*(n+2));
sum = sum + term;
n = n + 2;
count++;
}while (fabs(term) >= 1e-5);
printf("sin(x) = %f, count = %d\n", sum, count);
}
', '', 12),
(348, 197, '197', 'Q1121', '
*求阶乘
计算n!算法如下,请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
long fact(int n);
main()
{
int n, result = 0;
printf("Input n:");
scanf("%d", &n);
result = fact(n);
printf("%d != %d", n, result);
}
long fact(int n)
{
int result;
if (n < 0)
printf("n<0,data error!\n");
else
{
result = n * fact(n - 1);
return result;
}
}
', '', '', '', '', '', '', '
#include <stdio.h>
long fact(int n);
main()
{
int n;
unsigned long result = 0;
printf("Input n:");
scanf("%d", &n);
result = fact(n);
if (result != -1)
printf("%d != %u", n, result);
}
long fact(int n)
{
unsigned long result;
if (n < 0)
{
printf("n<0,data error!\n");
return -1;
}
else if (n == 0 || n == 1)
return 1;
else
{
result = n * fact(n - 1);
return result;
}
}
', '', 18),
(364, 176, '176', 'Q1117', '
*由小到大输出两实数
函数fun功能:由小到大输出两实数。请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
main()
{
fun();
}
fun(float a, float b)
{
float t;
scanf("%f%f", &a, &b);
if (a < b)
{
t = a;
a = b;
b = t;
}
printf("%5.2,%5.2f\n", &a, &b);
}
', '事实上是从大到小输出。', '', '', '', '', '', '
#include <stdio.h>
fun()
{
float t;
float a, b;
scanf("%f%f", &a, &b);
if (a < b)
{
t = a;
a = b;
b = t;
}
printf("%5.2f,%5.2f\n", a, b);
}
main()
{
fun();
}
', '', 6),
