# 输出一个数组的末尾加上一个1

#include
#include
using namespace std;
class Solution {
public:
vectorplusOne(vectordigits)
{
int len = size(digits);
if (digits[len - 1] != 9)
{
digits[len - 1]++;
return digits;
}
reverse(digits.begin(), digits.end());
if (digits[len - 1] != 9)
{
int remainder = 0;
remainder = (digits[0] + 1) / 10;
digits[0] = (digits[0] + 1) % 10;
int tmp = 0;
for (int i = 1;i < len;i++)
{
tmp = digits[i];
digits[i] = (digits[i] + remainder) % 10;
remainder = (tmp + remainder) / 10;
}
reverse(digits.begin(), digits.end());
return digits;
}
else {
vectordigitsplus(len + 1);
for (int i = 0;i < len;i++)
{
digitsplus[i] = digits[i];
}
int tmp = 0;
int remainder = 0;
remainder = (digitsplus[0] + 1) / 10;
digitsplus[0] = (digitsplus[0] + 1) % 10;
for (int i = 1;i < len + 1;i++)
{
tmp = digitsplus[i];
digitsplus[i] = (digitsplus[i] + remainder) % 10;
remainder = (tmp + remainder) / 10;
}
reverse(digitsplus.begin(), digitsplus.end());
return digitsplus;
}
}
};//方案的唯一缺点是较为吃性能,内存占用量较大
int main()
{
int n = 0;
cout << "please input the lenth of array: ";
cin >> n;
cout << "please input the element of the array: ";
vectorarr(n);
for (int& a : arr)
cin >> a;
Solution s;
for (int a : s.plusOne(arr))
cout << a;
return 0;
}

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客