经典算法(1~30)

可读性:1、2

算法:3、4、5、11、13、14

 

【程序1】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
掉不满足条件的排列。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, j, k, count=0;
 6     for(i=1; i<=4; i++)
 7     {
 8         for(j=1; j<=4; j++)
 9         {
10             for(k=1; k<=4; k++)
11             {
12                 if(i==j || i==k || j==k)
13                     continue;
14                 else
15                 {
16                     cout<<i*100+j*10+k<<"    ";
17                     count++;
18                     if(count%10==0)
19                         cout<<endl;
20                 }
21             }
22         }
23     }
24     cout<<"sum="<<count<<endl;
25     system("pause");
26     return 1;
27 }

进一步考虑输出格式,输出的三位数不一定非得乘上各自基数相加,依次输出即可

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, j, k, count=0;
 6     for(i=1; i<=4; i++)
 7     {
 8         for(j=1; j<=4; j++)
 9         {
10             for(k=1; k<=4; k++)
11             {
12                 if(i==j || i==k || j==k)
13                     continue;
14                 else
15                 {
16                     //cout<<i*100+j*10+k<<"    ";
17                     cout<<i<<j<<k<<"    ";
18                     count++;
19                     if(count%10==0)
20                         cout<<endl;
21                 }
22             }
23         }
24     }
25     cout<<"sum="<<count<<endl;
26     system("pause");
27     return 1;
28 }

进一步,直接找到符合条件的输出,不符合条件的舍弃,即采用 if 来精简 if…else结构,并对换行符的判断进行简化

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, j, k, count=0;
 6     for(i=1; i<=4; i++)
 7     {
 8         for(j=1; j<=4; j++)
 9         {
10             for(k=1; k<=4; k++)
11             {
12                 //if(i==j || i==k || j==k)
13                 //    continue;
14                 //else
15                 //{
16                 //    //cout<<i*100+j*10+k<<"    ";
17                 //    cout<<i<<j<<k<<"    ";
18                 //    count++;
19                 //    if(count%10==0)
20                 //        cout<<endl;
21                 //}
22                 if(i!=j && i!=k && j!=k)
23                 {
24                     cout<<i<<j<<k<<"    ";
25                     if(++count%10==0)
26                         cout<<endl;
27                 }
28             }
29         }
30     }
31     cout<<"sum="<<count<<endl;
32     system("pause");
33     return 1;
34 }

============================================================== 

【程序2】
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高
于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提
成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于
40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于
100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i;
 6     cin>>i;
 7     if(i<10 && i>=0)    cout<<i*1000<<endl;
 8     else if(i<20 && i>=10) cout<<10000+(i-10)*750<<endl;
 9     else if(i<40 && i>=20) cout<<10000+7500+(i-20)*500<<endl;
10     else if(i<60 && i>=40) cout<<10000+7500+10000+(i-40)*300<<endl;
11     else if(i<100 && i>=60) cout<<10000+7500+10000+6000+(i-60)*150<<endl;
12     else if(i>=100) cout<<10000+7500+10000+6000+6000+(i-100)*100<<endl;
13     else cout<<"error input"<<endl;
14     system("pause");
15     return 1;
16 }

参考代码如下,可读性更高,仅考虑正确输入情况

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, bonus1, bonus2, bonus4, bonus6, bonus10, bonus;
 6     cin>>i;
 7     bonus1=100000*0.1; bonus2=bonus1+100000*0.075;
 8     bonus4=bonus2+200000*0.05; bonus6=bonus4+200000*0.03;
 9     bonus10=bonus6+400000*0.015;
10     if(i<=100000)    bonus=i*0.1;
11     else if(i<=200000) bonus=bonus1+(i-100000)*0.075;
12     else if(i<=400000) bonus=bonus2+(i-200000)*0.05;
13     else if(i<=600000) bonus=bonus4+(i-400000)*0.03;
14     else if(i<=1000000) bonus=bonus6+(i-600000)*0.015;
15     else bonus=bonus10+(i-1000000)*0.01;
16     cout<<bonus<<endl;
17     system("pause");
18     return 1;
19 }

 ==============================================================

【程序3】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后
的结果满足如下条件,即是结果。请看具体分析:

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int panduan(int i);
 5 int main()
 6 {
 7     int i;
 8     for(i=0; i<=100000;i++)
 9         if(panduan(i))
10             cout<<i<<endl;
11     system("pause");
12     return 1;
13 }
14 int panduan(int i)
15 {
16     int flag1=0,flag2=0;
17     float r1=i+100;
18     float r2=i+268;
19     int max=(int)sqrt(r2)+1;
20     for(int j=1; j<max; j++)
21     {
22         if( fabs((float)(j*j)-r1)<0.0001)
23             flag1=1;
24         if( fabs((float)(j*j)-r2)<0.0001)
25             flag2=1;
26     }
27     //cout<<flag1<<"    "<<flag2<<endl;
28     if(flag1==1 && flag2==1)
29         return 1;
30     else
31         return 0;
32 }

参考代码的代码量少且思路独特(sqrt()赋值给int会强制转换为int)

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int main()
 5 {
 6     long int i,x,y,z;
 7     for(i=1; i<100000; i++)
 8     {
 9         x=sqrt(i+100);
10         y=sqrt(i+296);
11         //cout<<"x="<<x<<"    y="<<y<<endl;
12         if(x*x==i+100 && y*y==i+268)
13             cout<<i<<endl;
14     }
15     system("pause");
16     return 1;
17 }

==============================================================
【程序4】
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
情况,闰年且输入月份大于3时需考虑多加一天。

闰年的判断需思考进一步简化

 1 #include<iostream>
 2 using namespace std;
 3 int isLeapYear(int year);
 4 int main()
 5 {
 6     int year, month, day, flag, total(0);
 7     cin>>year>>month>>day;
 8     if(isLeapYear(year))    flag=1;
 9     else    flag=0;
10     switch(month)
11     {
12     case 1: total=0; break;
13     case 2: total=31; break;
14     case 3: total=31+28; break;
15     case 4: total=31+28+31; break;
16     case 5: total=31+28+31+30; break;
17     case 6: total=31+28+31+30+31; break;
18     case 7: total=31+28+31+30+31+30; break;
19     case 8: total=31+28+31+30+31+30+31; break;
20     case 9: total=31+28+31+30+31+30+31+31; break;
21     case 10: total=31+28+31+30+31+30+31+31+30; break;
22     case 11: total=31+28+31+30+31+30+31+31+30+31; break;
23     case 12: total=31+28+31+30+31+30+31+31+30+31+30; break;
24     default: cout<<"wrong input"<<endl; break;
25     }
26     if(month>2)                    //闰年且输入月份大于2时需多加一天
27         total+=day+flag;
28     else 
29         total+=day;
30     cout<<"total="<<total<<endl;
31     system("pause");
32     return 1;
33 }
34 int isLeapYear(int year)
35 {
36     if(year%4==0)
37     {
38         if(year%100==0 && year%400!=0)
39             return 0;
40         else return 1;
41     }
42     else return 0;
43 }

==============================================================
【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

 体会用异或交换两数数值

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int x, y, z;
 6     cin>>x>>y>>z;
 7     if(x>y)
 8     {
 9         x=x^y;
10         y=x^y;
11         x=x^y;
12     }
13     if(x>z)
14     {
15         x=x^z;
16         z=x^z;
17         x=x^z;
18     }
19     if(y>z)
20     {
21         y=y^z;
22         z=y^z;
23         y=y^z;
24     }
25     cout<<x<<y<<z<<endl;
26     system("pause");
27     return 1;
28 }

==============================================================
【程序6】
题目:用*号输出字母C的图案。
程序分析:可先用'*'号在纸上写出字母C,再分行输出。

1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5     cout<<"****"<<endl<<"*"<<endl<<"*"<<endl<<"****"<<endl;
6     system("pause");
7     return 1;
8 }

==============================================================
【程序8】
题目:输出9*9口诀。
程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, j;
 6     for(i=1; i<=9; i++)
 7     {
 8         for(j=1; j<=i; j++)
 9         {
10             cout<<i<<"*"<<j<<"="<<i*j<<"    ";
11         }
12         cout<<endl;
13     }
14     system("pause");
15     return 1;
16 }

==============================================================
【程序9】
题目:要求输出国际象棋棋盘。
程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int i, j;
 6     for(i=0; i<8; i++)
 7     {
 8         for( j=0; j<8; j++)
 9         {
10             if((i+j)%2)
11                 cout<<"* ";    //黑色正方形不会输出,用*代替
12             else
13                 cout<<"  ";
14         }
15         cout<<endl;
16     }
17     system("pause");
18     return 1;
19 }

==============================================================
【程序10】
题目:打印楼梯,同时在楼梯上方打印两个笑脸。(略)
============================================================== 

【程序11】 (非递归实现)
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

 1 //递归
 2 #include<iostream>
 3 using namespace std;
 4 int fun(int month);
 5 int main()
 6 {
 7     for(int i=1; i<20; i++)
 8         cout<<""<<i<<"月:"<<fun(i)<<endl;
 9     system("pause");
10     return 1;
11 }
12 int fun(int month)
13 {
14     if(month==1 || month==2)
15         return 1;
16     return fun(month-1)+fun(month-2);
17 }
 1 //非递归
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6     int f1=1, f2=1;
 7     for(int i=1; i<10; i++)
 8     {
 9         cout<<""<<i<<"月:"<<f1<<endl;
10         cout<<""<<i+1<<"月:"<<f2<<endl;
11         f1=f1+f2;
12         f2=f1+f2;
13     }
14     system("pause");
15     return 1;
16 }

==============================================================
【程序12】
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int main()
 5 {
 6     int count=0; int result; int flag;
 7     for( int i=101; i<=200; i++)
 8     {
 9         result=(int)sqrt(i);
10         flag=1;
11         for( int j=2; j<=result; j++)
12         {
13             if(i%j==0)
14             {
15                 flag=0;
16                 break;
17             }
18         }
19         if(flag)    
20         {
21             count++;
22             cout<<i<<endl;
23         }
24     }
25     cout<<"sum="<<count<<endl;
26     system("pause");
27     return 1;
28 }

==============================================================
【程序13】 (各个位数的判断)
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

 1 #include<iostream>
 2 using namespace std;
 3 int isDaffodil(int i);
 4 int main()
 5 {
 6     for(int i=100; i<1000; i++)
 7     {
 8         if(isDaffodil(i))
 9             cout<<i<<endl;
10     }
11     system("pause");
12     return 1;
13 }
14 int isDaffodil(int i)
15 {
16     int x, y, z;
17     z=i%10;
18     y=i/10%10;
19     x=i/100;
20     if(x*x*x+y*y*y+z*z*z==i)
21         return 1;
22     return 0;
23 }

==============================================================

【程序14】 (两种思路)
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int array_prime[50];
 6     int count=0;
 7     int input, key(2);
 8     cin>>input;
 9     cout<<input<<"=";
10     while(input!=1)
11     {
12         while(input%key==0)
13         {
14             input/=key;
15             array_prime[count++]=key;
16         }
17         key++;
18     }
19     for(int i=0; i<count-1; i++)
20         cout<<array_prime[i]<<"*";
21     cout<<array_prime[count-1];
22     system("pause");
23     return 1;
24 }
 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int input, key;
 6     cin>>input;
 7     for(key=2; key<=input; key++)
 8     {
 9         while(key!=input)
10         {
11             if(input%key==0)
12             {
13                 cout<<key<<"  ";
14                 input/=key;
15             }
16             else
17                 break;
18         }
19     }
20     cout<<input<<endl;
21     system("pause");
22     return 1;
23 }

==============================================================
【程序15】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,
60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int score;
 6     cin>>score;
 7     cout<<(char)((score>89)?'A':((score>59)?'B':'C'));
 8     system("pause");
 9     return 1;
10 }

==============================================================
【程序16】 (没想出好的方法)
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

程序分析:利用辗除法

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int num1, num2, temp;
 6     cin>>num1>>num2;
 7     int a=num1, b=num2;
 8     if(num1<num2)
 9     {
10         num1^=num2;
11         num2^=num1;
12         num1^=num2;
13     }
14     while(num2!=0)
15     {
16         temp=num1%num2;
17         num1=num2;
18         num2=temp;
19     }
20     cout<<"最大公约数: "<<num1<<endl;
21     cout<<"最小公倍数: "<<a*b/num1<<endl;
22     system("pause");
23     return 1;
24 }

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

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int count_letter(0), count_num(0), count_space(0), count_other(0);
 6     char ch;
 7     //char ch=getchar();
 8     //while((ch=getchar())!='\n')        //条件为输入的字符不为'\n'
 9     while((ch=getchar())!=EOF)          //'\n'被记入count_other
10     {
11         if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
12             count_letter++;
13         else if( ch>='0' && ch<='9')
14             count_num++;
15         else if( ch==' ')
16             count_space++;
17         else
18             count_other++;
19         //ch=getchar();
20     }
21     cout<<"count_letter= "<<count_letter<<endl;
22     cout<<"count_num= "<<count_num<<endl;
23     cout<<"count_space= "<<count_space<<endl;
24     cout<<"count_other= "<<count_other<<endl;
25     system("pause");
26     return 1;
27 }

==============================================================
【程序18】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
共有5个数相加),几个数相加有键盘控制。
程序分析:关键是计算出每一项的值。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a, count ,s(0);
 6     int result[100];
 7     cin>>a>>count;
 8     for( int i=1; i<=count; i++)
 9     {
10         result[i]=1;
11         for( int k=1; k<i; k++)
12             result[i]*=10;
13         result[i]*=a;
14         if(i !=1)    
15             result[i]+=result[i-1];
16         //cout<<result[i]<<endl;
17         s+=result[i];
18     }
19     cout<<s<<"=";
20     for( int i=1; i<count; i++)
21         cout<<result[i]<<"+";
22     cout<<result[count]<<endl;
23     system("pause");
24     return 1;
25 }

参考代码:

 1 main() 
 2 { 
 3 int a,n,count=1; 
 4 long int sn=0,tn=0; 
 5 printf("please input a and n\n"); 
 6 scanf("%d,%d",&a,&n); 
 7 printf("a=%d,n=%d\n",a,n); 
 8 while(count<=n) 
 9 { 
10 tn=tn+a; 
11 sn=sn+tn; 
12 a=a*10; 
13 ++count; 
14 } 
15 printf("a+aa+...=%ld\n",sn); 
16 } 
View Code

==============================================================
【程序19】
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
找出1000以内的所有完数。

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int main()
 5 {
 6     int temp, sum;
 7     for( int i=2; i<1000; i++)
 8     {
 9         temp=(int)sqrt(i+1);
10         sum=0;
11         for(int j=1; j<=temp; j++)
12         {
13             if(i%j==0)
14             {
15                 if(i!=i/j)
16                     sum=sum+j+i/j;
17                 else
18                     sum+=j;
19             }
20         }
21         if(sum==i)
22             cout<<i<<"    ";
23     }
24     system("pause");
25     return 1;
26 }

==============================================================
【程序20】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
第10次落地时,共经过多少米?第10次反弹多高?

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     double length=0, height=50;
 6     for( int i=1; i<10; i++)
 7     {
 8         length+=2*height;
 9         height/=2;
10     }
11     length+=100;
12     cout<<"共经过:"<<length<<""<<endl;
13     cout<<"第十次反弹高度:"<<height<<""<<endl;
14     system("pause");
15     return 1;
16 }

==============================================================
【程序21】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int sum=1;
 6     for(int i=1; i<10; i++)
 7         sum=(sum+1)*2;
 8     cout<<sum<<endl;
 9     system("pause");
10     return 1;
11 }

==============================================================
【程序22】(逻辑推理简单,用代码实现思路受阻)
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
   三队赛手的名单。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     char i, j, k;                        //分别表示a, b, c 的对手
 6     for( i='x'; i<='z'; i++)
 7     {
 8         for( j='x'; j<='z'; j++)
 9         {
10             if( i!=j)
11             {
12                 for( k='x'; k<='z'; k++)
13                 {
14                     if( i!=k && j!=k)
15                     {
16                         if( i!='x' && k!='x' && k!='z')
17                         {
18                             cout<<"a的对手是:"<<i<<endl;
19                             cout<<"b的对手是:"<<j<<endl;
20                             cout<<"c的对手是:"<<k<<endl;
21                         
22                         }
23                     }
24                 }
25             }
26         }
27     }
28     system("pause");
29     return 1;
30 }

==============================================================
【程序23】
题目:打印出如下图案(菱形)


      *
    ***
  ******
********
  ******
    ***
     *

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     for(int i=1; i<=4; i++)
 6     {
 7         for( int j=1; j<=7; j++)
 8         {
 9             if( j<4+1-i)    cout<<" ";
10             else if( j<=3+i)        cout<<"*";
11         }
12         cout<<endl;
13     }
14     for(int i=1; i<=3; i++)
15     {
16         for(int j=1; j<=7; j++)
17         {
18             if( j<=i)    cout<<" ";
19             else if( j<=7-i)    cout<<"*";
20         }
21         cout<<endl;
22     }
23     system("pause");
24     return 1;
25 }

==============================================================
【程序24】
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     double p1, p2, add1, add2, sum(0);
 6     p1=2; p2=1; add1=3; add2=2;
 7     sum+=p1/p2;
 8     for(int i=2; i<=20; i++)
 9     {
10         sum+=add1/add2;
11         add1+=p1;
12         add2+=p2;
13         p1=add1-p1;
14         p2=add2-p2;
15         //cout<<sum<<endl;
16     }
17     cout<<"sum="<<sum<<endl;
18     system("pause");
19     return 1;
20 }

==============================================================
【程序25】 (学会第二种方法)
题目:求1+2!+3!+...+20!的和

 1 #include<iostream>
 2 using namespace std;
 3 double factorial( double i);
 4 int main()
 5 {
 6     double sum=0;
 7     for( int i=1; i<=20; i++)
 8         sum+=(factorial((double)i));
 9     cout<<"sum="<<sum<<endl;
10     system("pause");
11     return 1;
12 }
13 double factorial( double i)
14 {
15     double result=1;
16     cout<<i<<":";
17     while( i>0)
18     {
19         result*=i--;
20         //cout<<result<<"*"<<endl;
21         //i--;
22     }
23     cout<<result<<endl;
24     return result;
25 }
 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     double sum(0), result(1);
 6     for( int i=1; i<=20; i++)
 7     {
 8         result*=i;
 9         //cout<<"*"<<i<<":"<<result<<endl;
10         sum+=result;
11     }
12     cout<<"sum="<<sum<<endl;
13     system("pause");
14     return 1;
15 }

==============================================================
【程序26】
题目:利用递归方法求5!。

 1 #include<iostream>
 2 using namespace std;
 3 int factorial(int i);
 4 int main()
 5 {
 6     cout<<"5!= "<<factorial(5)<<endl;
 7     system("pause");
 8     return 1;
 9 }
10 int factorial(int i)
11 {
12     if(i==1)
13         return 1;
14     else
15         return factorial(i-1)*i;
16 }

==============================================================
【程序27】
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

 1 #include<iostream>
 2 using namespace std;
 3 void input(int i);
 4 int main()
 5 {
 6     input(0);
 7     system("pause");
 8     return 1;
 9 }
10 void input(int i)
11 {
12     //cout<<"i="<<i<<endl;
13     char ch;
14     cin>>ch;
15     if(i<4)                                //4需要不断尝试得出
16         input(++i);
17     cout<<ch;
18 }

==============================================================
【程序28】
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第
   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后
   问第一个人,他说是10岁。请问第五个人多大?

递归:

 1 #include<iostream>
 2 using namespace std;
 3 int recursion(int i);
 4 int main()
 5 {
 6     cout<<recursion(5)<<endl;
 7     system("pause");
 8     return 1;
 9 }
10 int recursion(int i)
11 {
12     if(i==1)
13         return 10;
14     return 2+recursion(i-1);
15 }

非递归:

 1   #include<iostream>
 2   using namespace std;
 3   int recursion(int i);
 4   int main()
 5   {
 6       cout<<recursion(5)<<endl;
 7       system("pause");
 8       return 1;
 9   }
10  int recursion(int i)
11  {
12      if(i==1)
13          return 10;
14      return 2+recursion(i-1);
15  }

==============================================================
【程序29】 (掌握分解每一位数的方法)
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序分析:学会分解出每一位数。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int input, flag;
 6     int num_array[5];
 7     cin>>input;
 8     if( input<10)         flag=1;
 9     else if( input <100)        flag=2;
10     else if( input <1000)       flag=3;
11     else if( input <10000)      flag=4;
12     else if( input <100000)     flag=5;
13     cout<<flag<<"位数"<<endl;
14     num_array[0]=input%10;
15     for(int i=1; i<flag; i++)
16     {
17         int temp=input;
18         for( int j=1; j<=i; j++)
19             temp/=10;
20         num_array[i]=temp%10;
21     }
22     for(int i=0;i<=flag-1; i++)
23         cout<<num_array[i]<<" ";
24     system("pause");
25     return 1;
26 }

分解每个数参考代码

 1 main( )
 2 {
 3 long a,b,c,d,e,x;
 4 scanf("%ld",&x);
 5 a=x/10000;/*分解出万位*/
 6 b=x%10000/1000;/*分解出千位*/
 7 c=x%1000/100;/*分解出百位*/
 8 d=x%100/10;/*分解出十位*/
 9 e=x%10;/*分解出个位*/
10 if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
11 else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
12   else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
13     else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
14       else if (e!=0) printf(" there are 1,%ld\n",e);
15 }

==============================================================
【程序30】
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

 1 /*在29的基础上修改即可*/
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6     int input, flag=5;
 7     int num_array[5];
 8     cin>>input;
 9     //if( input<10)        flag=1;
10     //else if( input <100)      flag=2;
11     //else if( input <1000)        flag=3;
12     //else if( input <10000)     flag=4;
13     //else if( input <100000)     flag=5;
14     //cout<<flag<<"位数"<<endl;
15     num_array[0]=input%10;
16     for( int i=1; i<flag; i++)
17     {
18         int temp=input;
19         for( int j=1; j<=i; j++)
20             temp/=10;
21         num_array[i]=temp%10;
22     }
23     /*for( int i=0; i<=flag-1; i++)
24         cout<<num_array[i]<<" ";*/
25     int ok=1;
26     for( int i=0; i<=flag/2; i++)
27     {
28         if(num_array[i]!=num_array[flag-1-i])
29         {
30             ok=0;
31             break;
32         }
33     }
34     if(ok)        cout<<input<<"是回文数"<<endl;
35     else        cout<<input<<"不是回文数"<<endl;
36     system("pause");
37     return 1;
38 }

 

posted @ 2014-10-06 14:20  anthozoan77  阅读(329)  评论(0编辑  收藏  举报