数据结构练习(28)输出1到最大的N位数

http://zhedahht.blog.163.com/blog/static/2541117420094279426862/

思路:

简单的题目就要尽量要用简洁的代码来搞定,此时要求体现的是程序的逻辑性,切记。

#include <cstdio>

#include <cstring>

#include <cstdlib>



void PrintNumber(char* s, int len)

{

    bool flag = true;



    for (int i = 0; i < len; ++i)

    {

        if (flag && s[i] != '0')

            flag = false;



        if (!flag)

        {

            printf("%s\n", &s[i]); break;

        }

    }

}



void PrintNumberRecursively(char* s, int len, int index)

{

    if (index == len - 1)

        PrintNumber(s, len);

    else

    {

        for (int i = 0; i <= 9; ++i)

        {

            s[index+1] = i + '0';

            PrintNumberRecursively(s, len, index + 1);

        }

    }

}



void solve(int n)

{

    if (n <= 0)

        return;



    char b[100];

    memset(b, '\0', sizeof(b));



    for (int i = 0; i <= 9; ++i)

    {

        b[0] = i + '0';

        PrintNumberRecursively(b, n, 0);

    }

}



int main()

{   

    solve(2);

    return 0;

}

 

你可能感兴趣的:(数据结构)