《C程序设计(第五版)》谭浩强编著—习题—第五章
作者:互联网
3.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, temp, i, j, maxDivisor, minMultiple;
printf("请输入两个正整数\n");
scanf("%d %d", &a,&b);
//把较小的数保存在a
if(a > b){
temp = a;
a = b;
b = temp;
}
//最大公约数
for (i = a; i > 1; i--){
if((a%i == 0)&&(b%i == 0)){
maxDivisor = i;
break;
}
}
//最小公倍数
for(j = b; j < a*b; j++){
if((j%a == 0)&&(j%b == 0)){
minMultiple = j;
break;
}
}
printf("最大公约数为: %d\n", maxDivisor);
printf("最小公倍数为: %d\n", minMultiple);
return 0;
}
4.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int letter,space,number,other;
letter=space=number=other=0;
printf("请输入一行字符:\n");
while((ch=getchar()) != '\n'){
if((ch>='A'&& ch<='Z')||(ch>='a'&&ch<='z')){
//字母
letter++;
} else if (ch == ' '){
space++;
} else if(ch>='0'&& ch<='9'){
number ++;
} else {
other ++;
}
}
printf("英文字母个数为:%d\n", letter);
printf("空格个数为:%d\n", space);
printf("数字个数为:%d\n", number);
printf("其他字符个数为:%d\n", other);
return 0;
}
5.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,n,Sn=0,Tn=0,i=1;
printf("请输入a,n的值:\n");
scanf("%d %d", &a, &n);
while(i<=n){
Tn = Tn+a;
Sn = Sn+Tn;
a = a*10;
i++;
}
printf("Sn= %d", Sn);
return 0;
}
6.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
double eachResult = 1, result = 0;
printf("请输入n的值: \n");
scanf("%d", &n);
for(i = 1; i <= n; i++){
eachResult = eachResult * i;
result += eachResult;
}
printf("阶乘和为:%f", result);
return 0;
}
7.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
double k,result = 0;
for(i = 1; i <= 100; i++){
result += i;
}
for(j = 1; j <= 50; j++){
result += j*j;
}
for(k = 1; k <= 10; k++){
result += 1/k;
}
printf("%f", result);
return 0;
}
注: 假如在此程序中将k值设置为int类型,会出现错误
8.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, firstP, secondP, lastP;
for ( i = 100; i < 1000; i++)
{
firstP = i/100;
secondP = (i%100)/10;
lastP = i%10;
if ((firstP*firstP*firstP + secondP*secondP*secondP + lastP*lastP*lastP)==i)
{
printf("%d\n",i);
}
}
return 0;
}
9.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k, factor;
for(i = 1; i < 1000; i++){
factor = 0;
for(j = 1; j <= i/2; j++){
if(i%j == 0){
factor += j;
}
}
if(factor == i){
printf("%d its factors are ", i);
for(k = 1; k <= i/2; k++){
if(i%k == 0){
printf(" %d ",k);
}
}
printf("\n");
}
}
printf("Hello world!\n");
return 0;
}
10.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double molecule,denominator,sum,temp;
int i;
molecule=2;
denominator=1;
sum = molecule/denominator;
for(i = 1; i < 20; i++){
temp = molecule;
molecule +=denominator;
denominator = temp;
sum += molecule/denominator;
}
printf("前20项的和为 %f\n",sum);
return 0;
}
11.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
double height = 100, distance = 0;
for(i = 1; i <= 10; i++ ){
distance += height;
height /= 2;
distance += height;
}
printf("第十次落地时共经过 %f米 \n", distance-height);
printf("第十次落地时弹起的高度为 %f米 \n", height);
return 0;
}
12.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,sum = 1;
for(i = 9; i >= 1; i--){
sum = (sum+1)*2;
}
printf("总共有%d个桃子\n", sum);
return 0;
}
13.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double a,x1,x2;
printf("请输入a的值\n");
scanf("%lf", &a);
x1 = a/2;
x2 = (x1+a/x1)/2;
do{
x1 = x2;
x2 = (x1+a/x1)/2;
} while(fabs(x1-x2)>=1e-5);
printf("平方根为%f",x2);
return 0;
}
14
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double x1,x2,fx, fdx;
x1 = 1.5;
do{
x1 = x2;
fx = 2*pow(x1,3)-4*pow(x1,2)+3*x1-6;
fdx = 6*pow(x1,2)-8*x1+3;
x2 = x1 -fx/fdx;
}while(fabs(x2-x1)>=1e-5);
printf("根为%f", x2);
return 0;
}
15.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double x1,x2,x0,fx1,fx2,fx0;
x1=-10,x2=10;
fx1 = 2*pow(x1,3)-4*pow(x1,2)+3*x1-6;
fx2 = 2*pow(x2,3)-4*pow(x2,2)+3*x2-6;
do{
x0 = (x1+x2)/2;
fx0 = 2*pow(x0,3)-4*pow(x0,2)+3*x0-6;
if (fx0*fx1 < 0){
x2 = x0;
fx2 = fx0;
} else if (fx0*fx2 < 0){
x1 = x0;
fx1 = fx0;
}
}while(fabs(fx0)>= 1e-5);
printf("x = %f",x0);
return 0;
}
17.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char i,j,k;
for(i='X';i<='Z';i++){
for(j='X';j<='Z';j++){
if(i!=j){
for(k='X';k<='Z';k++){
if(i!=k&&j!=k){
if(i!='X'&&k!='X'&&k!='Z'){
printf("A--%c\nB--%c\nC--%c\n",i,j,k);
}
}
}
}
}
}
return 0;
}
溪午不闻钟_
发布了20 篇原创文章 · 获赞 0 · 访问量 368
私信
关注
标签:main,include,int,x1,谭浩强,printf,x2,习题,第五版 来源: https://blog.csdn.net/bruce_zhao1407/article/details/104580532