int MinCommonMultiple(int a, int b);
int main() { }
int MinCommonMultiple(int a, int b) { }
--------------------------------------32 33.
请编写函数fuc(),函数的功能是: 根据以下公式求出p的值,结果由函数值返回。m , n 是两个正整数,且 m>n. P= m!/(n!*(m-n)!)
**输入格式要求:\"%d %d\" 提示信息:\"请输入m,n的值( m>n ):\\n\" **输出格式要求:\"n项之和为:%lf\\n\" 程序运行示例如下: 请输入m,n的值( m>n ): 5 3
n项之和为:10.000000 答案:
int i;
for (i = 1; i <= a*b; i++) { }
if (i%a == 0 && i%b == 0)
return i;
int a, b, x;
printf(\"Input a,b:\"); scanf(\"%d,%d\", &a, &b); x = MinCommonMultiple(a, b);
printf(\"MinCommonMultiple = %d\\n\", x);
#include int fuc(int m, int n); int sum_s(int i);
int main() {
int m, n;
printf(\"请输入m,n的值( m>n ):\\n\"); scanf(\"%d %d\
printf(\"n项之和为:%lf\\n\ }
int fuc(int m, int n) {
int p;
p = sum_s(m) / (sum_s(n) * sum_s(m - n)); return p; }
int sum_s(int i) {
int s_ = 1, a;
for (a = 1;a <=i;a++) {
s_ = s_ *a; }
return s_; }
----------------------------------33 34.
采用穷举法,按如下函数原型
/* 函数功能:计算a和b的最小公倍数,当a或者b为非正整数时返回-1 */ int Lcm(int a, int b);
用函数编程实现计算两个正整数的最小公倍数(Least Common Multiple,LCM)的函数,在主函数中调用该函数计算并输出从键盘任意输入的两整数的最小公倍数。
**输入格式要求:\"%d,%d\" 提示信息:\"Input a,b:\"
**输出格式要求:\"Least Common Multiple of %d and %d is %d\\n\" \"Input error!\\n\"
程序运行示例如下: Input a,b:16,24
Least Common Multiple of 16 and 24 is 48 注:不允许使用goto语句
答案:
#include int Lcm(int a, int b); int main() {
int a, b;
printf(\"Input a,b:\"); scanf(\"%d,%d\ if(Lcm(a,b) > 0)
printf(\"Least Common Multiple of %d and %d is %d\\n\a, b, Lcm(a, b)); else
printf(\"Input error!\\n\"); }
int Lcm(int a, int b) {
int i;
if (a > 0 && b > 0) {
for (i = 1;i < a*b;i++) {
if ((i % a == 0) && (i % b == 0)) return i; } } else
return -1; }
--------------------34 35.
输入某班学生某门课的成绩(最多不超过40人), 当输入为负值时,表示输入结束,
用函数编程统计成绩不低于平均分的学生人数。 要求:
(1)按如下函数原型进行编程:
int GetAboveAver(int score[], int n); (2)在主函数中: 输入学生成绩,
然后调用函数GetAboveAver计算成绩不低于平均分的学生人数, 最后输出该人数。 要求:
(1)学生成绩和平均分均定义为int类型 (2)**无输入提示信息 **输入格式为:\"%d\"
**输出格式为:\"Students of above average is %d\\n\"
答案:
#include int a[40] = { 0 }, i = 0;
int GetAboveAver(int score[], int n); int main()
{
for (i; i <= 40; i++) {
int b = 0;
scanf(\"%d\ if (b >= 0)
a[i] = b; else
break; }
GetAboveAver(a, i); }
int GetAboveAver(int score[], int n) {
int sum = 0, c; int d, e = 0;
for (c = 0;c < i; c++) {
sum = sum + score[c]; }
d = sum / i;
for (c = 0;c < i; c++) {
if (score[c] >= d) e++; else
continue; }
printf(\"Students of above average is %d\\n\ return 0; }
----------------------------35 36.
从键盘任意输入10个整数存入一个数组中,
然后任意输入一个整数x,采用顺序查找法,在数组中查找该数, 如果找到,则函数返回该数在数组中的下标位置, 并在主函数中打印该值;
如果没有找到,则返回-1,并在主函数中打印“Not found!”。 要求按如下函数原型编程实现查找功能。 int Search(int a[], int n, int x); 在主函数中调用函数Search顺序查找x, 然后在主函数中打印查找结果。
要求必须按照题目要求和用函数编程,否则不给分。
**要求输入10个整数的提示信息格式为:
\"Input 10 numbers:\\n\"(每输入一个数,键一次回车); **要求输入整数x的提示信息格式为: \"Input x:\\n\"
**要求输出格式为:
找到时的打印格式为\"Subscript of x is %d\\n\" 没找到时的打印格式为\"Not found!\\n\"
答案:
#include int Search(int a[], int n, int x); int b[10] = {0}; int main() {
int x, n;
printf(\"Input 10 numbers:\\n\"); for (x = 0; x < 10; x++) {
scanf(\"%d\ b[x] = n; }
printf(\"Input x:\\n\"); scanf(\"%d\ Search(b,n,x);
if (Search(b,n,x) > 0)
printf(\"Subscript of x is %d\\n\ else
printf(\"Not found!\\n\"); }
int Search(int a[], int n, int x ) {
for (n = 0; n < 10; n++) {
if (a[n] == x) return n; else
continue; }
return -1; }
-------------------36 37.
利用一个字符数组作函数参数,实现字符串(最大长度为80个字符 )的逆序存放。
要求如下:
(1)在子函数Inverse中实现字符串的逆序存放。函数原型为: void Inverse(char str[]); (2)在主函数中
从键盘输入字符串(使用gets函数) 然后,调用Inverse函数, 最后,输出逆序后的字符串。
(3)**输入提示信息:\"Input a string:\\n\" **输出提示信息:\"Inversed results:\\n\" **输出格式:\"%s\\n\"
注:不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程,主函数不能使用int main和return 0。
答案:
#include char str_1[81];void Inverse(char str[]); int main() { }
void Inverse(char str[]) { }
-----------------------37 38.
char b; int a, c;
for (a = 0;a < 81;a++) { }
for (c = 0;c < (a) / 2.0; c++) { }
printf(\"Inversed results:\\n\"); printf(\"%s\\n\", str);
b = str[c];
str[c] = str[(a - 1) - c]; str[(a - 1) - c] = b; if (str[a] == '\\0')
break; continue; else
printf(\"Input a string:\\n\"); gets(str_1); Inverse(str_1);
从键盘任意输入一个字符串,计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程实现strlen()的功能。
**输入格式要求:\"%s\" 提示信息:\"Please enter a string:\" **输出格式要求:\"The length of the string is: %u\\n\" 程序的运行示例如下:
Please enter a string:Hello China The length of the string is: 11
答案:
#include main() {int a;
char str[100];
printf(\"Please enter a string:\"); gets(str);
for (a = 0;a <100; a++) {
if(str[a] == '\\0') break; else
continue; }
printf(\"The length of the string is: %u\\n\ }
----------------------38 39.
输入一行字符,统计其中的英文字符、数字字符、空格字符,以及其他字符的个数。请找出以下程序的错误,并改正之。 #include #include #define ARR_SIZE = 80;
main() {
char str[ARR_SIZE]; int len, i;
int letter=0,digit=0,space=0,other=0;
printf(\"请输入一个字符串:\"); gets(str);
len = strlen(str);
for (i=0; iif (a=letter ++; }else if (0=digit ++; }else if (str[i]=' ' ) { space ++; } else
other ++; }
printf(\"英文字符数:%d\\n\ printf(\"数字字符数:%d\\n\ printf(\"空格数:%d\\n\
printf(\"其他字符数:%d\\n\}
答案:
#include #include #define ARR_SIZE 80
main() {
char str[ARR_SIZE]; int len, i;
int letter = 0, digit = 0, space = 0, other = 0;
printf(\"请输入一个字符串:\"); gets(str);
len = strlen(str);
for (i = 0; i <= len; i++) {
if ('a' <= str[i] && str[i] <= 'z' || 'A' <= str[i] && str[i]<= 'Z')
}
}
{
letter++; }
else if ('0'<= str[i]&&str[i] <= '9') {
digit++; }
else if (str[i] == ' ') {
space++; } else
other++;
printf(\"英文字符数:%d\\n\ printf(\"数字字符数:%d\\n\ printf(\"空格数:%d\\n\
printf(\"其他字符数:%d\\n\
--------------------39 40.
编程判断输入的一个字符串是否是“回文”。所谓“回文”字符串就是左读和右读都一样的字符串。例如: \"abcba\"就是一个回文字符串。
输入提示信息:\"Input a string:\\n\" 输入格式:gets()
判断是回文的输出提示信息:\"This string is a plalindrome.\"
判断不是回文的输出提示信息:\"This string is not a plalindrome.\"
程序运行示例1: Input a string: abcba↙
This string is a plalindrome.
程序运行示例2: Input a string: friend↙
This string is not a plalindrome.
答案:
#include char str_1[81];void Inverse(char str[]); int main()
{
printf(\"Input a string:\\n\"); gets(str_1); Inverse(str_1); }
void Inverse(char str[]) {
char b; int a, c;
for (a = 0;a < 81;a++) {
if (str[a] == '\\0') break; else
continue; }
for (c = 0;c < (a) / 2.0; c++) {
if(str[c] == str[(a - 1) - c]) continue; else {
printf(\"This string is not a plalindrome.\");
return; } }
printf(\"This string is a plalindrome.\"); }
------------------------------40 41.
编程实现从键盘输入5个国名(每个国名最长80个字符), 找出并输出按字典顺序排在最前面的国名 要求:
(1)用gets输入字符串。 (2)
**输入提示信息为:\"Input five countries' names:\\n\" **输出格式为:\"The minimum is:%s\\n
答案:
#include main() {int i;
}
char a[5][81] = {0}, b, c;
printf(\"Input five countries' names:\\n\"); for (i = 0; i < 5;i++) {
gets(a[i]); }
b = a[0][0];
for (i = 1; i < 5; i++) {
if (a[i][0] < b) {
b = a[i][0]; c = i; }
else if (a[i][0] == b) {
if (a[i][1] < a[i - 1][1]) c = i; else
c = i - 1; } else
continue; }
printf(\"The minimum is:%s\\n\
---------------41 42.
一个n位正整数如果等于它的n个数字的n次方和,该数称为n位自方幂数。设计求3~6位自方幂数。 **输出格式要求:\"%d位自幂数有:\" \"%ld\\" \"\\n\" (每位完后换行)
程序运行示例如下:
3位自幂数有:153 370 371 407 4位自幂数有:1634 8208 9474 5位自幂数有:54748 92727 6位自幂数有:548834 尚未完成
---------------------42 43.
93084
从键盘为3*3的矩阵输入数据,找出主对角线上最大的元素,以及所在的行号。 **输入提示信息:无 **输入格式要求:\"%d\"
**输出格式要求:\"max=%d ,row=%d\" 程序运行示例如下:
1 2 3 4 5 6 7 8 9
max=9 ,row=2
注:不允许使用goto语句
答案:
#include main()
{ int i, j, row = 0, max; int a[3][3];
for (i = 0; i < 3; i++)
{ for (j = 0; j < 3; j++)
{
scanf(\"%d\ } }
max = a[0][0];
for (i = 0; i < 3; i++)
{ if (max < a[i][i])
{
max = a[i][i]; row = i; } }
printf(\"max=%d ,row=%d\ }
--------------------43 44.
任意输入英文的星期几,通过查找星期表,输出其对应的数字, 若查到表尾,仍未找到,则输出错误提示信息。
**输入格式要求:\"%s\" 提示信息:\"Please enter a string:\\n\" **输出格式要求:\"%s is %d\\n\" \"Not found!\\n\" 查找表中信息:
\"Sunday\\"Saturday\"
程序运行示例1如下: Please enter a string:
Monday
Monday is 1
程序运行示例2如下: Monkey
Not found!
答案:
#include #include #define WEEKDAYS 7#define MAX_STR_LEN 10 int main()
{ int i, pos;
int findFlag = 0; char x[MAX_STR_LEN];
char weekDay[][MAX_STR_LEN] = {\"Sunday\\"Wednesday\
printf(\"Please enter a string:\\n\"); scanf(\"%s\
for (i = 0; i < WEEKDAYS && !findFlag; i++)
{ if (strcmp(x, weekDay[i]) == 0)
{ pos = i;
findFlag = 1; } }
if (findFlag)
{ printf(\"%s is %d\\n\ } else
{ printf(\"Not found!\\n\"); }
return 0; }
-----------------------44 45.
有一个3*4的矩阵,求其中的最大元素的值。矩阵为: {{1,3,5,7},{2,4,6,8},{15,17,34,12}};
**输出格式要求:\"max value is %d\\n\" 程序运行示例如下: max value is 34
答案:
#include main() {int a[3][4] = {{1,3,5,7},{2,4,6,8},{15,17,34,12}}; int i, j, c; c = a[0][0];
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
if(a[i][j] >= c) c = a[i][j]; else
continue; } }
printf(\"max value is %d\\n\ }
-------------------45 46.
按如下函数原型编写程序,输入n×n阶矩阵,用函数编程计算并输出其两条对角线上的各元素之和。
void InputMatrix(int a[N][N], int n); int AddDiagonal(int a[N][N], int n); 输入提示信息: \"Input n:\"
\"Input %d*%d matrix:\\n\" 输入格式:\"%d\"
输出提示信息和输出格式:\"sum = %d\\n\"
答案:
#include #define PL 50 main() {int a[PL][PL];
int i, j, n, sum = 0; printf(\"Input n:\"); scanf(\"%d\
printf(\"Input %d*%d matrix:\\n\ for (i = 0; i < n; i++)
}
for(j = 0; j < n; j++) {
scanf(\"%d\ }
for (i = 0; i < n; i++) for(j = 0; j < n; j++) {
if(i == j || (i + j) == (n - 1)) sum = sum + a[i][j]; }
printf(\"sum = %d\\n\
------------------46 47.
有如下3*4的矩阵,求出其中值最大的元素的值。 1 2 3 4 9 8 7 6 10 -1 -4 4
在对数组进行初始化时,给出上面数据。
**要求输入提示信息为:无输入提示信息和输入数据 **要求输出格式为:\"max=%d\\n\"
答案:
#include main() {int a[3][4] = { { 1,2,3,4 },{ 9,8,7,6 },{ 10,-1,-4,4 } }; int i, j, c; c = a[0][0];
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
if (a[i][j] >= c) c = a[i][j]; else
continue; } }
printf(\"max=%d\\n\ }
--------------------47 48.
输入一个正整数n,再输入n个学生的成绩,计算平均分,并统计不及格成绩的个数。
**输入格式要求:\"%d\" 提示信息:\"Enter grade #%d: \" \"%lf\" 提示信息:\"Enter n: \"
**输出格式要求:\"Grade average = %.2f\\n\" \"Number of failures = %d\\n\" 程序运行示例如下: Enter n: 4
Enter grade #1: 67 Enter grade #2: 54 Enter grade #3: 88 Enter grade #4: 73 Grade average = 70.50 Number of failures = 1
答案:
#include double a[40] = { 0 }; int i = 0;int GetAboveAver(double score[], int n); int main() {
int n;
printf(\"Enter n: \"); scanf(\"%d\ for (i; i < n; i++) {
printf(\"Enter grade #%d: \ double b = 0; scanf(\"%lf\ if (b >= 0)
a[i] = b; else
break; }
GetAboveAver(a, n); }
int GetAboveAver(double score[], int n) {
int c;
double sum = 0, d; int e = 0;
for (c = 0;c < i; c++) {
sum = sum + score[c]; }
d = sum / i;
}
printf(\"Grade average = %.2f\\n\ for (c = 0;c < i; c++) {
if (score[c] < 60) e++; else
continue; }
printf(\"Number of failures = %d\\n\ return 0;
---------------------48 49.
直角三角形的三边满足a^2+b^2=c^2,如果三边都是整数,则称a、b、c为一组勾股数。
编程输出每边长都不大于20的所有勾股数。 **输入提示信息格式: 无 **输入数据格式要求: 无
**输出数据格式要求: \"a=%d\b=%d\c=%d\\n\"
答案:
#include #include main() {int a, b, c;
for (a = 1; a <= 20; a++)
for(b = 1;b <= 20; b++)
for(c = 1;c <= 20; c++) {
if(c >= b && b >=a)
if(pow(c,2) == pow(b,2)+pow(a,2)) printf(\"a=%d\b=%d\c=%d\\n\
} }
---------------------49 50.
求菲波那奇数列:数列1、1、2、3、5、8、13、21、…,是著名的菲波那奇数列,其递推通项公式为:U_1=U_2=1, (n=1,2)U_n=U_{n-1}+U_{n-2},(n>=3) 求出第n项的值,请编写程序。
**输入格式要求:\"%d\" 提示信息:\"Input n=?\" **输出格式要求:\"No. %d is %d\\n\" 程序运行示例如下: Input n=?10
No. 10 is 55
答案:
#include int Fct(int n); main() {int n;
printf(\"Input n=?\"); scanf(\"%d\
printf(\"No. %d is %d\\n\ }
int Fct(int n) {
if(n <= 2) return 1; else
return Fct(n-1)+Fct(n - 2); }
-----------------------50 51.
下面程序的功能是读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。找出其中的错误,并改正之。 #include \"stdio.h\" main()
{ int i,a,n=1; while(n<=7) {
do
{
scanf(\"%d\ }while(a<1 && a>50); for(i=1;i<=a;i++) printf(\"*\"); printf(\"\\n\"); n++; } }
答案:
#include \"stdio.h\" int main() {
int i, a, n = 1; while (n <= 7) {
}
scanf(\"%d\
if (a >= 1 && a <= 50);
for (i = 1;i <= a;i++) printf(\"*\");
printf(\"\\n\"); n++; }
-----------------51 52.
编写程序:有1,2,3,4共四个数字,能组成多少个互不相同的且无重复数字的三位数,都是多少。
**输出格式要求:\" % d % d % d\\n\" \"共有%d种组合!\" 程序运行示例如下: 1 2 3 1 2 4 1 3 2 1 3 4 1 4 2 1 4 3 2 1 3 2 1 4 2 3 1 2 3 4 2 4 1 2 4 3 3 1 2 3 1 4 3 2 1 3 2 4 3 4 1 3 4 2 4 1 2 4 1 3 4 2 1 4 2 3 4 3 1 4 3 2
共有24种组合!
答案:
#include main() {int a, b, c, d = 0; for (a = 1;a <= 4;a++) for (b = 1; b <= 4; b++ ) for (c = 1;c <= 4; c++) {
if(a != b && a != c && b != c) {
printf(\" % d % d % d\\n\ d++; } }
printf(\"共有%d种组合!\}
---------------------------------52 53.
用while语句编程,输入一组整型数据,然后显示每次将输入数据进行累加运算后的结果。当输入0时,停止输入数据,结束程序的运行。 **输入格式要求:\"%d\" 提示信息:\"Input num:\" **输出格式要求:\"sum = %d\\n\" 程序运行示例如下: Input num:1 sum = 1 Input num:2 sum = 3 Input num:3 sum = 6 Input num:4 sum = 10 Input num:0 sum = 10
答案:
#include main() {int sum = 0,num; do {
printf(\"Input num:\"); scanf(\"%d\ sum = sum + num;
printf(\"sum = %d\\n\ }while(num); }
--------------------------------------53 54.
利用pi/2 = (2/1)*(2/3)*(4/3)*(4/5)*(6/5)*(6/7)*...前100项之积,编程计算pi的值。
为保证计算精度,请用double类型计算。 **输出格式要求:\"pi = %f\\n\"
答案:
#include main() {double d = 1, pi; int i, j;
for(i = 1; i <= 50; i++)
for (j = 1; j <= 2; j++) {
if(j == 1)
d = d * (double)(i*2)/(i*2 - 1); else
d = d * (double)(i*2)/(i*2 + 1); } pi = 2*d;
printf(\"pi = %f\\n\ }
------------------------54 55.
利用π4=1−13+15−17+…,编程计算π的近似值,直到最后一项的绝对值小于10−4时为止,输出π的值并统计累加的项数。 **输出格式要求:\"pi = %f\\ncount = %d\\n\" 采用double定义累加和变量
答案:
#include int main() {double d = 0, pi; int i, j = 0, k = 1;
for (i = 1;1 / (double)i >= 0.0001 || 1 /(double) i < -0.0001; i = i + 2) {
if (k % 2 == 0) {
d = d - 1 / (double)i; j++;
}
k++; } else {
d = d + 1 / (double)i; j++; k++; } }
d = d + 1/(double)i; j++;
pi = 4 * d;
printf(\"pi = %f\\ncount = %d\\n\
---------------------------------55 56.
从键盘输入10个整数,编程计算并输出其最大值、最小值及其所在元素的下标位置。
输入格式:\"%d\"
输出格式:\"max=%d, pos=%d\\n\" \"min=%d, pos=%d\\n\" 输入样例:
1 2 3 4 5 6 7 8 9 10 输出样例: max=10, pos=9 min=1, pos=0
答案:
#include main() {int a[10] = {0}, j, i = 0, k = 0; int c, b;
for (j = 0; j < 10; j++) {
scanf(\"%d\ if(a[j] > i) {
i = a[j]; c = j; } if(j == 0) {
k = a[j]; b = j;
}
} else if(a[j] < k) {
k = a[j]; b = j; } }
printf(\"max=%d, pos=%d\\n\ printf(\"min=%d, pos=%d\\n\
----------------56 57.
冒泡排序(Bubble Sort),也称为沉降排序(Sinking Sort),之所以称其为冒泡排序,是因为算法中值相对较小的数据会像水中的气泡一样逐渐上升到数组的最顶端。与此同时,较大的数据逐渐地下沉到数组的底部。这个处理过程需在整个数组范围内反复执行多遍。每一遍执行时,比较相邻的两个元素。若顺序不对,则将其位置交换,当没有数据需要交换时,数据也就排好序了。编程将排序函数DataSort()改用冒泡法实现。
**输入格式要求:\"%d\" 提示信息:\"Input n:\" \"Input %d numbers:\" **输出格式要求:\"Sorting results:\" \"%4d\" 程序运行示例如下: Input n:10
Input 10 numbers: 2 9 3 4 0 6 8 7 5 1
Sorting results: 0 1 2 3 4 5 6 7 8 9
答案:
#includevoid DataSort(int a[],int n); main() {
int a[100] = {0}, n,i = 0; printf(\"Input n:\"); scanf(\"%d\
printf(\"Input %d numbers:\ do {
scanf(\"%d\ i++; }while(i < n); DataSort(a,n); }
void DataSort(int a[],int n) {
int j, t, i;
for(i = 0;i < n; i++)
}
for (j = 1;j < n; j++) if(a[j] < a[j - 1]) {
t = a[j-1]; a[j-1] = a[j]; a[j] = t; }
printf(\"Sorting results:\"); for(i = 0; i < n; i++) printf(\"%4d\
------------------------57 58.
冒泡排序
采用冒泡法进行升序排序法的基本原理是:对数组中的n个数执行n-1遍检查操作,在每一遍执行时,对数组中剩余的尚未排好序的元素进行如下操作:对相邻的两个元素进行比较,若排在后面的数小于排在前面的数,则交换其位置,这样每一遍操作中都将参与比较的数中的最大的数沉到数组的底部,经过n-1遍操作后就将全部n个数按从小到大的顺序排好序了。 程序的某次运行结果如下: Input n:10↙
Input 10 numbers:2 9 3 4 0 6 8 7 5 1↙
Sorting results: 0 1 2 3 4 5 6 7 8 9
输入格式:\"%d\" 输出格式:
输入数据个数提示:\"Input n:\"
输入数据提示:\"Input %d numbers:\" 输出提示:\"Sorting results:\" 输出格式:\"%4d\"
答案:
#includevoid DataSort(int a[], int n); main() {
int a[100] = { 0 }, n, i = 0; printf(\"Input n:\"); scanf(\"%d\
printf(\"Input %d numbers:\ do {
scanf(\"%d\
i++;
} while (i < n); DataSort(a, n); }
void DataSort(int a[], int n) {
int j, t, i;
for (i = 0;i < n; i++)
for (j = 1;j < n; j++)
if (a[j] < a[j - 1]) {
t = a[j - 1]; a[j - 1] = a[j]; a[j] = t; }
printf(\"Sorting results:\"); for (i = 0; i < n; i++)
printf(\"%4d\ }
----------------------------------58 59.(出题人是sb)
程序改错v1.0
下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,否则将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。 #include int main() {int score; char grade;
printf(\"Please input score:\"); scanf(\"%d\
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\ return 0; }
程序运行结果示例1: Please input score: -1↙
Input error!
程序运行结果示例2: Please input score: 95↙
grade: A
程序运行结果示例3: Please input score: 82↙
grade: B
程序运行结果示例4: Please input score: 72↙
grade: C
程序运行结果示例5: Please input score: 66↙
grade: D
程序运行结果示例6: Please input score: 32↙
grade: E
程序运行结果示例7: Please input score: 127↙
Input error!
输入格式: \"%d\"
输入提示信息:\"Please input score:\\n\"
输入错误提示信息:\"Input error!\\n\"
输出格式:\"grade: %c\\n\" (注意:%c前面有一个空格)
答案:
#include int main() {int score; char grade;
printf(\"Please input score:\\n\"); scanf(\"%d\
if (score < 0 || score > 100)
printf(\"Input error!\\n\"); else if (score >= 90) {
grade = 'A';
printf(\"grade: %c\\n\ }
else if (score >= 80) {
grade = 'B';
printf(\"grade: %c\\n\ }
else if (score >= 70) {
grade = 'C';
printf(\"grade: %c\\n\ }
else if (score >= 60)
{ grade = 'D';
printf(\"grade: %c\\n\ } else
{ grade = 'E';
printf(\"grade: %c\\n\ } return 0; }
-------------------------59 60.
下面程序用于输入三角形的三条边a,b,c,判断它们能否构成三角形,若能,则指出是何种三角形:等腰三角形、直角三角形、等腰直角三角形,一般三角形。若不能,则输出“不是三角形\\n”
浮点数的运算允许的误差在0.1范围内即可。
请修改下面程序,使其运行结果完全正确。只有全部错误都修改正确才给分,部分错误修改正确不给分。允许修改和增加语句,但是不允许删除语句,也不要修改变量的类型。 #include int main() {float a, b, c; int flag;
scanf(\"%d,%d,%d\
if (a+b>c || b+c>a || a+c>b); {
if (a=b||b=c||c=a) {
printf(\"等腰三角形\\n\"); flag = 0; }
else if (a*a+b*b=c*c || a*a+c*c=b*b || c*c+b*b=a*a) {
printf(\"直角三角形\\n\"); flag = 0; }
if (!flag) {
printf(\"一般三角形\\n\"); } } else {
printf(\"不是三角形\\n\"); }
return 0; }
答案:
#include int main() {float a, b, c; int flag;
scanf(\"%d,%d,%d\
if (a + b>c && b + c>a && a + c>b)
{
if ((a == b || b == c || c == a) && (a*a + b*b == c*c || a*a + c*c == b*b || c*c + b*b == a*a)) {
printf(\"等腰直角三角形\\n\"); }
else if (a == b || b == c || c == a) {
printf(\"等腰三角形\\n\"); flag = 0; }
else if (a*a + b*b == c*c || a*a + c*c == b*b || c*c + b*b == a*a)
{
printf(\"直角三角形\\n\"); flag = 1; } else {
printf(\"一般三角形\\n\"); } } else {
printf(\"不是三角形\\n\"); }
return 0; }
------------------60 61.
编写程序实现以下功能:从键盘输入一个大写英文字母,将该字母转换成小写字母后,打印输出转换后的小写字母及其所对应的ASC码值。 **输入提示信息**:\"Enter a capital letter:\\n\" **输入数据格式**:\"%c\"
**输出数据格式**:\"%c,%d\\n\"
友情提示:从键盘输入一个字符可用scanf也可用getchar
答案:
#include int main() {char a_;
printf(\"Enter a capital letter:\\n\"); scanf(\"%c\
printf(\"%c,%d\\n\ }
--------------------------------61 62.
编写程序计算圆的面积和周长。已知pi定义为3.14. **输入格式要求:\"%f\" 提示信息:\"请输入半径的值:\"
**输出格式要求:\"半径为%5.2f的圆的面积为%5.1f,圆的周长为%5.1f\\n\" 示例运行如下:
请输入圆的半径:3.5
半径为 3.50的圆的面积为 38.5,圆的周长为 22.0
答案:
#include #define pi 3.14 main() {float r, c, s;
printf(\"请输入半径的值:\"); scanf(\"%f\ c = 2*pi*r; s = pi*r*r;
printf(\"半径为%5.2f的圆的面积为%5.1f,圆的周长为%5.1f\\n\s, c); }
-------------------------62 63.
从键盘任意输入一个3位整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-123,则忽略负号,由123分离出其百位1、十位2、个位3,然后计算3*100+2*10+1 = 321,并输出321。 **输入格式要求:\"%d\" 提示信息:\"Input x:\" **输出格式要求:\"y = %d\\n\" 程序运行示例如下: Input x:-123 y = 321
答案:
#include int main(void) {int a, b, c, d, e; printf(\"Input x:\"); scanf(\"%d\ if (a < 0)
a = -a;
}
c = a / 100;
d = a % 100 / 10; e = a % 10;
b = e * 100 + d * 10 + c ; printf(\"y = %d\\n\ return 0;
--------------------------------63 64.
求输入两个数的和、差、积、商和余数(如果是浮点数,则需将其强转为整型后计算余数)。请改正程序中的错误,使它能得出正确的结果。 #include main() {
float a, b;
float sum, minus, product, quotient; int remainder;
printf(\"\\n请输入两个数:\\n\"); scanf(\"%f\\n%f\ sum = a + b; minus = a - b; product = a * b; quotient = a / b; remainder = a % b;
printf(\"和为:%.2f\\n\ printf(\"差为:%.2f\\n\ printf(\"积为:%.2f\\n\ printf(\"商为:%.2f\\n\ printf(\"余数为:%d\\n\}
答案:
#include main() {float a, b;
float sum, minus, product, quotient; int remainder;
printf(\"\\n请输入两个数:\\n\"); scanf(\"%f%f\ sum = a + b;
}
minus = a - b; product = a * b; quotient = a / b;
remainder = (int)a % (int)b; printf(\"和为:%.2f\\n\ printf(\"差为:%.2f\\n\ printf(\"积为:%.2f\\n\ printf(\"商为:%.2f\\n\ printf(\"余数为:%d\\n\
---------------------------------64 65.
已知三角形的三边长为a,b,c,计算三角形面积的公式为: area =sqrt(s(s-a)(s-b)(s-c)),s=1/2(a+b+c) 其中,a,b,c为浮点数,sqrt为开平方计算。
要求编写程序,从键盘输入a,b,c的值,计算并输出三角形的面积。 注意:不用判断输入的边长值是否满足三角形要求, 只需进行简单计算即可。
**要求输入提示信息格式为:\"Input a,b,c:\"; **要求输入数据格式:\"%f,%f,%f\" **输出格式要求:\"area=%.2f\\n\"
答案:
#include #include main() {float a, b, c,area, s; printf(\"Input a,b,c:\");
scanf(\"%f,%f,%f\ s=(a+b+c)/2;
area =sqrt(s*(s-a)*(s-b)*(s-c)); printf(\"area=%.2f\\n\ }
---------------------------------65 66.
编程实现简单的计算器功能,要求用户按如下格式从键盘输入算式: 操作数1 运算符op 操作数2
计算并输出表达式的值,其中算术运算符包括:加(+)、减(-)、乘(*)、除(/)。
**输入格式要求:\"%d%c%d\" 提示信息:\"Please enter an expression:\"
**输出格式要求:\"%d + %d = %d \\n\" \"%d - %d = %d \\n\" \"%d * %d = %d \\n\" \"%d / %d = %d \\n\" \"Division by zero!\\n\" \"Invalid operator! \\n\"
程序运行示例1如下:
Please enter an expression:22+12 22 + 12 = 34
程序运行示例2如下:
Please enter an expression:22-12 22 - 12 = 10
程序运行示例3如下:
Please enter an expression:22*12 22 * 12 = 264
程序运行示例4如下:
Please enter an expression:22/12 22 / 12 = 1
程序运行示例5如下:
Please enter an expression:22/0 Division by zero! 程序运行示例6如下:
Please enter an expression:22\\12 Invalid operator!
答案:
#include #include int main() {int a, b; char c;
printf(\"Please enter an expression:\"); scanf(\"%d %c %d\ if (c == '+')
printf(\"%d + %d = %d \\n\ else if (c == '-')
printf(\"%d - %d = %d \\n\ else if (c == '/') {
if (b != 0)
printf(\"%d / %d = %d \\n\ else
printf(\"Division by zero!\"); }
else if (c == '*' || c == 'x' || c == 'X') printf(\"%d * %d = %d \\n\ else if (c == '^')
printf(\"%d ^ %d = %d \\n\ else
printf(\"Invalid operator! \\n\");
}
---------------------------------66 67.
计算n!算法如下,请改正程序中的错误,使它能得出正确的结果。 #include long fact(int n);
main() {
int n, result = 0;
printf(\"Input n:\"); scanf(\"%d\
result = fact(n);
printf(\"%d != %d\ }
long fact(int n) {
int result; if (n < 0)
printf(\"n<0,data error!\\n\"); else {
result = n * fact(n - 1); return result; } }
答案:
#include long fact(int n); main() {int n, result = 0; printf(\"Input n:\"); scanf(\"%d\ if (n < 0)
printf(\"n<0,data error!\\n\"); else {
result = fact(n); }
printf(\"%d != %d\ }
long fact(int n) {
if (n == 0 ||n ==1) return 1; else {
return fact(n - 1)*n; } }
---------------------------------67 68.
用递归法将一个整数n转换成字符串,例如输入483,应输出字符串“483”。n的位数不确定,可以是任意位数的整数。 **输入提示:\"\\n输入整数:\" **输入格式:\"%d\"
**输出提示:\"\\n输出是:\" **输出转换的字符串
程序的运行示例如下: 输入整数:345 输出是:345
答案:
#include void convert(int n);
void main() {
int num;
printf(\"\\n输入整数:\"); scanf(\"%d\ printf(\"\\n输出是:\"); convert(num); }
void convert(int n)
{ }
if (n / 10 == 0)
{
putchar('0' + n % 10);
}
else
{
convert(n / 10);
putchar('0' + n % 10);
}
---------------------------------68 69.
假设有40个学生被邀请来给餐厅的饮食和服务质量打分,分数划分为1~10这10个等级(1表示最低分,10表示最高分),编程统计并按如下格式输出餐饮服务质量调查结果。 Grade Count Histogram 1 5 ***** 2 10 ********** 3 7 ******* ...
**输入格式要求:\"%d\" 提示信息:\"Input the feedbacks of 40 students:\\n\" \"input error!\\n\"
**输出格式要求:\"Feedback\Count\Histogram\\n\" \"%8d\%5d\\" 程序运行示例如下:
Input the feedbacks of 40 students: 10 9 10 8 7 6 5 10 9 8 8 9 7 6 10 9 8 8 7 7 6 6 8 8 9 9 10 8 7 7 9 8 7 9 7 6 5 9 8 7 Feedback Count Histogram 1 0 2 0
3 4 5 6 7 8 9 10
答案:
0 0 2 5 9 10 9 5 ** ***** ********* ********** ********* *****
#include main() {int a[10] = {0}, n, i;
printf(\"Input the feedbacks of 40 students:\\n\"); for (i = 1; i <= 40; i++) {
scanf(\"%d\ if(n == 1) a[0]++;
else if(n == 2) a[1]++;
else if(n == 3) a[2]++;
else if(n == 4) a[3]++;
else if(n == 5) a[4]++;
else if(n == 6) a[5]++;
else if(n == 7) a[6]++;
else if(n == 8) a[7]++;
else if(n == 9) a[8]++;
else if(n == 10) a[9]++; else
printf(\"input error!\\n\"); }
printf(\"Feedback\Count\Histogram\\n\"); for(i = 0; i < 10;i++) {
printf(\"%8d\%5d\\
}
for(n = 1; n <= a[i];n++) printf(\"*\");
printf(\"\\n\"); }
----------------------------69 70.
编程比较用户键盘输入的口令与内设的口令secret是否相同。若相同,则输出\"Correct password! Welcome to the system...\",若passwordpassword\"。 输入提示信息:\"Input Password:\" 输入格式:\"%s\" 输出提示信息:\"Correct password! Welcome to the system...\\n\" \"Invalid password!user inputpassword\\n\"答案:
#include #include int main() {
char password[7] = \"secret\";
char userInput[81];
printf(\"Input Password:\");
scanf(\"%s\
}
if (
strcmp(userInput, password) == 0
)
printf(\"Correct password! Welcome to the system...\\n\");
else if (
strcmp(userInput, password)<0
)
printf(\"Invalid password!user inputelseprintf(\"Invalid password!user input>password\");
return 0;
----------------------------70
71.
编写一个C函数,实现保留小数点后第N-1位,从第N位四舍五入。用该函数对数组a[5]中的各元素从小数点后第2位开始四舍五入。 a[5]={2.33,2.56,2.65,2.66,2.30}
**输出格式要求:\"Old array:\\n\" \"a[%d]=%.3f\\" \"\\nnew array:\\n\" \"a[%d]=%.2f\\"
程序运行示例如下: Old array:
a[0]=2.330 a[1]=2.560 a[2]=2.650 a[3]=2.660 a[4]=2.300
new array:
a[0]=2.30 a[1]=2.60 a[2]=2.70 a[3]=2.70 a[4]=2.30
答案:
#include #includefloat fife(float a,int n); main() {
int i;
float a[5]={2.33,2.56,2.65,2.66,2.30}; printf(\"Old array:\\n\"); for(i = 0;i < 5;i++) {
printf(\"a[%d]=%.3f\\ }
printf(\"\\nnew array:\\n\"); for(i = 0;i < 5;i++) {
printf(\"a[%d]=%.2f\\ } }
float fife(float a, int n ) {
int b, c;
c = a * pow(10,n); b = c%10; if(b >= 5) {
a = (int)(a * pow(10,n - 1)) + 1; a = a/10; return a; }
}
else {
a = (int)(a * pow(10,n - 1)); a = a/10; return a; }
-----------------------------71 72.
求最大数和最小数的最大公约数
从键盘输入10个正整数,求出最大数,最小数,以及他们的最大公约数。要求用数组实现。
程序运行结果示例1: Input 10 numbers:
15 23 56 87 94 105 78 19 22 43↙ maxNum=105 minNum=15 15
程序运行结果示例2: Input 10 numbers:
33 1 2 9 8 7 5 4 0 10↙ maxNum=33 minNum=0
输入格式: \"%d\" 输出格式:
输入提示信息:\"Input 10 numbers:\\n\" 最大数输出格式:\"maxNum=%d\\n\" 最小数输出格式:\"minNum=%d\\n\" 最大公约数输出格式:\"%d\"
答案:
#include main() {int a[10] = {0}, i, j, min = 0, max = 0; printf(\"Input 10 numbers:\\n\"); for(i = 0; i < 10; i++) {
scanf(\"%d\ if(0 == i) {
}
min = a[i]; max = a[i]; } else {
if(a[i] >= max) max = a[i]; if(a[i] <= min) min = a[i]; } }
printf(\"maxNum=%d\\n\ printf(\"minNum=%d\\n\ for(i = min;1 <= i; i--) {
if((min % i == 0) &&(max % i == 0)) {
printf(\"%d\ break; } }
-----------------------------72 73.
写一个函数days,实现主函数将年、月、日(结构体类型)传递给days函数,days函数计算该年该月该日是该年的第几天并传回主函数输出。 程序的运行示例如下: 请输入日期(年,月,日) 1990,2,14
2月14日是1990年的第45天。
答案1
#include struct time_1 {int year; int month; int day; };
int days(struct time_1 Atime); int main() {
struct time_1 Atime; int a, b, c,d;
printf(\"请输入日期(年,月,日)\\n\\n\"); scanf(\"%d,%d,%d,\ Atime.year = a; Atime.month = b; Atime.day = c; d = days(Atime);
printf(\"%d月%d日是%d年的第%d天。\ return 0; }
int days(struct time_1 Atime) {
int _day;
if (Atime.year % 4 == 0) {
if (Atime.month == 1)
_day = Atime.day; else if (Atime.month == 2)
_day = Atime.day + 31; else if (Atime.month == 3)
_day = Atime.day + 60; else if (Atime.month == 4)
_day = Atime.day + 91; else if (Atime.month == 5)
_day = Atime.day + 121; else if (Atime.month == 6)
_day = Atime.day + 152; else if (Atime.month == 7)
_day = Atime.day + 182; else if (Atime.month == 8)
_day = Atime.day + 213; else if (Atime.month == 9)
_day = Atime.day + 244; else if (Atime.month == 10)
_day = Atime.day + 274; else if (Atime.month == 11)
_day = Atime.day + 305; else if (Atime.month == 12)
_day = Atime.day + 335; }
if (Atime.year % 4 != 0) {
if (Atime.month == 1)
_day = Atime.day; else if (Atime.month == 2)
}
_day = Atime.day + 31; else if (Atime.month == 3)
_day = Atime.day + 60 - 1; else if (Atime.month == 4)
_day = Atime.day + 91 - 1; else if (Atime.month == 5)
_day = Atime.day + 121 - 1; else if (Atime.month == 6)
_day = Atime.day + 152 - 1; else if (Atime.month == 7)
_day = Atime.day + 182 - 1; else if (Atime.month == 8)
_day = Atime.day + 213 - 1; else if (Atime.month == 9)
_day = Atime.day + 244 - 1; else if (Atime.month == 10)
_day = Atime.day + 274 - 1; else if (Atime.month == 11)
_day = Atime.day + 305 - 1; else if (Atime.month == 12)
_day = Atime.day + 335 - 1; }
return _day;
答案2
#include struct time_1 { };int days(struct time_1 Atime); int main() {
struct time_1 Atime; int a, b, c,d;
scanf(\"%d,%d,%d,\", &a, &b, &c); if (a % 4 == 0) {
if ((2 == b) && (c > 29)) {
printf(\"你输入的日期有误!\");
int year; int month; int day;
o: printf(\"请输入日期(年,月,日)\");
}
}
}
goto o;
else if ((1 == b || 3 == b || 5 == b || 7 == b || 8 == b || 10 == b || 12 == b) { }
else if (c > 30) { }
printf(\"你输入的日期有误!\"); goto o;
printf(\"你输入的日期有误!\"); goto o;
&& (c > 31))
else { }
Atime.year = a; Atime.month = b; Atime.day = c; d = days(Atime);
printf(\"%d月%d日是%d年的第%d天。\", b, c, a, d); return 0;
if ((2 == b) && (c > 28)) { }
else if ((1 == b || 3 == b || 5 == b || 7 == b || 8 == b || 10 == b || 12 == b) { }
else if (c > 30) { }
printf(\"你输入的日期有误!\"); goto o;
printf(\"你输入的日期有误!\"); goto o;
printf(\"你输入的日期有误!\"); goto o;
&& (c > 31))
int days(struct time_1 Atime) {
int _day;
if (Atime.year % 4 == 0)
{ } else {
if (Atime.month == 1)
_day = Atime.day; _day = Atime.day + 31; _day = Atime.day + 60 - 1; _day = Atime.day + 91 - 1; _day = Atime.day + 121 - 1; _day = Atime.day + 152 - 1; _day = Atime.day + 182 - 1; _day = Atime.day + 213 - 1; else if (Atime.month == 2) else if (Atime.month == 3) else if (Atime.month == 4) else if (Atime.month == 5) else if (Atime.month == 6) else if (Atime.month == 7) else if (Atime.month == 8) if (Atime.month == 1)
_day = Atime.day; _day = Atime.day + 31; _day = Atime.day + 60; _day = Atime.day + 91; _day = Atime.day + 121; _day = Atime.day + 152; _day = Atime.day + 182; _day = Atime.day + 213; _day = Atime.day + 244; _day = Atime.day + 274; _day = Atime.day + 305; _day = Atime.day + 335; else if (Atime.month == 2) else if (Atime.month == 3) else if (Atime.month == 4) else if (Atime.month == 5) else if (Atime.month == 6) else if (Atime.month == 7) else if (Atime.month == 8) else if (Atime.month == 9) else if (Atime.month == 10) else if (Atime.month == 11) else if (Atime.month == 12)
}
}
else if (Atime.month == 9)
_day = Atime.day + 244 - 1; _day = Atime.day + 274 - 1; _day = Atime.day + 305 - 1; _day = Atime.day + 335 - 1; else if (Atime.month == 10) else if (Atime.month == 11) else if (Atime.month == 12)
return _day;
-----------------------------73 74.
马克思手稿中有一道趣味数学题:有30个人,其中可能有男人、女人和小孩,在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,问男人、女人和小孩各有几人?
假设男人为x人,女人为y人,小孩为z人,则有以下方程组:
{x+y+z=303x+2y+z=50
main() {
}
答案:
int x,y,z;
printf(\"Man \ Women \ Childern\\n\"); for (x=0; x<16; x++)
for (y=0; y<25; y++) {
z = 30 – x - y;
if (3 * x + 2 * y + z = 50)
printf(\"%3f\ %5f\ %8f\\n\ }
#include int main() {int x, y, z;
printf(\"Man \ Women \ Childern\\n\"); for (x = 1; x<16; x++)
for (y = 1; y<25; y++) {
z = (30 - x) - y;
if ((3 * x + 2 * y + z) == 50)
printf(\"%3d\ %5d\ %8d\\n\ } return 0; }
-----------------------------74 75.
程序:计算本金和复利之和。 编写程序打印出一个表格,这个表格显示了在几年时间内100美金投资在不通利率上的价值。用户将输入利率和要投资的年数。假设整合利息一年一次,表格将显示出一年间在此输入利率下河后边四个更高利率下投资的价值。
**输入格式要求:\"%d\" 提示信息:\"Enter interest rate:\" \"Enter number of year:\"
**输出格式要求:\"\\nyears\" \"%6d%%\" \"%3d \" \"%7.2f\" 下面是程序运行时的情况: Enter intereset rate: 6 Enter number of years: 5
years 6% 7% 8% 9% 10% 1 106.00 107.00 108.00 109.00 110.00 2 112.36 114.49 116.64 118.81 121.00 3 119.10 122.50 125.97 129.50 133.10 4 126.25 131.08 136.05 141.16 146.41 5 133.82 140.26 146.93 153.86 161.05
答案:
#include int main() {int i, y, r, j,k; float a[5], b = 100;
printf(\"Enter interest rate:\"); scanf(\"%d\", &r);
printf(\"Enter number of year:\"); scanf(\"%d\", &y); printf(\"\\nyears\");
for (i = r; i <= (r + 4);i++) { }
for (i = 1;i <= y;i++) {
printf(\"\\n%3d\\", i); k = r;
printf(\"%6d%%\", i);
}
}
for (j = 1;j <= 5;j++) { }
b = ((float)r / 100) * b + b;
printf(\"%7.2f\", ((float)k/ 100)*b + b); k++;
return 0;
-----------------------------75 76.
给定如下定义: struct date_rec {
int day ; int month ; int year ; };
写一个函数接收两个日期,如果两个日期相同,则返回0,如果第一个日期晚于第二个日期,则返回1,如果第一个日期早于第二个日期,则返回-1。并编写main函数测试它。
**输入格式要求:\"%d%d%d\" 提示信息:\"请输入当前日期(年 月 日):\" **输出格式要求:\"当前日期:%d年%d月%d日!\\n\" \"第一个日期晚于第二个日期!\" \"第一个日期早于第二个日期!\" \"两个日期相同!\"
程序运行示例如下:
请输入当前日期(年 月 日):2012 9 10 请输入当前日期(年 月 日):2013 7 10
当前日期:2012年9月10日! <=== 输出 当前日期:2013年7月10日! 第一个日期早于第二个日期!
答案:
#include struct date_rec {int day; int month; int year; };
int time_1(struct date_rec _time1, struct date_rec _time2); int main()
{
struct date_rec _time1, _time2;
printf(\"请输入当前日期(年 月 日):\"); printf(\"请输入当前日期(年 月 日):\");
scanf(\"%d%d%d\ printf(\"当前日期:%d年%d月%d日!\\n\_time1.month,_time1.day);
scanf(\"%d%d%d\ printf(\"当前日期:%d年%d月%d日!\\n\_time2.year, _time2.month, _time2.day);
if (0 == time_1(_time1, _time2)) printf(\"两个日期相同!\");
else if (-1 == time_1(_time1, _time2))
printf(\"第一个日期早于第二个日期!\"); else if (1 == time_1(_time1, _time2))
printf(\"第一个日期晚于第二个日期!\"); }
int time_1(struct date_rec _time1, struct date_rec _time2) {
if (_time1.year > _time2.year) return 1;
else if (_time1.year == _time2.year) {
if (_time1.month > _time2.month) return 1;
else if (_time1.month == _time2.month) {
if (_time1.day > _time2.day) return 1;
else if (_time1.day == _time2.day) return 0; else
return -1; } else
return -1; } else
return -1; }
评论
-----------------------------76 77.
写一个函数,如果它首次被调用,则返回字母A,第二次被调用,则返回字母B,第三次调用,则返回字母C,以此类推。(提示:使用一个static数据类型) 函数原型为:char call_times(void)。 编写main函数测试它。
**输出格式要求:\"第一次调用返回字符%c!\\n\" \"第二次调用返回字符%c!\\n\" \"第三次调用返回字符%c!\\n\"
答案:
#includechar call_times(void); char achar; int main() {
achar = 'A' - 1;
printf(\"第一次调用返回字符%c!\\n\ printf(\"第二次调用返回字符%c!\\n\ printf(\"第三次调用返回字符%c!\\n\ return 0; }
char call_times(void) {
achar = achar + 1; return achar; }
-----------------------------77 78.
编程将字符串s倒序输出,要求利用函数递归实现。
**输入格式要求:\"%s\" 提示信息:\"input your string:\\n\" **输出格式要求:\"%c\" 程序运行的输入输出样例: 屏幕先输出提示信息: input your string: 然后用户键盘输入: abcdefg 最后屏幕输出: gfedcba
答案:
#include char str(char b); int i = 0;char a[80] = { 0 }; int main() {
printf(\"input your string:\\n\"); scanf(\"%s\ str(*a); }
char str(char b) {
if (b != '\\0') {
i++;
str(*(a + i)); printf(\"%c\ } else
return '\\0'; }
-----------------------------78 79.
#include 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\x = Factorial(n);
printf(\"%d!=%ld\\n\
#include unsigned long Factorial(unsigned int n) {
if (n == 0 || n == 1) {
return 1; } else {
return n * Factorial(n - 1); } }
int main() {
int n;
unsigned long x;
printf(\"Input n:\\n\"); scanf(\"%d\ if(n >= 0) {
printf(\"%d!=%ld\\n\ } else
printf(\"data error!\"); return 0; }
-----------------------------79 80.
从键盘输入10个整数,用函数编程实现计算其最大值和最小值,并互换它们所在数组中的位置。
要求按如下函数原型编写程序 void ReadData(int a[], int n); void PrintData(int a[], int n);
void MaxMinExchang(int a[], int n); **输入提示信息要求: \"Input n(n<=10):\\n\" \"Input %d numbers:\\n\"
**要求输入格式为:\"%d\"
**输出提示信息:\"Exchange results:\" **要求输出格式为:\"%5d\"
输出数组中所有数据后换行
注:不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程,主函数不能使用int main和return 0。
答案:
#include main() {int a[10] = { 0 }, j, i = 0, k = 0; int c, b, n;
printf(\"Input n(n<=10):\\n\"); scanf(\"%d\
printf(\"Input %d numbers:\\n\ for (j = 0; j < n; j++) {
scanf(\"%d\ if (a[j] > i) {
i = a[j]; c = j; }
if (j == 0) {
k = a[j]; b = j; }
else if (a[j] < k) {
k = a[j]; b = j; } }
a[c] = k; a[b] = i;
printf(\"Exchange results:\"); for(j = 0; j < n; j++) printf(\"%5d\ }
-----------------------------80 81
按如下函数原型编写程序,用一个整型数组feedback保存调查的40个反馈意见。用函数编程计算反馈意见的平均数(Mean)、中位数(Median)和众数(Mode)。中位数指的是排列在数组中间的数。如果原始数据的个数是偶数,那么中位数等于中间那两个元素的算术平均值。众数是数组中出现次数最多的那个数(不考虑两个或两个以上的反馈意见出现次数相同的情况)。 int Mean(int answer[], int n); int Median(int answer[], int n); int Mode(int answer[], int n); void DataSort(int a[], int n);
输入提示信息:\"Input the feedbacks of 40 students:\\n\" 输入格式:\"%d\"
输出提示信息和输出格式: \"Mean value = %d\\n\" \"Median value = %d\\n\" \"Mode value = %d\\n\"
.答案:
#include int a[40] = { 0 };void DataSort(int a[], int n); int Mean(int answer[], int n); int Median(int answer[], int n); int Mode(int answer[], int n); int main() { }
//---------------------------------------------- void DataSort(int a[], int n) {
int j, t, i;
for (i = 0;i < n; i++)
for (j = 1;j < n; j++) int i = 0;
printf(\"Input the feedbacks of 40 students:\\n\"); do {
scanf(\"%d\", &a[i]); i++;
} while (i < 40); DataSort(a, 40);
printf(\"Mean value = %d\\n\", Mean(a, 40)); printf(\"Median value = %d\\n\", Median(a, 40)); printf(\"Mode value = %d\\n\", Mode(a, 40)); return 0;
}
if (a[j] < a[j - 1]) { }
t = a[j - 1]; a[j - 1] = a[j]; a[j] = t;
//----------------------------------------------- int Median(int answer[], int n) { }
//----------------------------------------------- int Mean(int answer[], int n) { }
//----------------------------------------------- int Mode(int answer[], int n) {
int o, p, m[40], k[40]; int c,i,j;
for (o = 0;o < n; o++) {
if (k[j] > i) { }
if (j == 0) {
i = k[j]; c = j;
for (p = 0; p < n; p++) { }
if (answer[o] == answer[p]) { }
m[o] = answer[p]; k[o]++;
int i = 0, d = 0; for (i = 0;i < n; i++)
d = d + answer[i]; return d / n;
return (answer[n / 2 - 1] + answer[n / 2]) / 2;
for (j = 0; j < 40; j++)
}
}
}
o = a[j]; p = j;
else if (k[j] < o) { }
o = k[j]; p = j;
return answer[c];
-----------------------------81 82.
从键盘任意输入10个整数存入一个数组中,然后任意输入一个整数x,采用顺序查找法,在数组中查找该数,要求按如下函数原型编程实现查找功能。 int Search(int a[], int n, int x);
在主函数中调用函数Search顺序查找x,然后在主函数中打印查找结果。 如果找到,则函数返回该数在数组中的下标位置,并在主函数中打印该值; 如果没有找到,则返回-1,并在主函数中打印“Not found!”。 要求必须按照题目要求和用函数编程,否则不给分。 **要求输入10个整数的提示信息格式为:
\"Input 10 numbers:\\n\"(每输入一个数,键一次回车); **要求输入整数x的提示信息格式为: \"Input x:\\n\"
**要求输出格式为:
找到时的打印格式为\"Subscript of x is %d\\n\" 没找到时的打印格式为\"Not found!\\n\"
注:不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程,主函数不能使用int main和return 0。
答案:
#includeint search(int a[], int n, int x); int main() {
int a[10] = { 0 }, n, x;
printf(\"Input 10 numbers:\\n\"); for (n = 0; n < 10; n++) scanf(\"%d\ printf(\"Input x:\\n\"); scanf(\"%d\
n = search(a, n, x); if (n == -1)
printf(\"Not found!\"); else
printf(\"Subscript of x is %d\\n\ return 0; }
int search(int a[], int n, int x) {
int i;
for (i = 0; i < n; i++) if (a[i] == x) return i; return -1; }
用函数编程统计平均成绩。要求在主函数中输入学生成绩,输入负数时,结束输入,调用子函数Average()计算平均成绩,并输出结果。 函数原型:int Average(int score[],int n) ****要求输入提示信息为:\"Input score:\" ****输出格式要求为:
\"Total students are %d\\n\" \"Average score is %d\\n\"
答案:
#includeint Average(int score[],int n); int main() {
int a[100] = {0}, i , j = 0 ; for (j = 0;j < 100;j++) {
printf(\"Input score:\"); scanf(\"%d\ if(i >= 0) a[j] = i; else break; }
printf(\"Total students are %d\\n\
printf(\"Average score is %d\\n\ Average(a,j); }
//--------------------------------- int Average(int score[],int n) {
int j,i = 0;
for(j = 0; j < n; j++) {
i = i + score[j];
}
return i/n; }
-----------------------------82 83.
请编写一个查找子字符串的程序,并统计子字符串出现的次数。
**输入格式要求:\"%s\" 提示信息:\"请输入主串:\" \"请输入要查找的串:\" **输出格式要求:\"%s,%s:\" \"子串出现的次数:%d\\n\" \"子串不在主串中\\n\" 程序运行示例1如下: 请输入主串:Hello,world! 请输入要查找的串:l
Hello,world!,l:子串出现的次数:3 程序运行示例2如下: 请输入主串:Hello,world! 请输入要查找的串:abc
Hello,world!,abc:子串不在主串中
答案:
#include int main() {int i, j, a = 0, b = 0,c = 0;
char str1[100] = {0},str2[100] = {0}; printf(\"请输入主串:\"); scanf(\"%s\
printf(\"请输入要查找的串:\"); scanf(\"%s\
for(i = 0;str1[i] != '\\0' ;i++) a++;
for(i = 0;str2[i] != '\\0';i++ ) b++;
for(i = 0; i < a; i++) {
for(j = 0; str1[j + i] == str2[j]; j++); if(j == b) c++; } if(c)
printf(\"%s,%s:子串出现的次数:%d\\n\ else
printf(\"%s,%s:子串不在主串中\\n\ return 0; }
-----------------------------83 84.
不用标准库函数strcmp,自己编写一个函数MyStrcmp 实现将两个字符串s和t进行比较,
然后将s和t中第一个不相同字符的ASCII码值之差作为函数值返回。 要求如下:
(1)函数MyStrcmp函数原型为:
int MyStrcmp(char s[],char t[]) (2)在主函数中
从键盘分别输入两个字符串s和t, (每个字符串的最大长度为80个字符) 然后调用MyStrcmp函数,
接下来,判定该函数的返回值:
如果大于0,输出\"string s>string t.\\n\" 如果小于0,输出\"string s输入s之前提示:\"Input s\\n\" 输入t之前提示:\"Input t\\n\"答案:
#includeint Mystrcmp(char s[],char t[]) {
int i,j;
for(i = 0;i < 80; i++) if(s[i] == t[i]) continue;
else if(s[i] > t[i]) return s[i] - t[i]; else
return s[i] - t[i]; return 0; }
int main() {
char s[80] = {0},t[80] = {0}; printf(\"Input s\\n\"); gets(s);
printf(\"Input t\\n\"); gets(t);
if(Mystrcmp(s,t) == 0)
printf(\"string s=string t.\\n\"); else if(Mystrcmp(s,t) < 0)
printf(\"string sprintf(\"string s>string t.\\n\"); }-----------------------------84 85.
下列给定程序中,函数fun()的功能是:从s所指字符串中,找出t所指字符串的个数作为函数值返回。如,当s所指字符串中的内容为abcdabfab,t所指字符串的内容为ab,则函数返回整数3。请改正程序中的错误,使它得出正确的结果:
#include int fun(char *s, char *t) {
int n;
char *p, *r; n = 0; while (*s) {
p = s; r = t;
while (*r)
if (*r == *p) {
r++; p++ }
else break; if (r == '\\0') n++; s++; }
return n; }
main() {
char s[100], t[100]; int m;
printf(\"\\nPlease enter string s: \"); scanf(\"%s\
printf(\"\\nPlease enter substring t:\");
scanf(\"%s\ m = fun(s, t);
printf(\"\\nThe result is: m=%d\\n\}
答案:
#include int fun(char *s, char *t) {
int n;
char *p, *r; n = 0;
while (*s) {
p = s; r = t;
while (*r)
if (*r == *p) {
r++; p++; }
else break; if (*r == '\\0') n++; s++; }
return n; }
main() {
char s[100], t[100]; int m;
printf(\"\\nPlease enter string s: \"); scanf(\"%s\
printf(\"\\nPlease enter substring t:\"); scanf(\"%s\ m = fun(s, t);
printf(\"\\nThe result is: m=%d\\n\ }
-----------------------------85
86.
从键盘输入某单位职工的月收入(最多不超过30人),具体人数由键盘输入。试编程输出该单位职最高月收入和最低月收收。
输出格式:printf(\"min=%10.2f,max=%10.2f\\n\要求:利用函数计算n名职工的最高和最低月收入。
函数原型:void find_MaxMin(float income[],int n,float *pmax,float *pmin);
答案:
#includevoid find_MaxMin(float income[], int n) {
int i;
float Max, Min;
for (i = 0; i < n;i++) {
scanf(\"%f\ }
Max = Min = income[0]; for (i = 0; i < n;i++) {
if (income[i] > Max)
Max = income[i]; else if (income[i] < Min) Min = income[i]; else
continue; }
printf(\"min=%10.2f,max=%10.2f\\n\ }
int main() {
int n;
float income[30] = { 0 }; scanf(\"%d\ find_MaxMin(income, n); return 0; }
-----------------------------86 87.
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 求1/1+1/3+...+1/n(要求利用函数指针编程) 程序运行示例1如下: 10
Even=1.141666
程序运行示例2如下: 9
Odd=1.787302
答案:
#include void E(int n); void O(int n); int main() {int n;
scanf(\"%d\ if (n % 2) O(n); else
E(n); return 0; }
void E(int n) {
int i;
float Even = 0;
for (i = 1;i <= n / 2;i++)
Even = Even + (float)1 / (2 * i); printf(\"Even=%f\\n\ }
void O(int n) {
int i;
float Odd = 0;
for (i = 1;i <= n; i = i + 2)
Odd = Odd + (float)1 / i; printf(\"Odd=%f\\n\ }
-----------------------------87 88.
给下面程序改错。程序功能是输入10个数,按从小到大顺序排序。 #include #define SIZE 10; main( ){ int i, j, t, a[SIZE];
printf(\"input 10 numbers: \\n\"); for(i = 1;i <= SIZE;i++)
scanf(\"%d\ printf(\"\\n\");
for (i = 0; i< SIZE; i++)
for (j = SIZE - 1; j >= i + 1; j--) if (a[j] > a[j-1]) { t=a[j]; a[j-1]=a[j]; a[j]=t; }
for (i = 0; i < SIZE; i++) printf(\"%d\\n\ }
注意:
(1)请将修改正确后的完整源程序拷贝粘贴到答题区内。
(2)对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。 (3)当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。 (4)改错时不能改变程序原有的意图,也不要改变代码的输入输出格式。
答案:
#include