算法入门(1)

来源:互联网 发布:剑网三李白脸型数据 编辑:程序博客网 时间:2024/06/05 09:56

最近在学习算法,所以想把所写的程序记录下来,程序比较简单,但是我相信始终会有帮助的

输入一个三位数,分离出它的百位,十位和个位,反转后输出

#include <stdio.h>#include <iostream>using namespace std;int main(){      int n;      scanf("%d", &n);      if (n%10==0)      {            printf("%d%d\n", n / 10 % 10, n /100);      }      else      {            printf("%d%d%d\n", n % 10, n / 10 % 10, n /100);      }      system("pause");      return 0;}

输入三个整数,从小到大输出

#include <stdio.h>#include <iostream>using namespace std;void comparethree(int &a, int &b, int &c);int main(){      int a,b,c,t;      scanf("%d%d%d", &a,&b,&c);      comparethree(a, b, c);      printf("%d\t%d\t%d\n", a, b, c);      system("pause");      return 0;}void comparethree(int &a, int &b, int &c){      int t;      if (a>b)      {            t = a;            a = b;            b = t;      }      if (a>c)      {            t = a;            a = c;            c = t;      }      if (b > c){            t = b;            b = c;            c = t;      }}

水仙花数
输出100-999中所有的水仙花数。若3位数ABC 满足ABC = A3+B3+C3,则称其为水仙花数。

#include <stdio.h>#include <iostream>using namespace std;int main(){      for (int i = 1; i <= 9; i++)      {            for (int j = 0; j <= 9; j++){                  for (int z = 0; z <=9; z++)                  {                        int x = i*i*i;                        int y = j*j*j;                        int m = z*z*z;                        int s = i * 100 + j * 10 + z;                        if (s==x+y+m)                        {                              printf("%d\n", s);                        }                  }            }      }      system("pause");      return 0;}

倒三角形
输入正整数,输出一个n层的倒三角形,例如n=5时输出如下:

######### #######  #####   ###    ##include <stdio.h>#include <iostream>using namespace std;int main(){      int n;      int count = 0;      scanf("%d", &n);  //输入层数      for (int i = n; i >=1; i--)  从顶层开始      {            int s = 1 + 2 * (i - 1); //等差数列求得#数量            count++;            for (int j = 0; j < s; j++)            {                  printf("#");            }            printf("\n");            for (int x = 0; x < count; x++)            {                  printf(" ");  //用于打印空格            }      }      system("pause");      return 0;}
0 0