- 题目描写叙述:
-
给定一个数字N。打印从1到最大的N位数。
- 输入:
-
每一个输入文件仅包括一组測试例子。
对于每一个測试案例,输入一个数字N(1<=N<=5)。
- 输出:
-
相应每一个測试案例,依次打印从1到最大的N位数。
- 例子输入:
-
1
- 例子输出:
-
1 2 3 4 5 6 7 8 9
方案1:
void func(int n)
{
if(n<=0)
{
return;
}
int i = 0,max = 1;
while(i++ < n)
{
max *= 10;
}
for(i = 1; i < max; i++)
{
cout<<i<<endl;
}
}
非常遗憾,这个方法不行,假设n太大的话。会溢出。
(对于此题,尽管不溢出,但会超时)
方案2:--->通过
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
bool Increment(char *number)
{
bool isOverFlow = false;//溢出标志
int takeOver = 0;//进位
int len = strlen(number);
for(int i = len-1; i >= 0; i--)
{
int nsum = number[i] - '0' + takeOver;
if(i == len-1)//假设是个位
{
nsum++;
}
if(nsum >= 10)//产生进位
{
if(i == 0)//进位的是最高位
{
isOverFlow = true;
}else
{
nsum -= 10;
takeOver = 1;
number[i] = nsum+'0';
}
}else//没有进位
{
number[i] = nsum + '0';
break;
}
}
return isOverFlow;
}
void PrintNumber(char *number)
{
bool flag = true;
int i = 0;
while(number[i] != '