设为首页 加入收藏

TOP

C语言程序设计经典50例,会用到的(八)
2014-11-23 23:33:41 来源: 作者: 【 】 浏览:11
Tags:语言 程序设计 经典 用到

1.程序分析:利用辗除法。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int a,b,num1,num2,temp;
printf("please input two numbers:\n");
scanf("%d,%d",&num1,&num2);
if(num1 {
temp=num1;
num1=num2;
num2=temp;
}
a=num1;b=num2;
while(b!=0)/*利用辗除法,直到b为0为止*/
{
temp=a%b;
a=b;
b=temp;
}
printf("gongyueshu:%d\n",a);
printf("gongbeishu:%d\n",num1*num2/a);
getch();
}
==============================================================
【程序17】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
      
2.程序源代码:
#include "stdio.h"
#include "conio.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);
getch();
}
==============================================================
【程序18】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
   共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int a,n,count=1;
long int sn=0,tn=0;
printf("please input a and n\n");
scanf("%d,%d",&a,&n);
printf("a=%d,n=%d\n",a,n);
while(count<=n)
{
tn=tn+a;
sn=sn+tn;
a=a*10;
++count;
}
printf("a+aa+...=%ld\n",sn);
getch();
}
==============================================================
【程序19】
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
   找出1000以内的所有完数。
1. 程序分析:请参照程序<--上页程序14.
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
static int k[10];
int i,j,n,s;
for(j=2;j<1000;j++)
{
n=-1;
s=j;
for(i=1;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 printf("%d,",k);
printf("%d\n",k[n]);
}
}
getch();
}
==============================================================
【程序20】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
   第10次落地时,共经过多少米?第10次反弹多高?
1.程序分析:见下面注释
2.程序源代码:
#include "stdio.h"
#include "stdio.h"
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);
getch();
}


  .:.:经典c程序100例==21--30:.:.

    经典c程序100例==21--30
【程序21】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析:采取逆向思维的方法,从后往前推断。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
x2=x1;
day--;
}
printf("the total is %d\n",x1);
getch();
}
==============================================================
【程序22】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
   三队赛手的名单。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
      则表明此数不是素数,反之是素数。       
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/
for(i='x';i<='z';i++)
for(j='x';j<='z';j++)
{
if(i!=j)
for(k='x';k<='z';k++)
{
if(i!=
首页 上一页 5 6 7 8 9 10 下一页 尾页 8/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇c语言编程出错汇总 下一篇比较字符串的大小

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: