从后往前,隔位相加自然数,求下一个数字
如果要存储一个很大的整数,可以使用数字字符串来存储数值。众所周知,计算机使用int数据类型无法存储大于32位的数字。因此,为了避免溢出,在这个问题中,我们将采用数字字符串作为输入,而不是int变量,这样我们就可以在大规模数字上处理这个问题。
问题陈述
在这个问题中,我们需要通过从后往前,隔位相加自然数来找到下一个数字。我们将得到一个数字字符串,并必须将最终输出作为字符串本身返回。让我们看看如何解决这个问题。
让我们通过一些例子来理解这个问题。
输入
s = “12345678”
输出
16375879
解释
首先,我们将取最后一位数字“8”,并加上第一个自然数“1”。我们将得到 (8 + 1) = 9 作为新的数字字符串的最后一位数字。
然后我们将保留“7”,因为我们必须交替执行操作。
接下来,我们将取“6”,并加上第二个自然数“2”。我们将得到 (6 + 2) = 8 作为新的数字字符串的第三位数字。
然后我们将保留“5”,因为我们必须交替执行操作。
接下来,我们将取“4”,并加上第三个自然数“3”。我们将得到 (4 + 3) = 7 作为新的数字字符串的第五位数字。
然后我们将保留“3”,因为我们必须交替执行操作。
接下来,我们将取“2”,并加上第四个自然数“4”。我们将得到 (2 + 4) = 6 作为新的数字字符串的第七位数字。
然后我们将保留“1”,因为我们必须交替执行操作。
因此,我们将得到最终的数字字符串“16375879”。
输入
s = “78930”
输出
18231
解释
首先,我们将取最后一位数字“0”,并加上第一个自然数“1”。我们将得到 (0 + 1) = 1 作为新的数字字符串的最后一位数字。
然后我们将保留“3”,因为我们必须交替执行操作。
接下来,我们将取“9”,并加上第二个自然数“2”。我们将得到 (9 + 2) = 11
现在,我们需要将这个两位数转换为一位数,我们可以通过对其取模9来轻松做到这一点,这将给我们“2”作为新的数字字符串的第三位数字。
然后我们将保留“8”,因为我们必须交替执行操作。
接下来,我们将取“7”,并加上第三个自然数“3”。我们将得到 (7 + 3) = 10
现在,我们需要将这个两位数转换为一位数,我们可以通过对其取模9来轻松做到这一点,这将给我们“1”作为新的数字字符串的第五位数字。
因此,我们将得到最终的数字字符串“18231”。
问题说明
让我们尝试理解这个问题并找到它的解决方案。在这个问题中,我们得到一个数字字符串,我们必须记住以下条件来修改我们的字符串:
从1、2、3等等一直加到无穷大的自然数,从最后一位开始隔位相加。
这意味着如果我们将最后一位数字的索引从1开始算作奇数位,我们将修改位于奇数位置的数字,并保持其余数字不变。
如果加法后没有得到一位数,我们应该通过连续加数字直到它变成一位数来将其变成一位数,或者我们可以使用另一种逻辑,对修改后的数字取模9,这将给我们相同的值,并且在更短的时间内也能给我们一位数。
在下面的文章中,我们将借助于简单代码中的注释来理解这种简单的方法:
算法
定义一个临时数字,并将addNum初始化为“0”。
定义一个空字符串,它将包含最终输出。
从字符串的末尾开始循环,并将字符转换为整数。
如果数字位于偶数位置,则从1开始一直加到无穷大的自然数。
将加法后得到的数字转换为一位数。
将数字转换为字符。
将字符附加到最终输出字符串中。
示例
以下是各种编程语言中上述方法的实现:
#include <bits/stdc++.h>
using namespace std;
// Function to find the next number by adding natural numbers in order on alternating indices from last
string Helper(string s){
// Define a temporary integer
// Initialize the number we will add in the numeric string integers by 0
int temp = 0, addNum = 0;
// Define the empty string ans
string ans = "";
// Store the length of the numerical string
int n = s.size();
// Start the loop to get the new string
for (int i = n - 1; i >= 0; i--) {
// Store the digit at ith position in the integer form rather character form
int num = s[i] - '0';
// Check if the position is even or not, if even alter the digit
if (temp % 2 == 0) {
addNum += 1;
num += addNum;
// Check if the digit becomes greater than or equal to 10
if (num >= 10) {
// If yes, we need to take a modulus of 9 to make it single digit
num %= 9;
// Check if the single digit is 0, and change the digit back to 9
if (num == 0)
num = 9;
}
}
// Store the result
ans = to_string(num) + ans;
temp += 1;
}
// Return the result
return ans;
}
int main(){
// Give the input string of numerical
string s = "12345678";
// Call the Helper function
cout << "The following number by adding natural numbers in order on alternating indices on the string " << s << " from the last is: "<< Helper(s);
return 0;
}
输出
The following number by adding natural numbers in order on alternating indices on the string 12345678 from the last is: 16375879
public class Main {
public static String Helper(String s) {
// Define a temporary integer
// Initialize the number we will add in the numeric string integers by 0
int temp = 0, addNum = 0;
// Define the empty string ans
StringBuilder ans = new StringBuilder();
int n = s.length();
for (int i = n - 1; i >= 0; i--) {
int num = Character.getNumericValue(s.charAt(i));
// Check if the position is even or not, if even alter the digit
if (temp % 2 == 0) {
addNum += 1;
num += addNum;
// Check if the digit becomes greater than or equal to 10
if (num >= 10) {
// If yes, we need to take a modulus of 9 to make it single digit
num %= 9;
// Check if the single digit is 0, and change the digit back to 9
if (num == 0) {
num = 9;
}
}
}
ans.insert(0, num);
temp += 1;
}
// Return the result
return ans.toString();
}
public static void main(String[] args) {
// Give the input string of numerical
String s = "12345678";
// Call the Helper function
String result = Helper(s);
System.out.println("The following number by adding natural numbers in order on alternating indices on the string " + s + " from the last is: " + result);
}
}
输出
The following number by adding natural numbers in order on alternating indices on the string 12345678 from the last is: 16375879
def Helper(s):
# Define a temporary varaible
# Initialize the number we will add in the numeric by 0
temp = 0
addNum = 0
# Define the empty ans
ans = ""
n = len(s)
# Start the loop to get the new string
for i in range(n - 1, -1, -1):
num = int(s[i])
# Check if the position is even or not, if even alter the digit
if temp % 2 == 0:
addNum += 1
num += addNum
# Check if the digit becomes greater than or equal to 10
if num >= 10:
# If yes, we need to take a modulus of 9 to make it single digit
num %= 9
# Check if the single digit is 0, and change the digit back to 9
if num == 0:
num = 9
ans = str(num) + ans
temp += 1
# Return the result
return ans
# Give the input string of numerical
s = "12345678"
# Call the Helper function
result = Helper(s)
print("The following number by adding natural numbers in order on alternating indices on the string", s, "from the last is:", result)
输出
The following number by adding natural numbers in order on alternating indices on the string 12345678 from the last is: 16375879
上述代码的复杂度
时间复杂度 - O(n); 其中n是字符串的长度
空间复杂度 - O(1); 我们在上述代码中没有将任何变量存储在任何数据结构中。
结论
在这篇文章中,我们通过从后往前,隔位相加自然数来找到下一个数字。我们通过对从最后一位开始的隔位数字进行操作并将其余数字保持不变来得到解决方案。我们将使用数字字符串而不是实际数字,这样我们就可以处理具有更多数字的大数值。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP