【程序11】
題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數(shù)為多少?
1.程序分析: 兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....
2.程序源代碼:
#include "stdio.h"
#include "conio.h"
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
{
printf("%12ld %12ld",f1,f2);
if(i%2==0) printf("\n"); /*控制輸出,每行四個*/
f1=f1+f2; /*前兩個月加起來賦值給第三個月*/
f2=f1+f2; /*前兩個月加起來賦值給第三個月*/
}
getch();
}
==============================================================
【程序12】
題目:判斷101-200之間有多少個素數(shù),并輸出所有素數(shù)。
1.程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除,則表明此數(shù)不是素數(shù),反之是素數(shù)。
2.程序源代碼:
#include "stdio.h"
#include "conio.h"
#include "math.h"
main()
{
int m,i,k,h=0,leap=1;
printf("\n");
for(m=101;m<=200;m++)
{
k=sqrt(m+1);
for(i=2;i<=k;i++)
if(m%i==0)
{
leap=0;
break;
}
if(leap)
{
printf("%-4d",m);
h++;
if(h%10==0)
printf("\n");
}
leap=1;
}
printf("\nThe total is %d",h);
getch();
}
==============================================================
【程序13】
題目:打印出所有的“水仙花數(shù)”,所謂“水仙花數(shù)”是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個“水仙花數(shù)”,因?yàn)?53=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循環(huán)控制100-999個數(shù),每個數(shù)分解出個位,十位,百位。
2.程序源代碼:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,k,n;
printf("'water flower'number is:");
for(n=100;n<1000;n++)
{
i=n/100;/*分解出百位*/
j=n/10%10;/*分解出十位*/
k=n%10;/*分解出個位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("%-5d",n);
}
getch();
}
==============================================================
【程序14】
題目:將一個正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5。
程序分析:對n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個最小的質(zhì)數(shù)k,然后按下述步驟完成:
(1)如果這個質(zhì)數(shù)恰等于n,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束,打印出即可。
(2)如果n<>k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)你n,重復(fù)執(zhí)行第一步。
(3)如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。
2.程序源代碼:
/* zheng int is divided yinshu*/
#include "stdio.h"
#include "conio.h"
main()
{
int n,i;
printf("\nplease input a number:\n");
scanf("%d",&n);
printf("%d=",n);
for(i=2;i<=n;i++)
while(n!=i)
{
if(n%i==0)
{
printf("%d*",i);
n=n/i;
}
else
break;
}
printf("%d",n);
getch();
}
==============================================================
【程序15】
題目:利用條件運(yùn)算符的嵌套來完成此題:學(xué)習(xí)成績>=90分的同學(xué)用A表示,60-89分之間的用B表示,60分以下的用C表示。
1.程序分析:(a>b)?a:b這是條件運(yùn)算符的基本例子。
2.程序源代碼:
#include "stdio.h"
#include "conio.h"
main()
{
int score;
char grade;
printf("please input a score\n");
scanf("%d",&score);
grade=score>=90?'A':(score>=60?'B':'C');
printf("%d belongs to %c",score,grade);
getch();
}
==============================================================
【程序16】
題目:輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。
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<num2)/*交換兩個數(shù),使大數(shù)放在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】
題目:輸入一行字符,分別統(tǒng)計出其中英文字母、空格、數(shù)字和其它字符的個數(shù)。
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是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制。
1.程序分析:關(guān)鍵是計算出每一項(xiàng)的值。
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】
題目:一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為“完數(shù)”。例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)。
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<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);
printf("%d\n",k[n]);
}
}
getch();
}
==============================================================
【程序20】
題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時,共經(jīng)過多少米?第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次落地時共經(jīng)過的米數(shù)*/
hn=hn/2; /*第n次反跳高度*/
}
printf("the total of road is %f\n",sn);
printf("the tenth is %f meter\n",hn);
getch();
}
.:.:經(jīng)典c程序100例==21--30:.:.
經(jīng)典c程序100例==21--3
本文導(dǎo)航
- 第1頁: 首頁
- 第2頁: 【程序11】-【程序20】
- 第3頁: 【程序21】-【程序30】
- 第4頁: 【程序31】-【程序40】
- 第5頁: 【程序41】-【程序49】