# C语言程序设计精髓习题总汇（上）

C语言 专栏收录该内容
5 篇文章 8 订阅

## 第一章 认识变量和常量

##### 1.1 hello world!

#include <stdio.h>
int main()
{
printf(“hello world!\n”);
return 0;
}

##### 1.2 在屏幕上输出多行信息

hello world!
hello hit!
Hello everyone!

#include <stdio.h>
int main()
{
printf(“hello world!\n”);
printf(“hello hit!\n”);
printf(“hello everyone!\n”);

return 0;
}

##### 1.3 计算半圆弧长以及半圆的面积

#include<stdio.h>
#define PI 3.14159
int main()
{
double r = 5.3,Area,circumference;

printf(“Area = %.5f\n”,PI*r*r/2);
printf(“circumference = %.5f\n”,PI*(r+r)/2);
}

##### 1.4 计算长方形体积

#include<stdio.h>
int main()
{
double const l = 1.2,w = 4.3,h = 6.4;
double v ;
v = l*w*h;
printf(“volume = %.3f\n”,v);
}


## 第二章 计算

##### 2.1 输出逆序数

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,e,sum;
printf("Input e:");
scanf("%d",&e);
a=fabs(e/100);
b=fabs((e/10)%10);
c=fabs(e%10);
sum=c*100+b*10+a;
printf("a=%d,b=%d,c=%d,sum=%d\n",a,b,c,sum);
return 0;
}


#include <stdio.h>
int main()
{
printf("Input x:\n");
int x,a,b,c;
scanf("%d",&x);
while (x < 0)
{
x = x * -1 ;
}
a = x % 10 ;
b=x%100/10;
c=x/100;
printf("y=%d\n",a*100+b*10+c);
return 0;
}


##### 2.2 计算总分和平均数

#include<stdio.h>
int main()
{
int a[5]={86,74,92,77,82};
int b[5]={81,87,90,62,88};
float result=0,result1=0,result2=0;
for(int i=0;i<5;i++)
{
result1+=a[i];
result2+=b[i];
}
result=result1*0.3+result2*0.7;
printf("total=%.2f\n",result);
printf("average=%.2f\n",result/5.0);
printf("average=%d\n",(int)result/5);
return 0;
}

##### 2.3 存款利率计算器V1.0

#include<stdio.h>
#include <math.h>
int main()
{
double capital,deposit,rate;
int year;
printf( "Please enter rate, year, capital:\n");
scanf("%lf,%d,%lf", &rate, &year, &capital);
deposit=capital*pow(1+rate,year);
printf("deposit=%.3f\n",deposit);
return 0;
}


##### 2.4 数位拆分v1.0

#include<stdio.h>
#include <math.h>
int main()
{
int x=4321,a,b;
a=x/100;
b=x%100;
printf("a=%d,b=%d\n",a,b);
printf("a+b=%d\n",a+b);
printf("a-b=%d\n",a-b);
printf("a*b=%d\n",a*b);
printf("a/b=%.2f\n",(float)a/b);
printf("a%%b=%d\n",a%b);
return 0;
}


##### 2.5 求正/负余数

#include<stdio.h>
int main()
{
printf("negative: %d\n",-11%5);
printf("positive: %d\n",-11%5+5);
return 0;
}


##### 2.6 身高预测

#include<stdio.h>
int main()
{
int mf=175,mm=162,hf=169,hm=153;
printf("Height of xiao ming:%d\n",(int)((mf + mm)*0.54));
printf("Height of xiao hong:%d\n",(int)((hf*0.923 + hm)/2));
return 0;
}


##### 2.7 求一元二次方程的根

#include<stdio.h>
#include<math.h>
int main()
{
int a=2,b=3,c=1;
double x1,x2;
x1=-b/2.0/a;
x2=sqrt(b*b-4*a*c)/2.0/a;
printf("x1=%.4f\n",x1+x2);
printf("x2=%.4f\n",x1-x2);
return 0;
}



## 第三章 输入输出

##### 3.1 日期显示

#include<stdio.h>
int main()
{
int mm,dd,yy;
printf("Enter a date (mm/dd/yy):\n");
scanf("%d/%d/%d",&mm,&dd,&yy);
printf("You entered the date: %04d.%02d.%02d\n",yy,mm,dd);
return 0;
}

##### 3.2 产品信息格式化

Enter item number:385↙
Enter unit price:12.5↙
Enter purchase date (mm/dd/yy):12/03/2015↙
Item Unit Purchase
385 $12.50 12032015 #include<stdio.h> int main() { int number,mm,dd,yy; float price; printf("Enter item number:\n"); scanf("%d",&number); printf("Enter unit price:\n"); scanf("%f",&price); printf("Enter purchase date (mm/dd/yy):\n"); scanf("%d/%d/%d",&mm,&dd,&yy); printf("Item Unit Purchase\n"); printf("%-9d$ %-9.2f%02d%02d%02d\n",number,price,mm,dd,yy);
return 0;
}

##### 3.3 计算两个数的平方和

#include<stdio.h>
#include<math.h>
int main()
{
float x,y;
scanf("%f,%f",&x,&y);
printf("Result=%.2f\n",pow(x,2.0)+pow(y,2.0));
return 0;
}

##### 3.4 逆序数的拆分计算

#include<stdio.h>
#include<math.h>
int main()
{
int x,y,x1,x2,x3,x4,a,b;
printf("Input x:\n");
scanf("%d",&x);
y=abs(x);
x1=y/1000;
x2=y/100%10;
x3=y/10%10;
x4=y%10;
y=x1+10*x2+100*x3+1000*x4;
a=x4*10+x3;
b=x2*10+x1;
printf("y=%d\n",y);
printf("a=%d,b=%d\n",a,b);
printf("result=%d\n",(int)pow(a,2)+(int)pow(b,2));
return 0;
}

##### 3.5 拆分英文名

#include<stdio.h>
int main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("%c%c%c\n",a-32,b,c);
printf("%c:%d\n",a,a-'a'+1);
printf("%c:%d\n",b,b-'a'+1);
printf("%c:%d\n",c,c-'a'+1);
return 0;
}

##### 3.6 计算体指数

#include<stdio.h>
int main()
{
int weight,height;
float h,t;
printf("Input weight, height:\n");
scanf("%d,%d",&weight,&height);
printf("weight = %d\n",weight*2);
h=height/100.0;
printf("height = %.2f\n",h);
printf("t=%.2f\n",weight/h/h);
return 0;
}

##### 3.7 学分绩计算

#include<stdio.h>
int main()
{
int a,b,c;
printf("Input math1, English and math2:");
scanf( "%d,%d,%d",&a,&b,&c);
printf("Final score = %.2f\n",(a*5+b*1.5+c*3.5)/10);
return 0;
}

##### 3.8 一尺之锤，日取其半

#include<stdio.h>
int main()
{
int b;
float a;
printf("Input length and days:");
scanf("%f,%d",&a,&b);
for(b>0;b--;)
{
a/=2;
}
printf("length=%.5f\n",a);
return 0;
}

##### 3.9 网购打折商品V1.0

#include<stdio.h>
int main()
{
float price;
printf("Input payment p:");
scanf("%f",&price);
printf("price = %.1f\n",price*(1-0.08));
return 0;
}

##### 3.10 计算时间差V1.0

#include<stdio.h>
#include<math.h>
int main()
{
typedef struct clock
{
int hour;
int minute;
int second;
} CLOCK;
struct clock stu1;
struct clock stu2;
struct clock stu3;

printf("Input time one(hour, minute):");
scanf("%d,%d", &stu1.hour,&stu1.minute);

printf( "Input time two(hour, minute):");
scanf("%d,%d", &stu2.hour, &stu2.minute);

stu3.hour = fabs((stu1.hour * 60 + stu1.minute) - (stu2.hour * 60 + stu2.minute)) / 60;
stu3.minute = fabs(((stu1.hour * 60 + stu1.minute) - (stu2.hour * 60 + stu2.minute)) % 60);

printf("%d hour %d minute\n", stu3.hour, stu3.minute);
}


## 第四章 选择

##### 4.1 检测用户错误输入

#include<stdio.h>
#include<stdlib.h>
int main()
{
int c;
int a,b;
c = scanf("%d %d",&a,&b);
if(c==2)
printf("a = %d,b = %d\n",a,b);
else
printf("input error!");
return 0;
}

##### 4.2 闰年判断

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf("%d",&a);
if(a>=1)
{
if(a%4==0&&a%100!=0||a%400==0)
printf("Yes\n");
else
printf("No\n");
}
else
printf("Input error!\n");
return 0;
}

##### 4.3 程序改错

//错误代码
#include<stdio.h>
int main()
{
int score;
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
else if (score >= 80)
else if (score >= 70)
else if (score >= 60)
else
return 0;
}

//更改后代码
#include<stdio.h>
int main()
{
int score,x;
x=scanf("%d", &score);
if (score < 0 || score > 100 || x!=1)
{
printf("Input error!\n");
return 0;
}
else if (score >= 90)
else if (score >= 80)
else if (score >= 70)
else if (score >= 60)
else
return 0;
}

##### 4.4 字符类型判断

#include<stdio.h>
int main()
{
char ch;
printf("Input simple:\n");
scanf("%c",&ch);
if(ch<='z'&&ch>='a'||ch<='Z'&&ch>='A')
printf("It is an English character.\n");
else if(ch<='9'&&ch>='0')
printf("It is a digit character.\n");
else
printf("It is other character.\n");
return 0;
}

##### 4.5 快递费用计算

1） 起重（首重）1公斤按起重资费计算（不足1公斤，按1公斤计算），超过首重的重量，按公斤（不足1公斤，按1公斤计算）收取续重费；
（2）同城起重资费10元，续重3元/公斤；
（3）寄往1区（江浙两省）的邮件，起重资费10元，续重4元；
（4）寄往其他地区的邮件，起重资费统一为15元。而续重部分，不同区域价格不同：2区的续重5元/公斤，3区的续重6.5元/公斤，4区的续重10元/公斤。

#include <stdio.h>
#include <stdlib.h>
int main()
{
int mark;
float weight,price;
scanf("%d,%f",&mark,&weight);
if(mark<0||weight<=0||mark>4)
printf("Error in Area\n");
else
{
if(weight<=1)
weight=1;
else
weight=(int)weight+1;
}
switch(mark)
{
case 0 : price=10+(int)(weight-1)*3;
break;
case 1 : price=10+(int)(weight-1)*4;
break;
case 2 : price=15+(int)(weight-1)*5;
break;
case 3 : price=15+(int)(weight-1)*6.5;
break;
case 4 : price=15+(int)(weight-1)*10;
break;
}
printf("Price: %5.2f\n",price);
return 0;
}

##### 4.6 数位拆分v2.0

#include<stdio.h>
int main()
{
int n,a,b;
scanf("%d",&n);
a=n/100;
b=n%100;
printf("%d,%d\n",a,b);
printf("sum=%d,sub=%d,multi=%d\n",a+b,a-b,a*b);
if(b==0) printf("The second operator is zero!\n");
else printf("dev=%.2f,mod=%d\n",a/1.0/b,a%b);
return 0;
}

##### 4.7 出租车计价

#include<stdio.h>
int main()
{
float dis,fee=0;
int tim;
printf("Input distance and time:");
scanf("%f,%d",&dis,&tim);
tim=tim/5;
fee+=2*tim;
if(dis>10){
fee+=3*(dis-10);
}
if(dis>3){
dis=dis>=10?10:dis;
fee+=2*(dis-3);
}
if(dis>0){
fee+=8;
}
printf("fee = %.0f\n",fee);
return 0;
}

##### 4.8 数据区间判断

#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
if(x<=0||x>=10000) printf("error!\n");
else if(x>=0&&x<10) printf("%d: 0-9\n",x);
else if(x>=10&&x<100) printf("%d: 10-99\n",x);
else if(x>=100&&x<1000) printf("%d: 100-999\n",x);
else if(x>=1000&&x<10000) printf("%d: 1000-9999\n",x);
return 0;
}

##### 4.9 计算一元二次方程的根v2.0

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,x,y;
scanf("%f,%f,%f",&a,&b,&c);
x=-b/2/a;
y=b*b-4*a*c;
if(y<0)
printf("error!\n");
else
printf("x1=%7.4f, x2=%7.4f\n",x+sqrt(y)/2/a,x-sqrt(y)/2/a);
return 0;
}

##### 4.10 分数比较

#include<stdio.h>
int main()
{
int a,b,c,d;
int fenshu,fenshu2;
printf("Input a/b, c/d:");
scanf("%d/%d,%d/%d",&a,&b,&c,&d);
fenshu=a*d;
fenshu2=c*b;
if(fenshu>fenshu2)
printf("%d/%d>%d/%d\n",a,b,c,d);
else if(fenshu2>fenshu)
printf("%d/%d<%d/%d\n",a,b,c,d);
else
printf("%d/%d=%d/%d\n",a,b,c,d);
return 0;
}

##### 4.11 存款利率计算器v2.0

deposit = capital * (1 + rate)^n

#include<stdio.h>
#include<math.h>
int main()
{
double capital , deposit , rate ;
int n;
char a;
printf("Input rate, year, capital:");
scanf("%lf,%d,%lf",&rate,&n,&capital);
printf("Compound interest (Y/N)?");
scanf("%c",&a);
if (a=='y'||a=='Y')
{
deposit=capital*pow(1+rate,n);
printf("deposit = %.4f\n",deposit);
}
else if(a=='n'||a=='N')
{
deposit=capital*(1+rate*n);
printf("deposit = %.4f\n",deposit);
}
else
printf("error");
return 0;
}

##### 4.12 博弈论之Best Response

Input percent of A and B:0.2 0.8↙
compete = 6.8000
standard = 9.6000
The Best Response is standard!

Input percent of A and B:0.8 0.2↙
compete = 9.2000
standard = 8.4000
The Best Response is compete!

Input percent of A and B:0.5 0.5↙
compete = 8.0000
standard = 9.0000
The Best Response is standard!

#include<stdio.h>
int main()
{
float a,b,compete,standard;
printf("Input percent of A and B:");
scanf("%f%f",&a,&b);
compete=a*10+b*6;
standard=a*8+b*10;
printf("compete = %.4f\nstandard = %.4f\n",compete,standard);

if(compete>= standard)
printf("The Best Response is compete!");

else
printf("The Best Response is standard!");

return 0;
}


## 第五章 循环

##### 5.1 判断一个整型数据有几位v2.0

12226↙

12226: 5 bits

1: 1

2: 3

6: 1

-12243↙

-12243: 5 bits

1: 1

2: 2

3: 1

4: 1

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,a,b,c,d,e,f,g,h,i,j,counter=0,num;
a=0;
b=0;
c=0;
d=0;
e=0;
f=0;
g=0;
h=0;
i=0;
j=0;
scanf("%d",&n);
num=n;
do{
n=abs(n);
switch(n%10)
{
case 0:a++;break;
case 1:b++;break;
case 2:c++;break;
case 3:d++;break;
case 4:e++;break;
case 5:f++;break;
case 6:g++;break;
case 7:h++;break;
case 8:i++;break;
default :j++;
}
n=n/10;
}while(n);
counter=a+b+c+d+e+f+g+h+i+j;
printf("%d: %d bits\n",num,counter);
if(a){printf("0: %d\n",a);}
if(b){printf("1: %d\n",b);}
if(c){printf("2: %d\n",c);}
if(d){printf("3: %d\n",d);}
if(e){printf("4: %d\n",e);}
if(f){printf("5: %d\n",f);}
if(g){printf("6: %d\n",g);}
if(h){printf("7: %d\n",h);}
if(i){printf("8: %d\n",i);}
if(j){printf("9: %d\n",j);}
return 0;
}


##### 5.2 奖金计算

#include <stdio.h>
int main()
{
long x, y, s;
scanf("%ld", &x);
if (x < 100000 && x >=0)
s = 1;
else if (x < 200000)
s = 2;
else if (x < 400000)
s = 3;
else if (x < 600000)
s = 4;
else if (x < 1000000)
s = 5;
else if (x >= 1000000)
s = 6;
else
s = -1;
switch(s)
{
case 1: y = x * 0.1;
break;
case 2: y = 10000 + (x - 100000) * 0.075;
break;
case 3: y = 17500 + (x - 200000) * 0.05;
break;
case 4: y = 27500 + (x - 400000) * 0.03;
break;
case 5: y = 33500 + (x - 600000) * 0.015;
break;
case 6: y = 39500 + (x - 1000000) * 0.01;
break;
default:
printf("Input error!");
}
printf("bonus=%ld\n", y);
return 0;
}


##### 5.3 程序修改—1

#include  <stdio.h>
int main()
{
int i, j, sum = 0, n=100;
for (i=1,j=n; i<=j; i++,j--)
{
sum = sum + i + j;
}
printf("sum = %d", sum);
return 0;
}


//修改后
#include <stdio.h>
int main()
{
int i, j, sum = 0, n;
scanf("%d", &n);
for(i=1,j=n;i<=j;i++,j--)
{
if(i==j)
j = 0;
sum = sum + i + j;
}
printf("sum = %d", sum);
return 0;
}


##### 5.4 程序修改—2

#include  <stdio.h>
int main()
{
int sum = 0, m;
do{
printf("Input m:\n");
scanf("%d", &m);
sum = sum + m;
printf("sum = %d\n", sum);
}while (m != 0);
return 0;
}


//修改后
#include <stdio.h>
int main()
{
int sum = 0, m;
printf("Input m:\n");
scanf("%d", &m);
while (m!=0)
{
sum = sum + m;
printf("sum = %d\n", sum);
printf("Input m:\n");
scanf("%d", &m);
}
return 0;
}


##### 5.5 程序改错—1

#include <stdio.h>
int main()
{
int x, y, z;
for (x=0; x<=20; x++)
{
for (y=0; y<=33; y++)
{
z = 100 - x - y;
if (5*x + 3*y + z/3 == 100)
{
printf("x=%d, y=%d, z=%d\n", x, y, z);
}
}
}
return 0;
}


//修改后
#include <stdio.h>
int main()
{
int x, y, z;

for (x=0; x<=20; x++)
{
for (y=0; y<=33; y++)
{
z = 100 - x - y;
if (5*x + 3*y + (float)z/3 == 100)
{
printf("x=%d, y=%d, z=%d\n", x, y, z);
}
}
}
return 0;
}


##### 5.6 程序改错—2

#include <stdio.h>
#include <math.h>
int main()
{
int n, i;
printf("Input n:\n");
scanf("%d", &n);
for (i=2; i<=sqrt(n); i++)
{
if (n % i = 0)
{
printf("No!\n");
}
}
printf("Yes!\n");
return 0;
}


//修改后
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, find = 0;
printf("Input n:\n");
scanf("%d", &n);
for (i=2;i<=sqrt(n);i++)
{
if (n%i == 0)
{
find = 1;
}
}
if(!find && n > 1)
printf("Yes!\n");
else
printf("No!\n");
return 0;
}


##### 5.7 程序改错—3

#include <stdio.h>
int main()
{
int x1, x2;
do{
printf("Input x1, x2:");
scanf("%d,%d", &x1, &x2);
}while (x1 * x2 > 0);
printf("x1=%d,x2=%d\n", x1, x2);
return 0;
}


//修改后
#include<stdio.h>
int main()
{
int  x1, x2, r;
do{
printf("Input x1, x2:\n");
r = scanf("%d,%d", &x1, &x2);
while (r != 2)
{
while (getchar() != '\n');
printf("Input x1, x2:\n");
r = scanf("%d,%d", &x1, &x2);
}
}while(x1 * x2 >= 0);
printf("x1=%d,x2=%d\n", x1, x2);
return 0;
}


##### 5.8 猴子吃桃程序_扩展1

#include <stdio.h>
int main()
{
int n, i, x = 1;
printf("Input days:\n");
scanf("%d", &n);
for(i=1;i<n;i++)
{
x = (x + 1) * 2;
}
printf("x=%d\n", x);
return 0;
}


##### 5.9 猴子吃桃程序_扩展2

#include <stdio.h>
int main()
{
int n, i, x = 1, r;
printf("Input days:\n");
r = scanf("%d", &n);
while(1)
{
while(r != 1)
{
while(getchar() == '\n');
printf("Input days:\n");
r = scanf("%d", &n);
}
if (n < 1 && r == 1)
{
printf("Input days:\n");
r = scanf("%d", &n);
}
else
{
for(i=1;i<n;i++)
{
x = (x + 1) * 2;
}
printf("x=%d\n", x);
goto end;
}
}
end:
return 0;
}


##### 5.10 6位密码输入检测

#include <stdio.h>
int main()
{
int a=0;
char n;
do{
scanf("%c",&n);
if(n>='0'&& n<='9')
{
a++;
printf("%c, you have enter %d-bits number\n",n,a);
}
else
{
printf("error\n");
}
getchar();
}while(a<6);
return 0;
}


##### 5.11 判断一个整型数据有几位

#include <stdio.h>
#include <math.h>
int main()
{
int n, x, count = 1;
scanf("%d", &n);
x = fabs(n);
while (x > 10)
{
count++;
x = x/10;
}
printf("%d: %d bits\n", n, count);
return 0;
}


##### 5.12 检测输入数据中奇数和偶数的个数

#include <stdio.h>
int main()
{
int x, odd = 0, even = 0;
scanf("%d", &x);
if (x == -1)
{
printf("over!\n");
}
while (x != -1)
{
if (x % 2 == 0)
{
even++;
printf("%d:even\n", x);
scanf("%d", &x);
}
else
{
odd++;
printf("%d:odd\n", x);
scanf("%d",&x);
}
}
printf("The total number of odd is %d\n", odd);
printf("The total number of even is %d\n", even);
return 0;
}


##### 5.13 计算球的反弹高度

#include <stdio.h>
int main()
{
int n;
float h = 100, m = 100;
printf("Input:\n");
scanf("%d", &n);
printf("%d times:\n", n);
while (n-- > 1)
{
h = h / 2;
m = m + h * 2;
}
printf("%.3f\n", m);
printf("%.3f\n", h / 2);
return 0;
}


##### 5.14 马克思手稿中的趣味数学题

#include <stdio.h>
int main()
{
int x, y, z;
printf("Man   Women   Children\n");
for (z=1;z<30;z++)
{
x = z - 10;
y = 40 - 2 * z;
if (x >= 0 && y >= 0)
{
printf("%3d%8d%8d\n", x, y, z);
}
}
return 0;
}


##### 5.15 猜神童年龄

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int cmp ( const void *a, const void *b )
{
return *(int *)a - *(int *)b;
}
int main(void)
{
int i, a[SIZE], b[SIZE], age, flag;
long n1, n2;
for (i=0; i<10; i++)
a[i]=i;
for (age=10; age<=22; age++)
{
n1=age*age*age;
n2=n1*age;
flag=1;
i=0;
do
{
b[i]=n1%10;
n1=n1/10;
i++;
}
while(n1>0);
do
{
b[i]=n2%10;
n2=n2/10;
i++;
}
while(n2>0);
qsort(b, SIZE, sizeof(b[0]),cmp);
for(i=0; i<10; i++)
{
if(a[i]!=b[i])
{
flag=0;
break;
}
}
if(flag)
printf("age=%d\n",age);
}
return 0;
}


##### 5.16 闰年相关的问题v3.0——计算有多少闰年

#include <stdio.h>
int main()
{
int x, y, count = 0;
scanf("%d", &x);
printf("Input this year:");
scanf("%d", &y);
for (x;x<=y;x++)
{
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
{
printf("%d\n", x);
count++;
}
}
printf("count=%d\n", count);
return 0;
}


##### 5.17 闰年相关的问题v4.0——计算心跳数

#include <stdio.h>
int main()
{
int x, y, count = 0, year, day;
unsigned long beat;
scanf("%d", &x);
printf("Input this year:");
scanf("%d", &y);
year = y - x;
for (x;x<y;x++)
{
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
{
count++;
}
}
day = year * 365 + count;
beat = day * 24 * 60 * 75;
printf("The heart beats in your life: %lu", beat);
return 0;
}


## 第六章 函数

##### 6.1绘制金字塔

A
ABA
ABCBA
ABCDCBA

D↙

____A

___ABA

__ABCBA

_ABCDCBA

F↙

______A

_____ABA

____ABCBA

___ABCDCBA

__ABCDEDCBA

_ABCDEFEDCBA

（说明：上面运行结果示例中，每行字母前面的下划线"_"代表屏幕上实际输出的是空格，最后一行前面有一个空格，倒数第二行有两个空格，以此类推。）

#include <stdio.h>
int main()
{
char c;
int q, m, n, z;
scanf(" %c", &c);
for(m=65;m<=c;m++)//控制行数
{
for(q=c-m;q>=0;q--)//控制空格输出
{
printf(" ");
}
for(n=65;n<=m;n++)//控制顺序输出
{
printf("%c", n);
}
for(z=m-1;z>=65;z--)//控制逆序输出
{
printf("%c", z);
}
printf("\n");
}
return 0;
}


##### 6.2循环嵌套的应用

F
FE
FED
FEDC
FEDCB
FEDCBA

#include <stdio.h>
int main()
{
char c='F';
int m, n, z, o;
for(m=65,o=1;m<=c;m++,o++)//控制行数
{
for(z=c,n=o;z>=65,n>0;z--,n--)//控制逆序输出
{
printf("%c", z);
}
printf("\n");
}
return 0;
}


##### 6.3利用泰勒级数计算sinx的值

Input x:
3↙
sin(x)=0.141,count=9

Input x:
10↙
sin(x)=-0.544,count=18

#include <stdio.h>
#include <math.h>
#define EPS 1e-5
double R(double a);
int main()
{
double x, sinx = 0, n, mul, count = 0,sign = 1, a = 1;
printf("Input x:\n");
scanf("%lf", &x);
n = x;
do{
mul = n/R(a);
mul = mul * sign;
sinx = sinx + mul;
n = n * x * x;
a = a + 2;
sign = -sign;
count++;
}while (fabs(mul) >= EPS);
printf("sin(x)=%.3f,count=%d\n", (float)sinx, (int)count);
return 0;
}
double R(double a)
{
double s = 1, r;
for(r=1;r<=a;r++)
{
s = s * r;
}
return (s);
}


##### 6.4计算100~200之间的所有素数之和

#include <stdio.h>
#include <math.h>
int fun(int m)
{
int a, ret;
for(a=2;a<=sqrt(m);a++)
{
if (m % a == 0)
{
ret = 0;
break;
}
if (m % a != 0)
{
ret = 1;
}
}
return (ret);
}
int main()
{
int sum = 0, m, sign;
for(m=100;m<=200;m++)
{
sign = fun(m);
if (sign == 1)
{
sum = sum + m;
}
else
continue;
}
printf("sum=%d\n", sum);
return 0;
}


##### 6.5编程实现一个输入指定范围内的整数的函数

#include <stdio.h>
int getint(int min, int max);
int main()
{
int min, max, ret;
scanf("%d,%d", &min, &max);
printf("Please enter an integer [%d..%d]:\n", min, max);
ret = getint(min, max);
printf("The integer you have entered is:%d\n", ret);
return 0;
}
int getint(int min, int max)
{
int a;
scanf("%d", &a);
while (a>=max || a<=min)
{
printf("Please enter an integer [%d..%d]:\n", min, max);
scanf("%d", &a);
}
return (a);
}


##### 6.6程序改错v2.0

#include<stdio.h>
int main()
{
int score;
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
else if (score >= 80)
else if (score >= 70)
else if (score >= 60)
else
return 0;
}


//修改后
#include <stdio.h>
int main()
{
int x, ret;
char a;
ret = scanf("%d", &x);
while (ret != 1 || x<0 || x>100)
{
while (getchar() != '\n');
printf("Input error!\n");
ret = scanf("%d", &x);
}
if (x>=90)
{
a = 'A';
}
else if (x>=80)
{
a = 'B';
}
else if (x>=70)
{
a = 'C';
}
else if (x>=60)
{
a = 'D';
}
else
{
a = 'E';
}
return 0;
}


##### 6.7编程计算a+aa+aaa+…+aa…a（n个a）的值

#include <stdio.h>
#include <math.h>

long Y(int a, int n);

int main()
{
int a, n;
long sum = 0;
printf("Input a,n:\n");
scanf("%d,%d", &a, &n);
printf("sum=%ld\n", Y(a,n));
return 0;
}
long Y(int a, int n)
{
int count, sign, x, m=0;
long sum=0;
for(count=1,sign=0;count<=n;count++,sign++)
{
x = a * pow(10,sign);
m = m + x;
sum = sum + m;
}
return sum;
}


##### 6.8搬砖问题

#include <stdio.h>
int main()
{
int n, men, women, children, y, z;
printf("Input n(27<n<=77):\n");
scanf("%d", &n);
for (men=0;men*4<=n;men++)
{
for(women=0;women*3<=n;women++)
{
children = 36 - women - men;
if (men+women+children==36 && 8*men+6*women+children== 2*n)
{
printf("men=%d,women=%d,children=%d\n", men, women, children);
}
else ;
}
}
return 0;
}


##### 6.9编程输出某年某月有多少天（考虑到闰年）

Input year,month:
2015,3↙
31 days

Input year,month:
2015,4↙
30 days

Input year,month:
2016,2↙
29 days

Input year,month:
2014,2↙
28 days

Input year,month:
2015,13↙
Input error!

      #include <stdio.h>
int R(int m);
int main()
{
int year, month, ret;
printf("Input year,month:\n");
scanf("%d,%d", &year, &month);
ret = R(year);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("31 days\n");
break;
case 4:
case 6:
case 9:
case 11:printf("30 days\n");
break;
case 2:
if (ret == 1)
{
printf("29 days\n");
}
else
{
printf("28 days\n");
}
break;
default:printf("Input error!\n");
}
return 0;
}
int R(int m)
{
int r;
if (m % 4 == 0 && m % 100 != 0 || m % 400 == 0)
{
r = 1;
}
else
{
r = 0;
}
return (r);
}


##### 6.10计算阶乘的和v2.0

#include <stdio.h>
long Fact(int n);
int main()
{
int m, n, a, b, c;
for (a=0;a<10;a++)
{
for (b=1;b<10;b++)
{
for (c=1;c<10;c++)
{
n = Fact(a) + Fact(b) + Fact(c);
m = a * 100 + b * 10 + c;
if (m == n)
{
printf("%d\n", m);
}
}
}
}
return 0;
}

long Fact(int n)
{
int  i;
long result = 1;
for (i=1;i<=n;i++)
{
result *= i;
}
return (result);
}


##### 6.11计算最大的三位约数

#include <stdio.h>
int Func(int n);
int main()
{
int n, m;
printf("Input n:");
scanf("%d", &n);
if (n<1000 || n>1000000)
{
printf("Input error!\n");
}
else
{
m = Func(n);
printf("%d\n", m);
}
return 0;
}
int Func(int n)
{
int i, m;
for (i=1;i<=n;i++)
{
if (n % i == 0)
{
m = n / i;
if (m > 100 && m < 1000)
break;
else
;
}
}
return m;
}


##### 6.12孔融分梨

#include <stdio.h>
int Gcd(int a, int b);
int main()
{
int m, n, i;
printf("Input m,n:");
scanf("%d,%d", &m, &n);
i = Gcd(m, n);
if (i != -1)
{
m = m / i;
n = n / i;
printf("%d/%d\n", m, n);
}
else
{
printf( "Input error!\n");
}
return 0;
}
int Gcd(int a, int b)
{
int r, i;
if (a < 1 || b > 10000 || b == 0)
{
return -1;
}
else
{
for(r=1;r<=a;r++)
{
if(a % r == 0 && b % r == 0)
{
i = r;
}
}
}
return i;
}


##### 6.13素数求和

#include <stdio.h>
#include <math.h>
int IsPrime(int x);
int main()
{
int n, sum = 0, r,q;
printf("Input n:");
scanf("%d", &n);
for(r=1;r<=n;r++)
{
q = IsPrime(r);
if (q == 1)
{
sum = sum + r;
}
else ;
}
printf("sum=%d\n",sum);
return 0;
}
int IsPrime(int x)
{
int i, p = 1;
if (x<=1)
{
return 0;
}
else
{
for(i=2;i<=sqrt(x);i++)
{
if (x % i == 0)
{
p = 0;
}
}
}
return p;
}


03-01
11-24 1万+

01-11
01-31 1万+
12-05 2904
03-04 2349
02-10 8654
01-12 1万+
01-21 9161
12-05 2066
04-16 1486

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。