C经典

来源:互联网 发布:珠海 知乎 编辑:程序博客网 时间:2024/05/04 20:13

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.
程序分析:利用while语句,条件为输入的字符不为'/n'.
      

2.
程序源代码:
#include "stdio.h"
main()
{char c;
 int letters=0,space=0,digit=0,others=0;
 
printf("please input some characters/n");
 
while((c=getchar())!='/n')
 
{
 
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
  
letters++;
 
else if(c==' ')
  
space++;
   
else if(c>='0'&&c<='9')
       
digit++;
     
else
       
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d/n",letters,
space,digit,others);
}
==============================================================
【程序19

题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=123.编程
   找出1000以内的所有完数。
1.
程序分析:请参照程序<--上页程序14.
2.
程序源代码:

main()
{
static int k[10];
int i,j,n,s;
for(j=2;j<1000;j++)
 {
 
n=-1;
 
s=j;
  
for(i=1;i<J;I++)
  
{
   
if((j%i)==0)
   { 
n++;
    
s=s-i;
    
k[n]=i;
   
}
  
}
 
if(s==0)
 
{
 
printf("%d is a wanshu",j);
 
for(i=0;i<N;I++)
 
printf("%d,",k[i]);
 
printf("%d/n",k[n]);
 
}
}
}
==============================================================

【程序20

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
   第10次落地时,共经过多少米?第10次反弹多高?
1.
程序分析:见下面注释
2.
程序源代码:
main()
{
float sn=100.0,hn=sn/2;
int n;
for(n=2;n<=10;n++)
 {
  sn=sn+2*hn;/*n次落地时共经过的米数
*/
  hn=hn/2; /*n次反跳高度
*/
 
}
printf("the total of road is %f/n",sn);
printf("the tenth is %f meter/n",hn);
}


原创粉丝点击