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

更新时间:2023-11-22 23:34:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

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

编程练习题及参考答案

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

#include main()

{ intt a ,b,s;

printf(\ scanf(\ s=a*a+b*b;

printf(\ is %d\\n\ }

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

#include

#define PI 3.14

main()

{ float r ,s , l;

printf(\ scanf(\

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

printf(\

printf(\else

printf(\ }

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);}

1

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

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(\if (x<0) y=1;

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

printf(\

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

main()

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

scanf(\

m=max(a,b);

printf(\}

float max(float x,float y) {

float temp; if (x

y=temp; }

return(x); }

2

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

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

{ int yourAge, hisAge; printf(\ scanf(\ /*输入你的年龄yourAge*/ printf(\ scanf(\ /*输入你朋友的年龄hisAge*/ if (yourAge >= hisAge) { printf(\ } if (hisAge > yourAge) { printf(\ }}

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”);

3

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

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

scanf(“%d”,&hour); if(hour<=10) 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);

4

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

if (income<0)

printf(“the input is error”); else

{ 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(\ scanf(\

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(\s %c”,grade); }

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

5

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

{ temp=a[i]; a[i]=a[N-I-1]; a[N-I-1]=temp; }

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

*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(\ printf(\n”); } }

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

#include \

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++;

11

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

}

return(z); }

maxgy(x,y) int x,y; {int z,t; 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(\ch=getchar();

printf(\

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

else if(ch='2') c=maxgy(a,b); printf(\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]);

12

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

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]; }

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(\ for(i=0;i<10;i++) scanf(\ printf(\for(i=1;i<10;i++) for(j=0;j<9;j++) if(a[j]

for(i=0;i<10;i++) printf(\}

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]);

13

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

for(i=0;i<5;i++) for(j=0;j<3;j++)

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

printf(“sum=]\\n”,sum); }

31、编写程序,交换两个数组中的对应元素。 #include #define N 20 main( )

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

scanf(“%d”, &a[i]);

printf(“please input b:\\n”); for(j=0; j

scanf(“%d”, &b[i]); for(i=0; i

for(j=0; j

for(j=0; j

*32、从键盘上输入一个4*3的整型数组,找出数组中的最小值及其在数组中的下标。

#include main()

{ int a[4][3], i , j ,min,m,n; printf(\ 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]

14

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

{min= a[i][j]; m=i; n=j; }

printf(\

printf(\ %d \\n, m,n); }

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

#include #include

#define ARR_SIZE 80 main() {

char str[ARR_SIZE];

int len, i, letter = 0, digit = 0, space = 0, others = 0; printf(\ 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(\ %d\\n\ printf(\ %d\\n\ printf(\ %d\\n\}

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

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

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

#include #define ARR_SIZE 40

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

15

本文来源:https://www.bwwdw.com/article/o2gv.html

Top