//例5.4
#include<stdio.h>
#define MAXNUM 100000
int main()
{
float sum,amount,ave;
sum = 0;
int i;
for(i = 1;i<=1000;i++)
{
printf("请输入第%d位同学的捐款数额:",i);
scanf ("%f",&amount);
sum += amount;
if (sum>MAXNUM)
break;
}
ave = sum/i;
printf("捐款人数%d\n平均金额%f",i,ave);
return 0;
}
//例5.5输出100-200之间不能被3整除的数
#include<stdio.h>
int main()
{
int i;
for (i = 100;i<=200;i++)
//1
// if (i%3 != 0)
// printf("%d\n",i);
//2
{
if (i%3 == 0)
continue; //如果可以被3整除,在这里跳出循环 ,不执行下一步的打印操作
printf("%d\n",i);
}
return 0;
}
//例5.6输出4*5的矩阵
#include<stdio.h>
int main()
{
int i,j,k;
for(i = 1;i<=4;i++)
for(j = 1,k = 1;j<=5;j++,k++)
{
printf("%d\t",i*j);
if (k%5 == 0)
printf("\n");
}
return 0;
}
//break结束整个循环,continue结束当前执行的循环
//例5.7
#include<stdio.h>
#include<math.h>
int main()
{
int sign;
float i,sum,temp;
sum = 0,i = 1,temp = 1,sign = 1;
while (fabs(temp)>=1e-6)
{
sum += temp;
i += 2;
sign *= -1;
temp = sign/i;
}
printf("%f",sum*4.0);
return 0;
}
//例5.8 斐波那契数列
#include<stdio.h>
int main()
{
int a,b,t,i;
a = 1,b = 1;
printf("%d\n%d\n",a,b);
for (i = 2;i<=40;i++)
{
t = a + b;
printf("%d\n",t);
a = b;
b = t;
}
return 0;
}
//例5.8改进
#include<stdio.h>
int main()
{
int f1 = 1,f2 = 1;
int i;
for (i = 1;i<=20;i++)
{
printf("%12d%12d",f1,f2);
if(i%2 == 0)
printf ("\n");
f1 = f1 + f2;
f2 = f1 + f2;
}
return 0;
}
//例5.9输入一个大于3的整数n,判断它是否为素数
#include<stdio.h>
int main()
{
int i,num,FLAG;
FLAG=0;
printf("请输入一个大于3的整数:");
scanf("%d",&num);
for (i = 2;i<num;i++)
if (num % i == 0)
FLAG = 1;
if (FLAG == 1)
printf("%d不是素数",num);
else
printf("%d是素数",num);
return 0;
}
//不必判断num被2-(num-1)之间的数整除,只需判断(2-1/2*num或者2-sqrt(num)之间的数即可)
//例5.10 100-200之间的素数
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,FLAG;
FLAG = 0;
for (i = 101;i<200;i+=2)
{
FLAG = 0;
for (j = 2;j<=sqrt(i);j++)
if(i%j == 0)
FLAG = 1;
if (FLAG == 0)
printf("%d\n",i);
}
return 0;
}
//!只对奇数进行检查
//例5.11译密码,从键盘输入一行字符,要求输出其相应的密码
#include<stdio.h>
int main()
{
char s;
while ((s = getchar() ) != '\n')
{
if ((s>='a'&&s<='v')||(s>='A'&&s<='V'))
s += 4;
else if ((s>='w'&&s<='z')||(s>='W'&&s<='Z'))
s -= 22;
printf("%c",s);
}
return 0;
}
//5.3 输入两个正整数m和n,求其最大公约数和最小公倍数
#include<stdio.h>
int main()
{
int m,n,t;
printf("请输入两个正整数:");
scanf("%d %d",&m,&n);
if (m>n)
{
t = m;
m = n;
n = t;
}
//最大公约数
for(t = m;t>=0;t--)
{
if ((m%t==0)&&(n%t==0))
{
printf("最大公约数%d",t);
break;
}
}
//最小公倍数
for(t = n;t<=m*n;t++)
{
if ((t%m==0)&&(t%n==0))
{
printf("最小公倍数%d",t);
break;
}
}
return 0;
}
//5.4 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
#include<stdio.h>
int main()
{
char ch;
int num_char,num_tab,num_num,num_others;
num_char = 0,num_tab = 0,num_num = 0,num_others = 0;
while ((ch = getchar()) != '\n')
{
if (((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
num_char++;
else if (ch==' ')
num_tab++;
else if (ch>='0'&&ch<='9')
num_num++;
else num_others++;
}
printf("英文字母数量:%d\n空格数量:%d\n数字数量:%d\n其他字符数量:%d\n",num_char,num_tab,num_num,num_others);
return 0;
}
//5.5求n个a的和,a是一个数字,n表示a的位数
#include<stdio.h>
int main()
{
int i,a,n,sum,temp;
sum = 0,temp = 0;
printf("请输入数字、位数并以空格隔开\n");
scanf("%d %d",&a,&n);
for(i = 1;i <= n;i++)
{
temp = temp * 10 + a;
sum += temp;
}
printf("和为%d",sum);
return 0;
}
//5.6求阶乘之和
#include<stdio.h>
int main()
{
int i,sum,temp;
sum = 0,temp = 1;
for (i = 1;i<=20;i++)
{
temp = temp*i;
sum += temp;
}
printf("和为%d",sum);
return 0;
}
//5.7 ???
#include<stdio.h>
int main()
{
int i;
float sum;
sum = 0;
for(i=1;i<=100;i++)
{
sum += i;
while (i>50)
//continue;
break;
printf("%d\n",i);
sum += i*i;
while(i>10)
//continue;
break;
printf("hello1\n");
sum += 1.0/i;
}
printf("和为%f",sum);
return 0;
}
//5.8水仙花数
#include<stdio.h>
int main()
{
int i;
for (i = 100;i<=999;i++)
{
if (i == (i/100)*(i/100)*(i/100)+ (i/10%10)*(i/10%10)*(i/10%10) + (i%10)*(i%10)*(i%10) )
printf("%d\n",i);
}
return 0;
}
//5.9完数
#include<stdio.h>
int main()
{
int i,j,factor,sum;
for (i = 1;i <= 1000;i++)
{
sum = 0;
for(j = 1;j < i ;j++)
{
if (i % j == 0)
sum += j;
}
if (i == sum)
{
printf("%6dfactors are:",i);
for(j = 1;j < i ;j++)
{
if (i % j == 0)
printf("%6d",j);
}
printf("\n");
}
}
return 0;
}
//5.10 求分数数列的前20项之和
#include<stdio.h>
int main()
{
int i,t;
float a,b,sum;
sum = 0,a = 1, b = 2;;
for (i = 1;i<=20;i++)
{
sum += b/a; //分母为a,分子为b
t = b;
b = a + b;
a = t;
}
printf("%f",sum);
}
//5.11 一个球从100高度自由落下,每次落地后反跳为原高度的一半,再落下,
//再反弹,求它在第10次落地时,共经过多少米,第10次反弹多高
#include<stdio.h>
int main()
{
int i;
float sum,temp;
sum = 0,temp = 100;
for(i = 1;i<=10;i++)
{
sum += temp * 2;
temp /= 2.0;
}
printf("总高度%f\n第十次%f\n",sum,temp);
return 0;
}
//5.12猴子吃桃问题
#include<stdio.h>
int main()
{
int day,peach_num;
peach_num = 1;
for(day = 9;day>0;day--)
peach_num = (peach_num+1)*2;
printf("%d",peach_num);
}
//5.13 用迭代法求x=根号下a。
#include<stdio.h>
#include<math.h>
int main()
{
float x1,x2,a;
x2 = 1.0;
printf("请输入a的值:");
scanf("%f",&a);
do
{
x1 = x2;
x2 = (x1+a/x1)/2;
}while(fabs(x2-x1)>=0.000001) ;
printf("%f",x2);
}
//5.14牛顿迭代法求下面方程在1.5附近的根
#include<stdio.h>
int main()
{
float f,f1,x0,d;
x0 = 1.5;
do
{
f = 2*x0*x0*x0 - 4*x0*x0 + 3*x0 -6;
f1 = 6*x0*x0 -8*x0 + 3;
d = f/f1;
x0 = x0-d;
}while (d>0.00001);
printf("%f",x0);
return 0;
}
//5.15用二分法求下面方程在(-10,10)之间的根:
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,fa,fb,fc;
a = 10,b = -10;
do
{
c = (a + b)/2;
fc = 2*c*c*c -4*c*c +3*c -6 ;
fa = 2*a*a*a -4*a*a +3*a -6 ;
fb = 2*b*b*b -4*b*b +3*b -6 ;
//printf("%f",fc);
if (fc == 0)
{
//printf("根为%f\n",c);
break;
}
if (fa*fc<0)
b = c;
if (fb*fc<0)
a = c;
}while (fabs(fc)>1e-10);
printf("根为%f",c);
return 0;
}
Chapter5
最新推荐文章于 2024-05-14 09:39:54 发布