zoukankan      html  css  js  c++  java
  • 789相邻不等数(分治递归)

          题目:

          789相邻不等数就是一个数,每位的数只能是7、8或9,并且相邻不能相等。因为要求相邻不等,故这样的数至少是个两位数。输入n(1< n < 100),输出长度为n的所有789相邻不等数。

          分析: 

          确定第一个数,第二数不和第一个相等,递归即可。

    代码:

     1 #include<stdio.h>
    2 #include<stdlib.h>
    3
    4 int a[100];
    5 void play789(int m,int n);
    6
    7 int main()
    8 {
    9 int i,n;
    10
    11 while(scanf("%d",&n) != EOF)
    12 {
    13 a[0] = n;
    14 for(i = 7 ; i < 10 ; ++i )
    15 {
    16 a[n] = i;
    17 play789(i,n-1);
    18 }
    19 }
    20 system("pause");
    21 return 0;
    22 }
    23
    24 void play789(int m,int n)
    25 {
    26 int i,j,k;
    27 k = n;
    28 for(i = 7 ; i < 10 ; ++i)
    29 {
    30 if(i != m) a[k] = i;
    31 else continue;
    32
    33 if(k == 1)
    34 {
    35 for(j = a[0] ; j > 0 ; --j)
    36 printf("%d",a[j]);
    37 printf("\n");
    38 }
    39 else
    40 play789(a[k],k-1);
    41
    42 }//for()
    43 }



     

  • 相关阅读:
    [Postman]历史(8)
    [Postman]响应(7)
    [Postman]请求(6)
    [Postman]查找替换(5)
    ORA-02050故障诊断一例
    转 js实践篇:例外处理Try{}catch(e){}
    转 PHP
    HTML DOM getElementById() 方法
    地点选择
    9i 和 11 g 区别
  • 原文地址:https://www.cnblogs.com/HpuAcmer/p/2299975.html
Copyright © 2011-2022 走看看