zoukankan      html  css  js  c++  java
  • 打印从1到最大的n位数

    本题目考察大数。

    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    
    bool Increment(char * number)
    {
        bool isOverflow = false;
        int nTakeOver = 0;
        int nLength = strlen(number);
    
        for (int i = nLength - 1; i >= 0; i--)  //模拟整数加法
        {
            int nSum = number[i] - '0' + nTakeOver;
            if (i == nLength - 1)
                nSum++;
            if (nSum >= 10)
            {
                if (i == 0)
                {
                    isOverflow = true;
                }
                else
                {
                    nSum -= 10;
                    nTakeOver = 1;
                    number[i] = char('0' + nSum);
                }
            }
            else
            {
                number[i] = char('0' + nSum);
                break;
            }
        }
        return isOverflow;
    }
    
    void PrintNumber(char * number)
    {
        bool isBeginning0 = true;
    
        int nLength = strlen(number);
        for (int i = 0; i < nLength; ++i)
        {
            if (isBeginning0 && number[i] != '0')
                isBeginning0 = false;
            if (!isBeginning0)
            {
                printf("%c", number[i]);
            }
        }
        printf("	");
    }
    
    
    void Print1ToMaxOfNDigits(int n)
    {
        if (n <= 0)
            return;
        char *number = new char[n + 1];
        memset(number, '0', n);
        number[n] = '';
    
        while (!Increment(number))
        {
            PrintNumber(number);
        }
        delete[]number;
    }
    
    
    
    int main()
    {
        Print1ToMaxOfNDigits(2);
    
        cout << "hello..." << endl;
        system("pause");
        return 0;
    }
  • 相关阅读:
    Project Euler 389 Platonic Dice (概率)
    单纯形(相关题目)
    关于C++中的内存泄露
    莫比乌斯反演与积性函数求和筛法中的一些细节
    清华集训2015-Day 2
    bzoj3456-城市规划
    多项式运算的一些技术
    bzoj2302-Problem c
    bzoj4300-绝世好题
    bzoj4726-Sabota?
  • 原文地址:https://www.cnblogs.com/zmm1996/p/11874161.html
Copyright © 2011-2022 走看看