1.整数反转输出

1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n, _n, newname = 0;  // int取值范围:0 到 4294967295(232 - 1)
 6     cout << "请输入一个小于9位的数,我帮你输出倒数!" << endl;
 7     cin >>n;
 8     cout << n << "的倒数是:";
 9     do
10     {
11         _n = n % 10;
12         n /= 10;
13         cout << _n;
14     } while (n!=0);
15     cout << endl;
16     return 0;
17 }

扩展:1000位以内的正整数的加法运算

声明两个能容纳1000位十进制数的char型数组存储输入数字字符串,以长的做被加数和结果,短的长度控制加法循环次数。在加法过程中判断和处理进位。举例代码如下:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include "stdio.h"

#include "string.h"

int main(void){

    char a[1002]={'0'},b[1002]={'0'},*pl=a,*ps=b;

    int i,j,la,lb;

    while(1){//保证输入是正确的        

        printf("Input a & b(length<=1000)...\n");

        scanf("%[1234567890] %[1234567890]",a+1,b+1);//最前面留1位做进位

        if((la=strlen(a))<1002 && (lb=strlen(b))<1002)

            break;

        printf("Error, redo: ");

    }

    if(la<lb){//找出长的做被加数和结果

        ps=a,pl=b;

        j=la,la=lb,lb=j;

    }

    for(i=lb-1,j=la-1;i>0;i--,j--)//从末位向前对应加

        if((pl[j]+=ps[i]-'0')>'9')//某位>'9'则处理进位

            pl[j]-=10,pl[j-1]++;

    for(;j>0;j--)//若被加数有进位则继续做完

        if(pl[j]>'9')

            pl[j]-=10,pl[j-1]++;

    printf("The result is %s\n",pl[0]=='1' ? pl : pl+1);//有进位则第0位输出

    return 0;

}



2.星期

1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int day;
 6     cout << "请在1~7之间输入一个代表星期的数:(1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;7:星期天)";
 7     cin >> day;
 8     switch (day)
 9     {
10     case 1:
11         cout << "星期一"<<endl;
12     case 2:
13         cout << "星期二"<<endl;
14     case 3:
15         cout << "星期三"<<endl;
16     case 4:
17         cout << "星期四"<<endl;
18     case 5:
19         cout << "星期五"<<endl;
20     case 6:
21         cout << "星期六"<<endl;
22     case 7:
23         cout << "星期天"<<endl;
24     default:
25         break;
26     }
27 }

3.在楼梯上打印笑脸

1 #include <stdio.h>
 2 int main()
 3 {
 4     int i, j;
 5     printf_s("\1\1\n");
 6     for ( i = 1; i < 11; i++)
 7     {
 8         for (j = 1; j <= i;j++) {
 9             printf_s("%c%c", 219, 219);  // ASCII码表里219代表“空格”
10         }
11     printf_s("\n");
12     }
13 }

4.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

分析:规律为:11,2,3,5,8,13,21 ...

1 #include <stdio.h>
 2 int main()
 3 {
 4  long f1, f2;
 5  int i;
 6  f1 = f2 = 1;
 7  for (i = 1; i <= 20; i++)
 8  {
 9   printf("%12ld %12ld", f1, f2);
10   if (i % 2 == 0)  
11    printf("\n");/*控制输出,每行四个*/
12   f1 = f1 + f2;  /*前两个月加起来赋值给第三个月*/
13   f2 = f1 + f2;  /*前两个月加起来赋值给第三个月*/
14  }
15  return 0;
16 }

5.打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

  程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。求其各位数字立方和

1 #include <stdio.h>
 2 int main()
 3 {
 4     int j, d, k,num;
 5     printf_s("所有的水仙数是:\n");//
 6     for (num = 100; num < 1000; num++) {
 7         j = num / 100;  /*分解出百位*/
 8         d = num / 10 % 10;  /*分解出十位*/
 9         k = num % 10;  /*分解出个位*/
10         if (num == j * j * j + d * d * d + k * k * k)
11         {
12             printf_s("%d ",num);
13         }
14     }
15     printf_s("\n");
16     return 0;
17 }

6.题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

  (1)用辗转相除法求最大公因数,

    辗转相除法:

      假设有两整数m和n:

      ①m%n得余数r

      ② 若r=0,则n即为两数的最大公约数

      ③ 若r≠0,则m=n,n=r,再回去执行①

      流程图为:

      

C++例题_#include

  (2)再用两数相乘再除以最大公因数来求最小公倍数。

1 #include <stdio.h>
 2 int main()
 3 {
 4  int m, n, r, a, b;
 5  int temp;
 6  printf_s("请输入两个数,我帮你输出最大公因数和最小公倍数:\n");
 7  scanf_s("%d %d",&m,&n);
 8  if (m<n)
 9  {
10   temp = m;
11   m = n;
12   n = temp;
13  }
14  a = m; b = n;
15  while (a % b != 0)
16  {
17   r = a % b;
18   a = b;
19   b = r;
20  }
21  printf_s("最大公因数%d,\n 最小公倍数%d \n", b,m*n/ b);
22  return 0;
23 }


作者:꧁执笔小白꧂