700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C语言程序设计 谭浩强(第五版)课后代码答案

C语言程序设计 谭浩强(第五版)课后代码答案

时间:2024-03-30 18:57:56

相关推荐

C语言程序设计 谭浩强(第五版)课后代码答案

**

第四章

**

4、有3个整数,输出最大的数

#include <stdio.h>int main() {int a,b,c;scanf("%d,%d,%d",&a,&b,&c);int max(int a,int b,int c);printf("%d",max(a,b,c));}int max(int a,int b,int c){int x;x = (a>b) ? a : b;x = (x>c) ? x : c;return x;}

5、输入1000正数求平方根并判断输入的数

#include <stdio.h>#include<math.h>#include <string.h>int main(){double x ;while(1) {scanf("%lf", &x);if (x <= 1000 && x >= 0) {printf("%.2lf", sqrt(x));return 0;} else {printf("请重新输入小于1000的正数\n");}}}

6、有一个函数

#include <stdio.h>int main(){float x,y;scanf("%f",&x);y = x<1?x:(x<10?2*x-1:3*x-11);printf("%.2f",y);}

8、

#include <stdio.h>int main(){float x;int a;scanf("%f",&x);a = x/10;switch(a){case 9:printf("成绩为%.2f,等级为A",x);break;case 8:printf("成绩为%.2f,等级为B",x);break;case 7:printf("成绩为%.2f,等级为C",x);break;case 6:printf("成绩为%.2f,等级为D",x);break;default:printf("成绩为.2%f,等级为E",x);break;}}

9、

#include <stdio.h>#include <string.h>int main(){char str[100];scanf("%s",str);int x = strlen(str);printf("该数是%d位数\n",x);for (int i = 0; i < x; ++i) {printf(" %c ",str[i]);}printf("\n");for(int i = x-1;i>=0; i--)printf(" %c ",str[i]);}

11、输入四个整数,按从小到大排序

#include <stdio.h>int main(){int e[3];int a,b,c,d,temp,index;scanf("%d,%d,%d,%d",&a,&b,&c,&d);e[0] = a;e[1] = b;e[2] = c;e[3] = d;for (int i = 0; i < 3; ++i) {for (int j = i+1; j < 4; ++j) {if(e[j]>e[i]){temp = e[i];e[i]=e[j];e[j]=temp;}}}for (int i = 0; i < 4; ++i) {printf(" %d ",e[i]);}}

12、有四个圆塔,圆心分别为

#include<stdio.h>int main(){int h=10;float x1 = 2, y1 = 2, x2 = -2, y2 = 2, x3 = -2, y3 = -2, x4 = 2, y4 = -2, x, y, d1, d2, d3, d4;printf("请输入1个点(x,y):");scanf("%f,%f",&x,&y);d1 = (x - x1)*(x - x1) + (y - y1)*(y - y1);d2 = (x - x2)*(x - x2) + (y - y2)*(y - y2);d3 = (x - x3)*(x - x3) + (y - y3)*(y - y3);d4 = (x - x4)*(x - x4) + (y - y4)*(y - y4);if (d1 > 1&& d2 > 1 && d3 > 1 && d4 > 1)h = 0;printf("该点高度为:%d\n",h);}

第五章

3、请输入两个正整数m,n,求最大公约数和最小公倍数

#include<stdio.h>int main(){int max(int a,int b);int min(int a,int b);int m,n,x,y;int e;int res = 1;scanf("%d,%d",&m,&n);x = max(m,n);y = min(m,n);e = x - y;while(e>y){e = e - y;}if (e == 0)printf("%d",y);for (int i = 2; i <= e; ++i){if(x%i==0&&e%i==0)res = i;}printf("公约数%d",res);x = m/e;y = n/e;x = x*y*e;printf("公倍数%d",x);}int max(int a,int b){int x;x = a>b?a:b;return x;}int min(int a,int b){int x;x = a<b?a:b;return x;}

4、输入一行字符,分别统计其中英文字母,空格、数字和其它字符个数

#include<stdio.h>#include "string.h"#include "ctype.h"int main(){char str[100];int alp=0,num=0,kong=0,oth=0;gets(str);for (int i = 0; i < strlen(str); ++i) {if (isalpha(str[i]))alp++;else if (isdigit(str[i]))num++;else if (str[i]==' ')kong++;}oth = strlen(str)-alp-num-kong;printf("有%d个字母,%d个数字,%d个空格,%d个其他字符",alp,num,kong,oth);}

5、求Sn = a+aa+aaa+…+aa…a之值

#include<stdio.h>#include "math.h"int main(){double a,n;double res = 0;double x = 1;scanf("%lf,%lf",&a,&n); //使用%lf格式控制输入a,n成功赋值 for (int i = 0; i < n; ++i){if (i != 0)x = x + pow(10,i); //pow内参数为double型 res = res + x*a;}printf("%.0f\n",res);}

6、求1!+2!+3!+…+n!

#include<stdio.h>int main(){int n,res=0;int a;scanf("%d",&n);for (int i = 0; i < n; ++i){int s = 1;for (int j = 1; j <= i+1; ++j){s = j*s;}res = res+s;}printf("%d",res);}

7、

#include<stdio.h>int main(){double Jie(double n,double x);double Jie2(double n,double x);double k;scanf("%lf",&k);printf("%.2lf", Jie(100,k)+ Jie(50,k*k)+Jie2(10,k));}double Jie(double n,double x){double res = 0;for (int i = 1; i <= n; ++i){res = res+i;}return res;}double Jie2(double n,double x){double res = 0;for (double i = 1.0; i <=n ; ++i) //使用1.0才可得到小数{res = res+1/i;printf("--%lf--",res);}return res;}

8、输出所有的水仙花数

#include<stdio.h>#include "math.h"int main(){int a,b,c;for (int i = 100; i <= 999; ++i){a = i/100;b = (i-a*100)/10;c = i%10;if(pow(a,3)+pow(b,3)+pow(c,3)==i)printf("%d\t",i);}}

9、输出1000以内的完数

#include<iostream>using namespace std;int main(){int get(int a,int sys[]);int sys[50];for(int i = 1;i<1001;i++){get(i,sys);}}int get(int a,int sys[]){int x = 0;int result = 0;for(int i = 1;i<a;i++){if(a%i == 0){sys[x] = i;x++;}}for(int i = 0;i<x;i++)result = result+sys[i];if(result == a){cout<<a<<"its factors are";for(int i = 0;i<x;i++)cout<<" "<<sys[i];cout<<"\n";}}

10、求数列前20项之和

#include<iostream>using namespace std;int main(){double a = 2;double b = 3;double x;double result = 3.5;for(int i = 0;i<18;i++){result = result + ((a+b)/b);x = a;a = b;b = x+b;}cout<<result;}

11、一个球从100m的高度自由落下,每次落地反弹原高度一半

#include<iostream>using namespace std;double x = 100.0;double y = 50.0;int main(){double total=100;for(int i = 0;i<9;i++){total = total + x/2;x = x/2;}total = total+50;for(int i = 0;i<8;i++){total = total + y/2;y = y/2;}cout<<"total is"<<total<<"\n";double x = 100;for(int i = 0;i<10;i++)x = x/2;cout<<"the 10 rebound high is"<<x<<"\n";}

12、猴子吃桃问题

#include<iostream>using namespace std;int main(){int x = 1;for(int i=0;i<9;i++){x = (x+1)*2;}cout<<"the first day get peaches are"<<x;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。