C语言练习题(山东科技大学吐血整理) 下载本文

数学11-1 C语言平时训练题

1、算术基本运算

Description

计算两整数x和y(0

输入只有一行。 Output

输出为多行,按顺序每行输出x,y的和、差、积、商、余数、x的平方和y的三次方。 Sample Input x = 11, y = 3

Sample Output x + y : 14 x - y : 8 x * y : 33

x / y quotient: 3, remainder: 2 x ^ 2 : 121 y ^ 3 : 27 Answer

#include int main() { int x,y,a,b,c,d,e,f,g; 0

2、求圆的面积和周长

Description

从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。 Input

输入一个浮点型数据,有效数字不会超过十进制的6位。 Output

输出为两行。

第一行为圆的面积,第二行为圆的周长,格式见sample。 Sample Input 3

Sample Output Area: 28.260000 Perimeter: 18.840000

Answer

#include #define PI 3.14 int main() { float r,s,c; scanf(\ s=PI*r*r; c=2*PI*r; printf(\ printf(\ return 0; }

3、 平均值

Description

求3个数的平均值。 Input

输入只有一行,为3个较小的整数。 Output

输出为这3个整数的平均值,保留3位小数。 Sample Input 1 2 3

Sample Output 2.000 Answer

#include int main() { int a,b,c; float d; scanf(\ d=(a+b+c)/3.0; printf(\ return 0; }

4、货币兑换

Description

给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币能兑换成人民币的金额。 要计算的外币有三种:美元、欧元、日元。 Input

输入有三行。

第一行依次为美元、欧元、日元外币汇率,用空格分开。汇率用100外币为单位,精确到小数点后4位,如668.5200表示“100美元=668.5200人民币”。汇率浮动范围为(0,10000)。 第二行为外币金额x,第三行为人民币金额y。x,y均为整数,且0

输出为两行。

第一行为金额为x的美元、欧元、日元兑换成人民币的金额,用空格分开。 第二行为金额为y的人民币兑换成美元、欧元、日元的金额,用空格分开。 所有金额精确到小数点后两位。 Sample Input

668.5200 908.0685 7.9852 1500 1500

Sample Output

10027.80 13621.03 119.78 224.38 165.19 18784.75 Answer

#include int main() { double x,y,a,b,c,d,e,f,g,h,i; scanf(\ scanf(\ scanf(\ d=x/100*a; e=x/100*b; f=x/100*c; g=y/a*100; h=y/b*100; i=y/c*100; printf(\ printf(\ return 0; }

5、 求字符的值

Description

从键盘输入3个字符(不含双字节字符),分别输出每个字符的十进制值(ASCII码)、八进制值和十六进制值。 Input

输入为3个字符。 Output

输出为3行。

每一行为每个字符(对应输入顺序)的十进制、八进制和十六进制值,用空格分隔开。每个输出的值占3个字符,不足3个字符前面补0。 Sample Input 0 A

Sample Output 048 060 030 032 040 020 065 101 041 Answer

#include int main() {

char a,b,c;

scanf(\ printf(\ printf(\ printf(\ return 0; }

6、 奇数还是偶数?

Description

输入一个整数,判读它是奇数还是偶数。 Input

输入只有一行,为一个100以内的正整数。 Output

输出为一行。

若输入为偶数则输出“even”,奇数输出“odd”。

Sample Input 30

Sample Output even Answer

#include int main() {

int a;

scanf(\ if(a>=0&&a<=100)

{

if (a%2==0)

printf(\ else

printf(\ } else printf(\ return 0; }

7、绝对值

Description

求整型数据和浮点型数据的绝对值。 Input

输入两个数,第一个是整数,第二个是浮点数。 Output

输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。 Sample Input -1 1

Sample Output 1 1

Answer

#include #include #include int main() {

int a,c; double b,d;

scanf(\ c=abs(a); d=fabs(b);

printf(\ return 0; }

8、简单的打折计算

Description

商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单位:元),精确到分。 Input

输入只有一行,三个整数m、n和x,且0

Output

输出金额,精确到分。 Sample Input 95 300 4

Sample Output 334.40 Answer

#include int main() {

int m,x,n,a; float b;

scanf(\ 0

mn)

b=0.88*a; else

b=a;

printf(\ return 0; }

9、 判断闰年

Description

输入一个正整数的年份,判断是否为闰年。 Input

输入只有一行,为一个10000以内的正整数。 Output

输出为一行。

若输入为闰年偶数则输出“Yes”,否则输出“No”。 Sample Input 2010

Sample Output No 答案

#include int main() {

int a;

scanf(\ if (a>0&&a<10000) { if (a%4==0&&a0!=0) printf(\ else if (a@0==0) printf(\ else

printf(\ } else printf(\ return 0; }

10、 水仙花数

Description

如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153。 Input

一个整数x,100<=x<=999。 Output

x是水仙花数,则输出“YES”,否则为“NO”。 Sample Input 153

Sample Output YES Answer

#include int main() {

int a,b,c,d,e; scanf(\ b=a/100;

c=(a-b*100)/10; d=(a-b*100-c*10);

e=b*b*b+c*c*c+d*d*d; if(a==e)

printf(\ else

printf(\ return 0; }

11、 三个数比较大小

Description

从键盘上输入0~100之间的三个数,按从小到大的顺序输出。 Input

输入只有一行,为三个整数。 Output

按从小到大输出这三个数。 Sample Input 15 10 20

Sample Output 10 15 20 Answer

#include int main() { int a,b,c; scanf(\ if (a>=b) { if (b>=c) printf(\ else if (a>c) printf(\ else printf(\ } else {

}

if (a>=c) printf(\ else if (b>=c) printf(\ else printf(\}

return 0;

12、 输出整数的最低两位

Description

把一个整数的最低两位打印出来,不输出整数的符号。 Input

输入为一个整数n,不会超出int类型的数据范围。 Output

输出n的最低两位数字。但是,输入的数字本身不足两位时,不应当补0。如,输入为“1”,则输出为“1”。 Sample Input -102

Sample Output 02

Answer

#include int main() { int a,b,c; scanf(\ if(a>=100) { b=a-a/100*100; printf(\ } else if(a>=0) { printf(\ } else if(a>=-99) { printf(\ } else { c=-a; b=c-c/100*100; printf(\ } return 0; }

13、判断奇偶数(填空)

Description

编写一个程序,判断读取的正整数的奇偶性,部分程序已经给出,请填上空白语句,并提交填充后的完整程序。 程序(含答案): #include int main() {

int num;

scanf(\ if (num%2==0) printf(\是一个偶数 else

printf(\是一个奇数 return 0; }

14、求分段函数的值(填空)

Description

设有分段函数如下:

给出N>0个x的值,求对应的y值并输出。

部分程序已经给出,请填充其中的空白语句,并提交填充后的完整程序。 程序(含答案): #include #include int main() {

double x,y; int i,N;

scanf(\ for (i=0;i

scanf(\ if (x<0) y=-x; else if (x<1) y=sin(2*x); else if (x<5)

y=sqrt(x*x*x+x); else

y=2*x+10; if (i==0)

printf(\ else

printf(\ }

return 0; }

15、输出是m的倍数或n的倍数、但不是m和n的公倍数的数

Description

输出1~k之间是m的倍数或n的倍数、但不是m和n的公倍数的数,其中1<=m,n

输入三个整数,依次为k、m、 n。 Output

从小到大输出符合题意的所有整数,两数之间用一个空格分开。 Sample Input 15 2 3

Sample Output 2 3 4 8 9 10 14 15 Answer

#include int main() { int k,m,n,a,i=1; scanf(\ if(m

16、A+B ProblemT

Description

计算a+b,0<=a,b<1000。 Input

输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。 Output

每行输出一个a+b的值,顺序与输入对应。 Sample Input 1 2 10 20

Sample Output 3 30

Answer

#include int main() { int a,b; while(scanf(\ { printf(\ } return 0; }

17、A+B Problem (II) : Input/Output Pratice

Description

计算a+b,0<=a,b<1000。 Input

输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。 Output

每行输出一个a+b的和,顺序与输入对应。 Sample Input 2 1 2 10 20

Sample Output 3 30

Answer

#include int main() { int a[1000],b[1000],N,i; scanf(\ for(i=1;i<=N;i++) scanf(\ for(i=1;i<=N;i++) printf(\ return 0; }

18、成绩的等级

Description

把百分制的考试成绩转换成五级制的成绩: 90~100:Excellent 80~89:Good 70~79:Average 60~69:Pass 0~59:Failing

不在0~100之间的输入是非法数据,输出“Error”。 Input

输入多行,每行一个整数。 Output

输入所对应的成绩等级。 Sample Input -1 81 92 35 68 72 100

Sample Output Error Good Excellent Failing Pass Average Excellent Answer

#include int main() {

int score; while(scanf(\ { if (score<0||score>100) printf(\ else {

switch (score/10) { case 0: case 1: case 2: case 3: case 4: case 5:printf(\ case 6:printf(\ case 7:printf(\ case 8:printf(\ case 9: case 10:printf(\ } } }

return 0; }

19、n个数的最大值和最小值

Description

找出n个数中最大的数和最小的数,并将它们的值输出出来。 Input

输入为n+1个整数,都在int类型范围内。这些数可能用若干空格或者换行符分隔开。 输入的第1个数为n,表示后续有n个数输入。从输入的第2个数开始,求出直到第n+1个数中最大的数和最小的数。 Output

输出为两行,格式见sample。 Sample Input 3 0 1 -1

Sample Output

The maximum number is 1. The minimum number is -1. Answer

#include int main() {

int n,i,max,min; scanf(\ int a[n];

for(i=0; i

scanf(\ max=a[0]; min=a[0];

for(i=0; i

if(maxa[i]) min=a[i]; }

printf(\ printf(\ return 0; }

20、字符加密

Description

Tom和Jack是密码学爱好者,他们在聊天时经常使用一些暗语。他们使用的一种最简单的暗语是:将要说的每句话里面的英文字母变成这个字母之后的某个字母。现在要求你写一个程序,将一个字母变成它之后的某个字母。 Input

输入有2个:一个大写字母c和一个正整数d(0

输出字母c之后的第d个字母。大小写与c一致。如果c之后的某个字母已经超出'Z',则再从字母'A'开始计数。

如:c='A',d=3,则输出应为:D。 若:c='Y',d=3,则输出应为:B。 Sample Input A 3

Sample Output D

Answer

#include void main() { int d,x; char c; scanf(\ scanf(\ x=c+d; c=c+d; x<=90?printf(\ }

21、求100以内的素数

Description

素数是只能被1和自身整除的正整数,根据数学定义1不是素数。素数也叫质数。 Input

输入为两个整数m和n,满足0<=m<=n<=100。 Output

从大到小输出m~n之间的所有素数,一个素数一行。如果m~n之间没有素数,则不输出任何数。

输出的所有数在两行“=====”之间。 Sample Input 2 12

Sample Output ===== 11 7 5 3 2

===== Answer

#include #include int main() {

int m,n,i,j,k,t;

scanf(\printf(\ for(i=n;i>=m;i--) { t=0; for(j=2;j<=sqrt(i);j++)//12 if(i%j==0) t=1; if(t==0&&i>1) printf(\ } printf(\return 0; }

22、 Sum Problem (II) : Input/Output Pratice

Description

计算若干整数的和,这些整数都是小于1000的非负整数。 Input

输入的第一行是一个整数M,后面有M个测试样例。每个测试样例以一个整数N开始,后面接着是N个整数。 Output

每组测试样例对应一行输出,为所给的N个整数之和,顺序与输入对应。 Sample Input 2

3 1 2 3

5 10 15 20 30 50 Sample Output 6 125

Answer

#include int main() {

int m,n,a,i,j,s; scanf(\ for(i=1;i<=m;i++) {

scanf(\ s=0;

for(j=1;j<=n;j++) {

scanf(\ s=s+a; }

printf(\ }

return 0; }

23、十进制整数转二进制

Description

给出一个十进制的非负整数x,x<=216,把它转换成二进制数输出。 Input

输入为多行,每行一个整数x,至读入EOF结束。 Output

每行输出x对应的二进制数值。

Sample Input 0 1 3 33 65535

Sample Output 0 1 11

100001

1111111111111111 Answer

#include int main() {

int a[100],i,b;

while(scanf(\ {

for(i=0;;i++) {

a[i]=b%2; b=b/2; if(b==0) break; }

for(;i>=0;) {

printf(\ i--; }

printf(\ }

return 0; }

24、简单的整数排序

Description

对给出的若干整数按从小到大排序。 Input

输入的第一个数为n(n<=1000),后接n个整数。 Output

按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。

Sample Input

10 3 9 1 5 2 8 5 6 7 3 Sample Output 1 2 3 3 5 5 6 7 8 9 Answer

#include int main() {

int c,i,n,j; int a[1000];

scanf(\ for (i=0;i

scanf(\ for(i=1;i<=n-1;i++) { for(j=0;ja[j+1]) { c=a[j]; a[j]=a[j+1]; a[j+1]=c; } } }

printf(\ for(i=1;i

25、兔子的繁殖问题

Description

假设一对兔子每月能生一对小兔(一雌一雄),每对小兔出生后的下一个月是没有繁殖能力的,至出生后的第三个月开始又可以每月生一队小兔,问从一对刚出生的小兔开始,经过若干个月后一共有多少兔子(假设在此过程中兔子没有死亡)?

这个问题是意大利数学家菲波那契(Fibonacci)在他1202年出版的《算盘全书》中提出来的,从第一对刚出生的小兔开始每月的兔子数被乘坐菲波那契序列。 Input

输入的第一个数为n,接下来有n个数字。每个数字为一个月份m(m<=45)。 Output

输出为n行,每行为第m个月后的兔子总数。 Sample Input 6

1 2 3 4 5 10 Sample Output 1 2 3 5 8 89

Answer

#include int main() {

int n,x,i;

int a[50],b[50]; a[0]=1; a[1]=1; i=2;

while(i<50) {

a[i]=a[i-1]+a[i-2]; i++; }

scanf(\ for(i=1;i<=x;i++) {

scanf(\ b[i]=a[n]; }

for(i=1;i<=x;i++)

printf(\}

26登录密码验证

Description

编写一个程序,模拟用户登录系统的密码验证过程。系统提供给用户的密码长度最长为20个字符,若密码输入错误可以再次输入。但为了保证用户密码安全,若连续输入密码错误超过5次就会锁定账号一段时间。 Input

输入为若干个串,至EOF结束。输入的第一个串是用户的正确密码,后面的串为模拟用户登录时的输入的密码。 Output

每次输入错误的密码,输出一个“Wrong!”,若输入的密码为正确的,输出一个“Welcome!”,并结束密码测试。若前5次输入的密码都是错误的,则后面的输入中不管是否有正确的密码都输出“Out of limited!”。 Sample Input abcdefg

123456 kkkkkkkk abcdefg Sample Output Wrong! Wrong! Welcome! Answer

#include #include int main() {

char a[20],b[20]; int i,j=1;

scanf(\

while(scanf(\ {

if(j<=5) {

if((strcmp(a,b)==0)) {

printf(\ break; } else

printf(\ j++; }

else printf(\ } }

27、 Matrix Problem : Array Pratice

Description 求一个m×n阶矩阵A的转置矩阵AT。矩阵A的每个元素都在int类型的范围之内。 Input

输入的第一行为一个整数M(M>0),后面有M组输入数据。每组数据以两个正整数m和n开始,满足0

Output

输出为多组,每组输出A的转置矩阵AT。矩阵的输出为:每行两个元素之间用一个空格分开,每行最后一个元素之后为一个换行,在下一行开始输出矩阵的下一行。每两组输出之间用一个空行分隔开。 Sample Input 1 3 3 1 2 3 4 5 6 7 8 9

Sample Output 1 4 7 2 5 8 3 6 9 Answer

#include int main() {

int a[100][100]={0},M,m,n,i,j,k,b[100][100]={0}; scanf(\ for(k=0;k

scanf(\ for(i=0;i

scanf(\ for(j=0;j

b[j][i]=a[i][j]; if(i<(m-1))

printf(\ else

printf(\ }

if (k<(M-1)) printf(\ }

return 0; }

28、string to integer(I)

Description

需要编写一个函数,将字符串转换为整数。函数原型为: int strToInt(char str[]);

其中:参数str[]是需要转换的字符串,返回值表示字符串str[]转换为整数之后的结果。 转换规则是:将字符串str[]中全部都是数字字符的前缀转换为整数,并返回。如果str[]的首字符不是数字字符,则返回0;如果str[]是空串,则返回-1。 示例:strToInt(“123ab”)=123; strToInt(\

注意:主函数已经给出,你只需要提交strToInt()函数的代码。而且不能使用标准库函数进行转换。提交的代码要包含必要的头文件包含命令。 Input

输入是若干行字符串,它们都包含不超过10个的字符,而且包含的整数不会超出int类型的表示范围。 Output

输出为若干行整数,每一行输出与上述输入一一对应。

Sample Input 123c 0123dd -45ed e1321

Sample Output 123 123 -1 0 0

Answer

#include int strToInt(char str[]) {

int i,j,a=0,s=0; if(str[0]=='\\0') return -1; else {

for(i=0;str[i]!='\\0';i++) {

if(str[0]<48||str[0]>57) {

return 0; goto loop; } else {

if(str[i]>=48&&str[i]<=57) {

a=str[i]-'0'; s=s*10+a; a=0; } else

break; } }

return s; loop:; } }

int main() {

char str[11]; gets(str);

printf(\ while (gets(str)!=NULL) {

printf(\ }

return 0; }

29、 string to integer(II)

Description

需要编写一个函数,将字符串转换为整数。函数原型为: int strToInt(char str[]);

其中:参数str[]是需要转换的字符串,返回值表示字符串str[]转换为整数之后的结果。 转换规则是:字符串中可能包含符号位,即“+”或“-”,而且如果包含符号位,能且仅能是第一个字符。将字符串str[]中全部都是数字字符(含0个或1个符号位)的前缀转换为整数,并返回。如果str[]的首字符不是数字字符也不是符号位,则返回0;如果str[]是空串,则返回0。

示例:strToInt(“+123ab”)=123; strToInt(\-a123\。

注意:主函数已经给出,你只需要提交strToInt()函数的代码。而且不能使用标准库函数进行转换。提交的代码要包含必要的头文件包含命令。 Input

输入是若干行字符串,它们都包含不超过10个的字符,而且包含的整数不会超出int类型的表示范围。 Output

输出为若干行整数,每一行输出与上述输入一一对应。 Sample Input 123c 0123dd

-45ed e1321 +76abcdef

Sample Output 123 123 0 -45 0 76

Answer

#include int strToInt(char str[]) {

int i,j,a=0,s=0; if(str[0]=='\\0') return 0; else {

if((str[0]<48||str[0]>57)&&str[0]!='-'&&str[0]!='+') return 0; else if(str[0]=='+')

for(i=1;str[i]!='\\0';i++) {

if(str[i]>=48&&str[i]<=57) { a=str[i]-'0'; s=s*10+a; } else

break; }

else if(str[0]=='-') {

for(i=1;str[i]!='\\0';i++) {

if(str[i]>=48&&str[i]<=57)

{

a=str[i]-'0'; s=s*10+a; } else

break; } s=-s; } else

for(i=0;str[i]!='\\0';i++) {

if(str[i]>=48&str[i]<=57) {

a=str[i]-'0'; s=s*10+a; } else

break; } return s; } }

int main() {

char str[11]; gets(str);

printf(\ while (gets(str)!=NULL) {

printf(\ }

return 0; }

30、string compare(I)

Description

需要编写一个可以比较字符串大小的函数,其原型为: int strcmp(char str1[],char str2[]);

其中:参数str1[]和str2[]是两个用于比较的字符串。返回值表示它们的大小关系。

比较规则是:按照相同位置的字符的ASCII码进行比较,如果两个字符串所有位置的字符都相同,则返回0;如果第(ii>=0)个位置的字符是第一对不同的字符,则返回str1[i]与str2[i]的ASCII码的差。

示例:strcmp(\

注意:主函数已经给出,你只需要提交strcmp()函数的代码。不能使用标准库函数进行比较。提交的代码要包含必要的头文件包含命令。 Input

输入为n(n>0)对字符串(可能含空白符),每一对字符串中的第一个字符串即为str1,第二个是str2。 Output

输出是n行整数,每一行整数与上述每一对输入一一对应,即表示相应的每对字符串的比较结果。

Sample Input abc ABC abc

abC abc abc

Sample Output 65 32 0

Answer

#include

int strcmp(char str1[],char str2[]) {

int i=0,j=0,s;

while(str1[i]!='\\0'||str2[i]!='\\0') {

if(str1[i]==str2[i]) i++; else {

s=str1[i]-str2[i]; j++; i++; break; } }

if(j==0)

return 0; else

return s; }

int main() {

char str1[101],str2[101]; gets(str1); gets(str2);

printf(\ while (gets(str1)!=NULL) {

gets(str2);

printf(\ }

return 0; }

31、 insert a number into an ordered sequence(I)

Description

现有一个正整数数组Array,以0表示数组的结束。其中任意2个元素都不同,而且已经按照递增序排列。另有一个整数Key>0。要将Key插入到数组Array中,并保证插入之后的数组依然保持递增序。在插入过程中,会有以下情况出现: 1. 数组Array已经达到其容量上界Vol,此时应返回结果-1。

2. 数组Array中已经有与Key相等的元素存在,此时应返回结果-2。

3. 如果Array既没有满,也没有与Key相等的元素,则应返回Key在数组Array中插入的位置,即Key插入Array后所在的下标。

要求编写一个函数int insert(int array[],int key,int vol)实现上述插入操作,其中: array[]:需插入元素所在的数组; key:待插入的元素;

vol:array数组的最大容量,即数组中最多可以容纳的数值个数。

样例中已经包含上述三种可能的情形。上述情况按照上述顺序依次判断,即先判断Array是否已满,再判断Array是否已经有Key。

注意:主函数已经给出,只需提交insert函数的代码以及必要的预处理命令。 Input

输入分为多行。第一行是M,表示后面有M组测试数据。

每组测试数据有2行输入。第一行是Array中的元素,不超过1000个,以数字“0”表示该行数据的结束。第二行是要插入Array中的数值Key。 Output

输出为M行,每一行代表上述每一组输入的插入结果。其中: 如果数组已满,则输出:“The array if full!”

如果数组中已经有Key,则输出:“The key is already in this array!”

如果Key可以插入Array,则输出:“The key is inserted in position x”. 其中x是Key插入数组后的下标。 Sample Input

3

1 2 3 0 2

1 2 4 5 6 7 8 9 10 0 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 0 2

Sample Output

The key is already in this array! The key is inserted in position 2. The array if full!

Answer

#include

int insert(int array[],int key,int vol) { int i;

for(i=0;i<=vol;i++) {

if(array[vol]==0) return -1; else {

if(key==array[i]) return -2; else {

if(key

if(key>array[i]&&key

if(key>array[i]&&array[i+1]==0) return i+1; } } } }

int main() {

int array[1001],key; int M,i,j;

scanf(\ for (i=0;i

j=0;

scanf(\ while (array[j]!=0) {

j++;

scanf(\ }

scanf(\

j=insert(array,key,1000); if (j==-1)

puts(\ else if (j==-2)

printf(\ else

printf(\ }

return 0; }

32、求字符串的长度(编程题)

Description

编写一个求字符串长度的函数,其原型如下: int strlen(char str[]);

其中str[]表示待求长度的字符串,返回值是str[]的长度。

注意:主函数已经给出,只需提交strlen()函数及必要的头文件包含命令。 Input

输入为多行。第一行N>0表示有N个测试用例,后面有N行,每行包含一个字符串(不超过1000个字符)。 Output

输出为多行,每行对应于一个测试用例。每行的格式为: case i:lenght=j.

其中i表示测试用例编号(从1开始),j表示相应的字符串长度。 Sample Input 4

I love China!

Do you want to pass this examination? You will succeed finially! Wish you succeed! Sample Output case 1:length=13. case 2:length=37. case 3:length=26. case 4:length=17. Answer

#include int strlen(char str[]) { int j;

for(j=0;str[j]!='\\0';j++) { } return j; }

int main() {

int i,N;

char str[1001]; scanf(\ getchar(); gets(str);

printf(\ for (i=2;i<=N;i++) {

gets(str);

printf(\ }

return 0; }

33、求数组中的最大值(填空)

Description

现有一个不超过1000个整数组成的数组,其中可能有重复数据出现。要求编写一个程序,求该数组中的最大值以及最大值所在的所有下标。部分程序已经给出,请填充空白语句。 程序(含答案): #include int main() {

int array[1000],N,maxIndex[1000],max,i,numOfMax; scanf(\; for (i=0;i

scanf(\ max=array[0]; numOfMax=0; maxIndex[numOfMax++]=0; for (i=1; i

if (max

max=array[i]; numOfMax=0; maxIndex[numOfMax++]=i; }

else if (array[i]==max) {

maxIndex[numOfMax++]=i; } }

printf(\ printf(\ for (i=1;i

return 0; }

34、求矩阵的每行之和(编程题)

Description

编写一个程序,求一个矩阵的每行值的和。设矩阵的所有元素都是整数,而且每行的和不超过int类型的表示范围。 Input

输入为多行。第一行K>0,表示有K个测试用例。

之后K个测试用例中,首先是两个整数0

输出有K行,每个测试用例的结果占一行。每行的格式为: case i:d1 d2 ... dj

其中i表示测试用例的编号(从1开始),d1、d2、....、dj表示相应测试用例的各行的和,两两之间用空格隔开。 Sample Input 4 3 3 1 2 3 1 2 3 1 2 3 2 3 1 1 1 1 1 1 1 1 1 5 1 3 4 5 6 7

Sample Output case 1:6 6 6 case 2:3 3 case 3:1

case 4:3 4 5 6 7 Answer

#include int main() { int K,i,j,k,m,n,r[100],sum[100][100],a[100][100]; scanf(\ for(k=0;k

for(i=0;i

35、 一维数组的逆序(编程题)

Description

编程,实现对一个一维数组的逆序,即将数组的元素反转。 Input

输入分多行,第一行是N>0,表示有N个用例。

每个用例的输入有1行或2行,其中第一行是0<=M<=1000,表示该数组有M个整数。如果M>0,则第二行包含M个整数,两两之间用空格隔开;如果M=0,则该用例没有第二行输入。 Output

输出为N行,每行与上述输入一一对应,分别是对应用例的逆序。输出格式为: case i:d1 d2 ...

其中i表示用例编号(从1开始),d1、d2等是数组逆序后的结果,两两之间用空格隔开。如果该用例不包含任何输入,即:M=0,则仅输出: case i:

Sample Input 3 10

10 9 8 7 6 5 4 3 2 1 0 5

1 2 3 4 5

Sample Output

case 1:1 2 3 4 5 6 7 8 9 10 case 2:

case 3:5 4 3 2 1 Answer

#include int main() { int N,M[10000],a[100][1001],i,j; scanf(\ for(i=0;i0;j--) printf(\ printf(\ printf(\ } else printf(\ } return 0; }

36、 结构体的使用(编程题)

Description

设有结构体定义如下: typedef struct Student {char major[50];//专业 char name[50];//姓名

int score[3];//3门课程的成绩 } STU;

编写一个子函数,输出每个学生的总分,函数原型如下: void printInfo(STU students[],int num);

其中students[]是由num个STU类型的结构体组成的数组。 输出格式见下。

注意:主函数已经给出,提交时需提交以下内容:上述结构体STU的定义(直接复制上就可以,要放在头文件包含命令之后)、必要的头文件包含命令以及printInfo函数的代码。 Input

输入为多行。第一行N>0表示有N个学生的信息。之后有N行,每一行包含5个部分,分别表示每位学生的专业、姓名和3门课程的成绩,两两之间用空格隔开。成绩为正整数。 Output

输出为N行,每一行为一名学生的信息,格式为: major,name:totalSocre.

其中major表示学生的专业,name表示学生的姓名,totalScore表示该生的总分。所有的标点符号均为半角字符。

Sample Input 3

Computer Tom 100 98 89 Information Jack 98 89 87 Management Mary 89 89 89 Sample Output Computer,Tom:287. Information,Jack:274. Management,Mary:267. Answer

#include typedef struct Student {

char major[50]; char name[50]; int score[3]; } STU;

void printInfo(STU students[],int num) { int i;

for(i=0;i

printf(%udents[i].score[2]); printf(\} }

int main() {

int i,N;

scanf(\ STU stus[N]; getchar();

for (i=0;i

scanf(\

scanf(\ }

printInfo(stus,N); return 0; }