搜索
您的当前位置:首页正文

计算机程序设计基础(C语言)编程习题

来源:爱够旅游网
计算机程序设计基础(C语言)

编程练习题及参考答案

1.输入2个整数,求两数的平方和并输出。

#include main()

{ intt a ,b,s;

printf(\"please input a,b:\\n\"); scanf(\"%d%d”,&a,&b); s=a*a+b*b;

printf(\"the result is %d\\n\ }

2. 输入一个圆半径(r)当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。

#include

#define PI 3.14

main()

{ float r ,s , l;

printf(\"please input r:\\n\"); scanf(\"%f”,&r);

if (r>=0) {s=pi*r*r; l=2*i*r ;

printf(\"the area is %f\\n\

printf(\"the circumference is %f\\n\else

printf(\"input error!\\n\"); }

3、函数y=f(x)可表示为:

2x+1 (x<0) y= 0 (x=0) 2x-1 (x>0)

编程实现输入一个x值,输出y值。 main() {int x,y;

scanf(“%d”,&x); If(x<0)y=2*x+1; If(x>0)y=2*x-1; If(x==0) y=0; printf(“%d”,y);}

4、编写一个程序,从4个整数中找出最小的数,并显示此数。

main( )

{int a,b,c,d,t;

scanf (“%d,%d,%d,%d ”,&a,&b,&c,&d); if (a>b)

{t=a; a=b; b=t;} if (a>c)

{t=a; a=c; c=t;} if (a>d)

{t=a; a=d; d=t;}

printf (“min = %d \\n”,a); }

5.有一函数当x<0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,输出y值。

main() {int x,y;

scanf(\"%d\if (x<0) y=1;

else if(x==0) y=5; else y=3;

printf(\"x=%d,y=%d\\n\

6.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中调用该函数)

main()

{float max(float x,float y); float a,b,m;

scanf(\"%f,%f\

m=max(a,b);

printf(\"Max is %f\\n\}

float max(float x,float y) {

float temp; if (xy=temp; }

return(x); }

7、从键盘输入你和你朋友的年龄,编成判断谁的年龄最大,并打印最大者的年龄。 #include

2

计算机程序设计基础(c语言) 习题 编程题

main()

{ int yourAge, hisAge; printf(\"Please enter your age:\"); scanf(\"%d\ /*输入你的年龄yourAge*/ printf(\"Please enter your friend's age:\"); scanf(\"%d\ /*输入你朋友的年龄hisAge*/ if (yourAge >= hisAge) { printf(\"You are older! Your age is = %d\\n\ } if (hisAge > yourAge) { printf(\"Your friend is older! HisAge age is = %d\\n\ }}

8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error”

#include “stdio.h” main( ) {int a,b,c;

printf(“please input a and b\\n”); scanf (%d,%d”,&a,&b);

printf(“please input the answer for a+b\\n”); scanf (%d”,&c); if (c==a+b)

printf(“right\\n”); else

printf(“error\\n”); }

9. 编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:

30元 费用每小时3元每小时2.5元main()

{ int hour; float fee;

printf(“please input hour:\\n”); scanf(“%d”,&hour); if(hour<=10)

3

10小时1050小时

50小时要求当输入每月上网小时数,显示该月总的上网费用(6分)

fee=30;

else if(hour>=10&&hour<=50) fee=3*hour;

else fee=hour*2.5;

printf(“The total fee is %f”,fee); }

10.神州行用户无月租费,话费每分钟0.6元,全球通用户月租费50元,话费每分钟0. 4元。输入一个月的通话时间,分别计算出两种方式的费用,判断哪一种合适。 main()

{float a,x,y;

printf(“\\n请输入您的话费:”); scanf(“%f,”,&a); x= 0.6*a;

y=50+0.4*a;

printf (“神州行话费为: %f\\n”,x); printf (“全球通话费为: %f\\n”,y); if (x>=y)

printf(“建议使用全球通”); else printf(“建议使用神州行); } 11.个人所得税计算,应纳税款的计算公式如下:

收入 收入<=1000元部分 2000元>=收入>1000元的部分 3000元>=收入>2000元的部分 6000元>=收入>3000元的部分 收入>6000元的部分 税率 0% 5% 10% 15% 20% 输入某人的收入,计算出应纳税额及实际得到的报酬。(7分)

(如需连续计算多个人的纳税情况,直到输入负数为止,程序应如何改进?试写出程序) #include “stdio.h” main() {

int grade;

float income,tax,money;

printf(“please input your income\\n”); scanf (“%f”,&income); if (income<0)

printf(“the input is error”); else

4

计算机程序设计基础(c语言) 习题 编程题

{ grade=(int)income/1000; switch(grade)

{ case 0 : tax=0;break;

case 1 : tax=(income-1000)*0.05;break; case 2 : tax=50+(income-2000)*0.1;break; case 3 :

case 4 :

case 5 : tax=150+(income-3000)*0.15;break; default: tax=600+(income-6000)*0.2;

}

money=income-tax;

printf(“\\n tax=%f, money=%f”,tax, money); } }

12.从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A;80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;score<60,等级为E。

#include main() {

int data;

char grade; printf(\"Please enter the score:\"); scanf(\"%d”, &data);

switch(data/10) { case 10:

case 9 : grade=’A’; break; case 8: grade=’B’; break; case 7: grade=’C’; break; case 6: grade=’D’; break; default: grade=’E’; }

printf(\"the grade is %c”,grade); }

*13. 编程设计一个简单的计算器程序。从键盘输入2个操作数,1个运算符,当运算符为加(+)、减(-)、乘(*)、除(/)时,输出计算结果 #include main()

{ int data1, data2; /*定义两个操作符*/

5

char op; /*定义运算符*/ printf(\"Please enter the expression:\"); scanf(\"%d%c%d\ /*输入运算表达式*/ switch(op) /*根据输入的运算符确定要执行的运算*/ { case '+': /*处理加法*/ printf(\"%d + %d = %d \\n\ break; case '-': /*处理减法*/ printf(\"%d - %d = %d \\n\ break; case '*': /*处理乘法*/ printf(\"%d * %d = %d \\n\ break; case '/': /*处理除法*/ if (0 == data2) /*为避免出现溢出错误,检验除数是否为0*/ printf(\"Division by zero!\\n\"); else printf(\"%d / %d = %d \\n\ break; default: printf(\"Unknown operator! \\n\"); } }

14. 从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。

main( )

{int a[10], i,p=0,n=0,z=0;

printf(“please input number”); for(i=0;i<10;i++) {scanf(“%d,”,&a[i]); if (a[i]>0) p++;

else if (a[i]<0) n++;

else z++}

printf(“正数:%5d, 负数:%5d,零:%5d\\n”,p,n,z); } }

15、编程序实现求1-200之间的所有数的乘积并输出。

#include main( )

{ int i, sum=1

6

计算机程序设计基础(c语言) 习题 编程题

for(i=1; i<200 i=i+1) sum=sum*i;

printf(“the sum of odd is :%d”,sum);

}

16. 从键盘上输入10个数,求其平均值。

main() {

int a[10],i,s=0; float ave;; for(i=0;i<10;i++)

scanf(“%d”,&a[i]); for(i=0;i<10;i++) sum+=a[i];

ave=(float)sum/10;

printf(\"ave = %f\\n\ }

17、编程序实现求1-1000之间的所有奇数的和并输出。

#include main( )

{ int i, sum=0;

for(i=1; i<1000; i=i+2) sum=sum+i;

printf(“the sum of odd is :%d”,sum);

}

18.有一个分数序列:2/1,3/2,5/3,8/5,13/8,21/13……

编程求这个序列的前20项之和。 main() {

int i,t,n=20;

float a=2,b=1,s=0; for(i=1;i<=n;i++) {s=s+a/b; t=a; a=a+b; b=t; }

printf(\"sum=%9.6f\}

19. 用数组实现以下功能:输入5个学生成绩,而后求出这些成绩的平均值并显示出来。

7

main()

{float a[5],i; float s=0;

for(i=0;i<5;i++) scanf(“%f”,&a[i]); for(i=0;i<5;I++) s=s+a[i];

printf(“result=%f”,s/5); }

*20、用循环的方法构造一个5行5列的二维数组,使主对角线上的变量为1,其它为0,并将数组中所有项按行按列显示出来。

main()

{int a[5][5],i,j, s=0; for(i=0;I<5;i++) for(j=0;j<5;j++) if(i= =j) a[i][j]=1; else a[i][j]=0; for(i=0;i<5;i++) for(j=0;j<5;j++)

{if(j= =0) printf(“\\n”); printf(“%d ”, a[i][j]); } }

21.求一个3×3矩阵对角线元素之和。从键盘输入矩阵元素的值并输出和的值.

main()

{ int a[3][3],sum=0; int i,j;

printf(\"Enter data:\\n\"); for(i=0;i<3;i++) for(j=0;j<3;j++)

scanf(\"%d\ for(i=0;i<3;i++)

sum=sum+a[i][i]; printf(\"sum=%d\}

22.输入n的值,n代表行数,输出如图所示的图形。(6分) *

* * *

* * * * *

* * * * * * * (此图为n=4时的输出结果)

8

计算机程序设计基础(c语言) 习题 编程题

#include main()

{int i , j , k; for (i = 1; i <= 4; i++) /*控制行数*/ { for (k = 1; k <= (2 * i - 1); k++) /*控制每行输出的*号个数*/ { printf(\"*\"); } printf(\"\\n\"); }} /*输出一行后换行*/

23、从键盘输入30名学生的成绩数据,求其中的最高分、最低分和平均分。

(提示:用数组存放成绩数据)

#include #define M 30 main ( )

{ float score[M], max , min, aver; int i ;

printf(“please input score: \\n”); for(i=0; iscanf(“%f”, &score[i]); max=score[0]; min=score[0]; aver=score[0]; for(i=1; i{ if (max < score[i]) max= score[i]; if (min>score[i]) min=score[i]; aver+=score[i]; }

printf(“max=%f, min=%f,aver=%f”, max, min, aver/M);

}

24. 从键盘输入某班学生某门课的成绩及其学号(班级人数最多40人,具体人数由键盘输入),输出该班最高分和最低分及其学生学号;并输出该班该课程的总分和平均分。请编写程序。

#include #define ARR_SIZE 40 main()

{ float score[ARR_SIZE], maxScore,minScore,sum; int n, i;

long maxNum, minNum,num[ARR_SIZE]; printf(\"Please enter total number:\"); scanf(\"%d\

9

printf(\"Please enter the number and score:\\n\"); for (i=0; iscanf(\"%ld%f\ maxScore = score[0];minScore= score[0]; maxNum = num[0]; minNum= num[0]; sum=score[0];

for (i=1; iif (score[i] > maxScore)

{ maxScore = score[i]; maxNum = num[i]; }

{ minScore = score[i];

minNum = num[i];

}

sum=sum+score[i]; }

printf(\"maxScore = %.0f, maxNum = %ld\\n\ printf(\"sum = %.1f, average = %.1f\\n\

}

*25.将一个有5个元素的数组中的值(整数)按逆序重新存放。

例: 原来顺序为:8、6、5、4、1,要求改为1、4、5、6、8 define N 5

main()

{int a[N],I,temp;

printf(“enter array a:\\n”); for(I=0;I{ temp=a[i]; a[i]=a[N-I-1]; a[N-I-1]=temp; }

printf(“\\n Now, array a:\\n”); for(I=0;I10

else if (score[i] < minScore)

printf(\"minScore = %.0f, minNum = %ld\\n\

计算机程序设计基础(c语言) 习题 编程题

printf(“%4d”,a[i]); printf(“\\n”); }

*26.从键盘上输入一个2*3的矩阵,将其转置后形成3*2的矩阵输出。

main()

{int a[2][3], b[3][2],i,j; for(i=0;i<2;i++) for(j=0;j<3;j++)

scanf(“%d”,&a[i][j]); for(i=0;i<3;i++) for(j=0;j<2;j++) b[i][j]=a[j][i]; for(i=0;i<3;i++) {for(j=0;j<2;j++)

printf(\"%5d\ printf(\"\\n”); } }

*27.编写两个函数分别求两个整数的最小公倍数和最大公约数,用主函数调用这两个函数并输出结果。两个整数由键盘输入。

#include \"stdio.h\"

mingb(x,y) int x,y; {int z,i,t; z=1; i=1; if(x>y)

{t=x;x=y;y=t;}

while(z<=x*y) {

z=i*y;

if((z%x==0)&&(z%y==0)) break;

i++; }

return(z); }

maxgy(x,y) int x,y; {int z,t;

11

if(x>y)

{t=x;x=y;y=t;} z=x;

while(z>1)

{ if((x%z==0)&&(y%z==0)) break; z--; }

return(z); } main() {

int a,b,c; char ch;

printf(\"\\nmingb(1)/maxgy(2)?\"); ch=getchar();

printf(\"\\ninput:\");

scanf(\"%d,%d\if(ch=='1') c=mingb(a,b);

else if(ch='2') c=maxgy(a,b); printf(\"the result is %d\getch(); }

*28. 输入一个3*3矩阵,求出其转置矩阵,并求出两个矩阵的和.

main() {

int a[3][3]; int b[3][3]; int c[3][3] int i,j;

printf(“please input 6 numbers!”) for (i=1;i<3;i++) for(j=1;j<3;j++) {

scanf(“%d”,&a[i][j]); b[j][i]=a[i][j]; }

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

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

c[i][j]=a[i][j]+b[i][j]; }

12

计算机程序设计基础(c语言) 习题 编程题

for (i=1;i<3;i++) for(j=1;j<3;j++) {

printf(“%d”,a[i][j]); } }

29、从键盘输入10名学生的成绩数据,按成绩从高到低的顺序排列并输出。(提示:用数组存放成绩数据)

main()

{ int a[10]; int i,j,temp;

printf(\"input score:\\n\"); for(i=0;i<10;i++) scanf(\"%d\ printf(\"\\n\"); for(i=1;i<10;i++) for(j=0;j<9;j++) if(a[j]for(i=0;i<10;i++) printf(\"%d,\}

30. 定义一个5行3列的数组,从键盘输入各数组元素的值,计算各数组元素之和。 #include

main( )

{ int i, j ,a[5][3];

printf(“Enter data:\\n”); for(i=0;i<5;i++) for(j=0;j<3;j++)

scanf(“%d”,&a[i][j]); for(i=0;i<5;i++) for(j=0;j<3;j++)

sum=sum+a[i][j];

printf(“sum=%5d\\n”,sum); }

31、编写程序,交换两个数组中的对应元素。 #include

13

#define N 20 main( )

{ int a[N], b[N], i, j, temp; printf(“please input a:\\n”); for(i=0; iscanf(“%d”, &a[i]);

printf(“please input b:\\n”); for(j=0; jscanf(“%d”, &b[i]); for(i=0; ifor(j=0; jfor(j=0; j*32、从键盘上输入一个4*3的整型数组,找出数组中的最小值及其在数组中的下标。

#include main()

{ int a[4][3], i , j ,min,m,n; printf(\"Please enter data:\"); for (i=0; i<4; i++) for (j=0; j<3; j++)

scanf(“%d”,& a[i][j]); min=a[0][0]; m=0; n=0;

for (i=0; i<4; i++) for (j=0; j<3; j++) if (a[i][j]{min= a[i][j]; m=i; n=j; }

printf(\"the min is %d\\n, min);

printf(\"posion is %d %d \\n, m,n); }

14

计算机程序设计基础(c语言) 习题 编程题

33.编程实现如下功能:从键盘输入一行字符,统计其中大写英文字符,小写英文字符和其他字符的个数。

#include #include

#define ARR_SIZE 80 main() {

char str[ARR_SIZE];

int len, i, letter = 0, digit = 0, space = 0, others = 0; printf(\"Please input a string:\"); gets(str);

len = strlen(str); for (i=0; i{ if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') letter ++; /*统计英文字符*/ else if (str[i] >= '0' && str[i] <= '9' )

digit ++; /*统计数字字符*/ else others ++; /*统计其它字符的个数*/ }

printf(\"English character: %d\\n\ printf(\"digit character: %d\\n\ printf(\"other character: %d\\n\}

*34.编程实现如下功能:

1)在主函数中,实现从键盘输入10名学生某门课的成绩,保存在一维数组中;调用排序函数;对排序后的数组中的元素按从高到低打印输出。

2)编写排序函数,使用数组名做函数参数,实现对该成绩的排序。

#include #define ARR_SIZE 40

void Sort(float score[], long num[], int n); main()

{ float score[ARR_SIZE]; int n, i;

long num[ARR_SIZE];

printf(\"Please enter total number:\"); scanf(\"%d\

printf(\"Please enter the number and score:\\n\");

15

for (i=0; iSort(score, num, n); printf(\"Sorted results:\\n\");

for (i=0;ivoid Sort(float score[], long num[], int n) { int i, j;

float temp1;

long temp2;

for (i=0; i{ if (score[j] > score[i]) { temp1 = score[j]; score[j] = score[i]; score[i] = temp1; /*交换学号*/ temp2 = num[j]; num[j] = num[i]; num[i] = temp2; } } } }

*35.编程实现如下功能:

实现从键盘输入两个字符串,分别存入两个不同的字符数组中;将两个字符串连接为一个字符串,并打印输出连接后的整个字符。

#include #include

#define ARR_SIZE 80

void MyStrcat(char dstStr[], char srcStr[]); main()

{ char s[ARR_SIZE], t[ARR_SIZE]; printf(\"Please enter source string: \"); gets(s);

printf(\"Please enter destination string: \"); gets(t);

16

计算机程序设计基础(c语言) 习题 编程题

MyStrcat(s,t);

printf(\"The concatenate string is: \"); puts(s); }

void MyStrcat(char dstStr[], char srcStr[]) { int i = 0, j;

while (dstStr[i] != '\\0') { i++; }

for (j=0; srcStr[j]!='\\0'; j++, i++) {

dstStr[i] = srcStr[j]; }

dstStr[i] = '\\0'; }

*36、猜数游戏。系统随机产生一个整数,通过键盘输入数据猜数,猜对为止,并要求统计猜的

次数。

注:rand()函数可以产生0~32767间的正整数,程序中需包含stdlib.h。

#include #include main() {

int magic; int guess; int counter; magic = rand() % 100 + 1; counter = 0; do{ printf(\"Please guess a magic number:\"); scanf(\"%d\ counter ++; if (guess > magic) printf(\"Wrong!Too high!\\n\"); else if (guess < magic) printf(\"Wrong!Too low!\\n\"); }while (guess != magic);

printf(\"Right!\\n\");

printf(\"counter = %d\\n\

17

}

37.输入两个整数,利用指针变量作为函数参数,编程实现两数互换功能,并将交换后的数据重新输出。

#include

void Swap(int *x, int *y); main()

{ int a, b;

printf(\"Please enter a,b:\"); scanf(\"%d,%d\

printf(\"Before swap: a = %d,b = %d\\n\ Swap(&a, &b);

printf(\"After swap: a = %d,b = %d\\n\ }

void Swap(int *x, int *y) {

int temp; temp = *x; *x = *y;

*y = temp; }

38.随机输入若干个学生的体重,以输入负数或零结束,分别求最重和最轻的体重,并计算平均体重。

#include

main() { int n=0;

float weight,max=0,min=10,sum=0,ave; printf(“please input the weight:”); scanf(“%f”,& weight); while(weight>0)

{ sum=weight+sum; n++;

if (weightelse if(weight>max) max=weight;

scanf(“%f”,& weight);} if (n>0)

{ ave=sum/n; printf(\"maxweight = %f\\n \" , max); printf(\"minweight = %f\\n\

printf(\"ave = %f\\n\

else printf(\"NO VALID DATA”);

18

计算机程序设计基础(c语言) 习题 编程题

}

39.输入m,k的值,编程求下面表达式的值:(要求编写一个求阶乘的函数,调用函数实现本题)

pk!

(mk)!

#include

unsigned long Factorial(unsigned int number); main()

{ unsigned int m, k; double p;

printf(\"Please input m, k:\"); scanf(\"%u, %u\

p = (double)Factorial(k) / Factorial (m-k); printf(\"p=%f\\n\}

unsigned long Factorial(unsigned int number) { unsigned long i, result = 1; for (i=2; i<=number; i++) result *= i; return result; }

*40. 编写程序,其中自定义一函数,用来判断一个整数是否为素数,主函数输入一个数,输出是否为素数。

#include

int IsPrimeNumber(int number) { int i;

if (number <= 1) return 0;

for (i=2; iprintf(“Please input n:”); scanf(“%d”,&n);

if(IsPrimeNumber(n))

printf(“\\n%d is a Prime Number”,n);

19

else printf(“\\n%d is not a Prime Number”,n);}

20

因篇幅问题不能全部显示,请点此查看更多更全内容

Top