第5章 循環(huán)結構程序設計
1、void?main()
???{
int?n,value;
int?i,count=0;
float?average?=?0;
long?int?sum?=?0;
scanf("%d",&n);
for(i?=?0;?i?<?n;?i++)
{
scanf("%d",&value);
if(value%2?==?0)
{
sum+=value;
count++;
}
}
average?=?sum?/?(float)count;
printf("the?average?is?%f\n",average);
???}
?
2、
???#include?"stdio.h"
???void?main()
???{
char?ch;
int?zm?=?0,?sz?=?0;
ch?=?getchar();
while(ch?!=?'*')
{
if((ch?>=?'A'?&&?ch?<=?'Z')?||?(ch?>=?'a'?&&?ch?<?'z')?)
zm++;
if(ch?>=?'0'?&&?ch?<=?'9')
sz++;
ch?=?getchar();
}
printf("zm?=?%d???;???sz?=?%d\n",zm,sz);
???}
?
3、
?????void?main()
?????{
long?i_value;
int?sum?=?0;
int?temp?=?0;
scanf("%ld",&i_value);
if(i_value?<?0)?{printf("you?input?the?int?is?error!\n");return;}
for(;;)
{
temp?=?i_value%10;
i_value?=?i_value/10;
sum+=temp;
printf("%d+",temp);
if(i_value?==?0)break;
}
printf("\b=%d\n",sum);
?
?????}
4、
?????#include?"stdio.h"
?????void?main()
?????{
char?ch;
ch?=?getchar();
while(ch?!=?'.')
{
if((ch?>=?'A'?&&?ch?<=?'Z'))
{
ch?=?ch?+?32;
putchar(ch);
}
else?if((ch?>=?'a'?&&?ch?<?'z')?)
{
ch?=?ch?-?32;
putchar(ch);
}
else
{
putchar(ch);
}
ch?=?getchar();
}
printf("\n");
?????}
5、
?????void?main()
?????{
float?sum?=?200;
int?count?=?0;
for(;;)
{
sum?=?sum+?sum*0.045;
count++;
if(sum?>?500)break;
}
printf("the?%d?years?later?complete!\n",count);
?????}
6、
????void?main()
????{
int?i,temp=0,bit=0;
for(i?=?1?;?i?<=?999;?i++)
{
if(i%3?==?0)
{
temp?=?i;
for(;temp?!=?0;)
{
bit?=?temp%10;
if(bit?==?5){printf("%d,",i);break;}
temp?=?temp/10;
}
}
}
printf("\b?\n");
????}
?
7、
#include?<stdio.h>
?
int?main(void)
{
????int?steps?=?29,?i?=?1;
????
????while?(?steps?%?7?)
????{
????????steps?=?(30?*?++i)?-?1;
????}
????
????printf("Total?steps:?%d\n",?steps);
????
????return?0;
}
8、
main()
{
?int?i,j,k,n;
?printf("the?narcissus?number?is:\n");
?for(n=100;n<1000;n++)
??{i=n/100;
???j=n/10-i*10;
???k=n%10;
???if(i*i*i+j*j*j+k*k*k==n)?printf("%d\n",n);
??}
}
9、
main()
{?float?i,j,temp,n,sum;
??int?t;
??i=2;j=1;sum=0;
??scanf("%d",&t);
??for(n=1;n<=t;n++)
????{sum=sum+i/j;
?????temp=i;i=i+j;j=temp;
????}
??printf("2/1+3/2+5/3+8/5+13/8+...=%f\n",sum);
}
10、
void?main()
{
???int?nWidth,a,b;
???scanf("%d",&nWidth);
???for(a=(nWidth%2);a<=nWidth;a+=2)
???{
?????for(b=0;b<nWidth;b++)
?????{
???????char?chOut?='?';
???????int?nleft?=(nWidth?-a)/2;
???????int?nright?=?(nWidth+a)/2;
???????if(b>=nleft&&b<nright)
???????{
?????????chOut?='*';
???????}
???????printf("%c",chOut);
?????}
?????printf("\n");
???}
}
11、
void?main()
{
??int?i=1,j=1;
??for(i=1;i<=9;i++)
??{
?????for(j=1;j<=i;j++)
???????printf("%d*%d=%d",i,j,i*j);
?????printf("\n")
??}
}
12、
#include?"stdio.h"
void?main()
{
??char?c;
??//count1是正數(shù)的個數(shù)
??//count2是負數(shù)的個數(shù)
??int?count1=0,count2=0;
??//sum1是正數(shù)之和
??//sum2是負數(shù)之和
??int?sum1=0,sum2=0;
??int?flage?=?0;
??c=getchar();
??while(c?!=?'*')
??{
????if(c?==?'-')flage?=?1;
????if(flage?==?0)
???????{sum1+=(c-48);count1++;}
????else
???????{if(c?!=?'-'){sum2-=(c-48);flage=0;count2++;}}
????c?=?getchar();
??}
??printf("%d,%d\n",count1,count2);
??printf("%f,%f\n",sum1/count1,sum2/count2);
}
13、
void?main()
{
????int?i,j;
????int?s?=?0;
????for(i?=?100?;?i?<=?999;?i++)
????{
s?=?0;
for(j=1;?j?<?i-1?;?j++)
{
if(?i%j?==?0?)?s?=?s?+?j;
}
if(s?==?i)printf("%5d",i);
}
}
14、
#include?<stdio.h>
#include?<math.h>
void?main()
{
int?n;
long?k?=?1;
float?e?=?1;
n?=?1;
clrscr();
while?(?fabs(1.0/k)?>=?0.000001)
{
n++;
e?=?e+?1.0/k;
k?=?k?*?n;
}
printf("%f\n",e);
}
15、
#include?"math.h"
main()
{float?x0,x1,x2,f0,f1,f2;
?x1=-10;
?f1=2*x1*x1*x1-4*x1*x1+3*x1;
?x2=10;
?f2=2*x2*x2*x2-4*x2*x2+3*x2;
?do
??{?x0=(x1+x2)/2;
????f0=2*x0*x0*x0-4*x0*x0+3*x0;
????if((f0*f1)<0)
????{x2=x0;?f2=f0;}
??????else
????{x1=x0;?f1=f0;}
??}
?while(fabs(f0)>1e-6);
?printf("______2*x*x*x-4*x*x+3*x=0______\n");
?printf("the?root?is?%f\n",x0);
}
第6章 函數(shù)與編譯預處理
1、
#include<stdio.h>
int?divisor(int?a,int?b)
{?int?r;
while((r=a%b)!=0)
{?a=b;
b=r;
}
return?b;
}
int?multiple(int?a,int?b)
{?int?d;
d=divisor(a,b);
return?a*b/d;
}
void?main()
{?int?a,b,c,d;
printf("intput?(a,b):???");
scanf("%d,%d",&a,&b);
c=divisor(a,b);
d=multiple(a,b);
printf("\ndivisor=%d\t\tmultiple=%d",c,d);
}
2、
#include<stdio.h>
void?tongji(char?a[])
{?int?b[3]={0,0,0},i=0;
while(a[i]!='\0')
{?if((a[i]<=90&&a[i]>=65)||(a[i]<=122&&a[i]>=97))
b[0]++;
else?if(a[i]<=57&&a[i]>=48)??
b[1]++;
else
b[2]++;
i++;
}
printf("zimu?have:?%d;?shuzi?have:?%d;?qita?have:?%d",b[0],b[1],b[2]);
getch();
}
void?main()
{?char?a[100];
printf("Please?input?a?string:???");
gets(a);
tongji(a);
}
3、
#include<stdio.h>
int?flower(int?n)
{?int?x=0,i,j,k;
i=(n%10);?
j=?(n/10%10);
k=(n/100);
x=i*i*i+j*j*j+k*k*k;
if(x==n)??return?1;
else??????return?0;
}
void?main()
{?int?i,n;
printf("Please?intput?n:??");
scanf("%d",&n);
if(n>999||n<100)printf("Input?error!!!");
else
{?for(i=100;i<n;i++)
if(flower(i))printf("%d???",i);
}getch();
}
4、
#include<stdio.h>
#define?SWAP(a,b)?t=b;b=a;a=t;
main()
{?float?x,y,t;
printf("Enter?two?number?(x,y):???");
scanf("%f,%f",&x,&y);
SWAP(x,y);
printf("\n\nExchanged:x=%f,y=%f",x,y);
getch();
}
5、
#include<stdio.h>
int?fib(int?n)
{?int?p;
if(n==0)??p=0;
else?if(n==1)?p=1;
else?p=fib(n-1)+fib(n-2);
return?p;
}
void?main()
{?int?n;
printf("Please?input?fib:??");
scanf("%d",&n);
printf("\n\nfibonacci=%d",fib(n));
}
6、
#include<stdio.h>
long?fac(int?n)
{?long?f;
if(n==0)?f=1;
else??f=n*fac(n-1);
return?f;
}
void?main()
{?int?m,n;
long?f;
printf("Please?input?(m,n):??");
scanf("%ld,%ld",&m,&n);
f=fac(n+m)+fac(n);
printf("\n\nfibonacci=%ld",f);
}
7、
#include<stdio.h>
void?list()
{?int?i,j;
for(i=1;i<=9;i++)
{??for(j=1;j<=i;j++)
printf("%d*%d=%d?",i,j,i*j);
???printf("\n");
}
}
void?main()
{?list();
}
8、
#include<stdio.h>
int?he(int?n)
{?int?i,s=0;
for(i=1;i<n;i++)
???if(n%i==0)s+=i;
return?s;
}
void?main()
{?int?i,j;
for(i=1;i<30000;i++)
{for(j=1;j<30000;j++)
if((he(i)==j)&&(he(j)==i)&&(i!=j))
?printf("%d<--->%d???",i,j);
}
}
9、
#include<stdio.h>
#define?max?100
struct?work
{?long?sid;
char?name[15];
}worker[max];
int?size=0;
struct?work?*set_list()
{?do
{?printf("\n\nPlease?input?(sid,name):???");
scanf("%ld,%s",&worker[size].sid,worker[size].name);
size++;
}while(worker[size-1].sid!=0);
return?worker;
}
void?paixu(struct?work?a[])
{?int?i,j,k;long?t;char?v[15];
for(i=0;i<size-1-2;i++)
{?k=i;
for(j=i;j<size-2;j++)
if(a[i].sid>a[j].sid)k=j;
if(k!=i)
{?t=a[i].sid;??a[i].sid=a[j].sid;??a[j].sid=t;
strcpy(v,a[i].name);strcpy(a[i].name,a[j].name);strcpy(a[j].name,v);
}
}
for(i=0;i<=size-2;i++)
printf("sid:?%ld\tname:%s\n\n",a[i].sid,a[i].name);
getch();
}
void?select(struct?work?a[])
{?int?i=0,found=1;long?num;
printf("Input?select?sid:??");
scanf("%ld",&num);
for(i=0;i<=size-2;i++)
{?if(a[i].sid==num)
{???????found=0;
printf("%ld:??%s",num,a[i].name);
break;
}
}
if(found)printf("\n\nNO?this?sid!!!");
getch();
}
void?main()
{?struct?work?*p;
p=set_list();
paixu(p);
select(p);
}
10、
#include<stdio.h>
float?jiecheng(int?n)
{?long?s=1;int?i;
for(i=1;i<=n;i++)
s=s*i;
return?s;
}
float?chengfang(float?x,int?n)
{?float?s=1;int?i;
for(i=1;i<=n;i++)
s=s*x;
return?s;
}
float?f(float?x,int?n)
{?float?s=1,t=0;int?i=1,j=-1;
for(i=1;i<=n;i++)
{?t=j*chengfang(x,2*i)/jiecheng(2*i);
s+=t;
j=j*j;
}
return?s;
}
main()
{?float?s1=0,s2=0,s3=0,s4=0,x=5.6;int?n=7;
s1=f(x,n);
s2=f(x+2.3,n);
s3=f(x-3.2,n+3);
s4=s1/(s2+s3);
printf("%f",s4);
getch();
}