输入n,打印1到n之间的每个数,考虑整数溢出
代码 :c c++
bool Increment(char *data)
{
int temp = 0;
int len = strlen(data);
int i = len - 1;
int f = 0;
bool b = false;
while(i >= 0)
{
temp = data[i] - '0' + f;
if (i == len - 1)
{
temp++;
}
if (temp >= 10)
{
if (0 == i)
{
b = true;
}
else
{
data[i] = temp - 10 + '0';
f = 1;
}
}
else
{
data[i] = temp + '0';
break;
}
i--;
}
return b;
}
void PrintData(char *data)
{
int f = 0;
int i = 0;
int len = strlen(data);
for (i = 0; i < len; i++)
{
if (data[i] != '0' && !f)
{
f = 1;
}
if (f)
{
cout << data[i];
}
}
cout << " ";
}
void dataincrement(int n)
{
char *data = new char[n+1];
memset(data, '0', n);
data[n] = '\0';
while(!Increment(data))
{
PrintData(data);
}
cout << endl;
}
void main()
{
dataincrement(3);
}