📌  相关文章
📜  通过从最后一个交替索引上按顺序添加自然数来查找下一个数字

📅  最后修改于: 2022-05-13 01:56:06.194000             🧑  作者: Mango

通过从最后一个交替索引上按顺序添加自然数来查找下一个数字

给定一个大小为N的数字字符串S ,任务是找到通过将数字123 …… 加到给定数字字符串S的每个替代数字(从最后一个位置开始)直到无穷大而形成的数字。在任何时候,如果加法结果不是个位数,则重复进行数字相加,直到结果为个位数。

例子:

方法:解决这个问题的思路在于重复加法部分。实际上不需要重复执行加法直到有一位数。相反,执行number % 9 。如果数字 % 9等于9 ,则数字之和将等于 9 ,否则数字之和将等于数字 % 9 。请按照以下步骤解决问题:

  • 将变量tempadded_number初始化为0以存储要更改的位置和要添加的数字。
  • 将字符串变量result初始化为空字符串以存储结果。
  • 迭代范围[len-1, 0] ,其中len是字符串的长度,使用变量i并执行以下步骤:
    • 将变量digit初始化为字符串中当前位置的数字。
    • 如果temp%2等于0则将 added_number的值增加1并将其添加到变量digit。
    • 如果digit大于等于10 ,则将 digit 的值设置为digit %9 ,如果仍然, digit等于0 ,则将其值设置为 9。
    • 将变量digit添加到变量结果的开头。
  • 执行上述步骤后,打印结果的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to generate the resultant
// number using the given criteria
string generateNumber(string number)
{
    int temp = 0, adding_number = 0;
 
    // Storing end result
    string result = "";
 
    // Find the length of numeric string
    int len = number.size();
 
    // Traverse the string
    for (int i = len - 1; i >= 0; i--) {
 
        // Storing digit at ith position
        int digit = number[i] - '0';
 
        // Checking that the number would
        // be added or not
        if (temp % 2 == 0) {
 
            adding_number += 1;
            digit += adding_number;
 
            // Logic for summing the digits
            // if the digit is greater than 10
            if (digit >= 10) {
                digit %= 9;
                if (digit == 0)
                    digit = 9;
            }
        }
 
        // Storing the result
        result = to_string(digit) + result;
        temp += 1;
    }
 
    // Returning the result
    return result;
}
 
// Driver Code
int main()
{
    string S = "1345";
    cout << generateNumber(S);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
    // Function to generate the resultant
    // number using the given criteria
    public static String generateNumber(String number) {
        int temp = 0, adding_number = 0;
 
        // Storing end result
        String result = "";
 
        // Find the length of numeric string
        int len = number.length();
 
        // Traverse the string
        for (int i = len - 1; i >= 0; i--) {
 
            // Storing digit at ith position
            int digit = (int) number.charAt(i) - (int) '0';
 
            // Checking that the number would
            // be added or not
            if (temp % 2 == 0) {
 
                adding_number += 1;
                digit += adding_number;
 
                // Logic for summing the digits
                // if the digit is greater than 10
                if (digit >= 10) {
                    digit %= 9;
                    if (digit == 0)
                        digit = 9;
                }
            }
 
            // Storing the result
            result = digit + result;
            temp += 1;
        }
 
        // Returning the result
        return result;
    }
 
    // Driver Code
    public static void main(String args[]) {
        String S = "1345";
        System.out.println(generateNumber(S));
    }
 
}
 
// This code is contributed by gfgking.


Python3
# Python3 program for the above approach
 
# Function to generate the resultant
# number using the given criteria
def generateNumber(number) :
     
    temp = 0; adding_number = 0;
 
    # Storing end result
    result = "";
 
    # Find the length of numeric string
    l = len(number);
 
    # Traverse the string
    for i in range(l - 1, -1, -1) :
 
        # Storing digit at ith position
        digit = ord(number[i]) - ord('0');
 
        # Checking that the number would
        # be added or not
        if (temp % 2 == 0) :
 
            adding_number += 1;
            digit += adding_number;
 
            # Logic for summing the digits
            # if the digit is greater than 10
            if (digit >= 10) :
                digit %= 9;
                if (digit == 0) :
                    digit = 9;
         
        # Storing the result
        result = str(digit) + result;
        temp += 1;
 
    # Returning the result
    return result;
 
# Driver Code
if __name__ ==  "__main__" :
 
    S = "1345";
    print(generateNumber(S));
 
    # This code is contributed by AnkThon


Javascript


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to generate the resultant
    // number using the given criteria
    public static String generateNumber(string number) {
        int temp = 0, adding_number = 0;
 
        // Storing end result
        string result = "";
 
        // Find the length of numeric string
        int len = number.Length;
 
        // Traverse the string
        for (int i = len - 1; i >= 0; i--) {
 
            // Storing digit at ith position
            int digit = (int)number[i] - (int)'0';
 
            // Checking that the number would
            // be added or not
            if (temp % 2 == 0) {
 
                adding_number += 1;
                digit += adding_number;
 
                // Logic for summing the digits
                // if the digit is greater than 10
                if (digit >= 10) {
                    digit %= 9;
                    if (digit == 0)
                        digit = 9;
                }
            }
 
            // Storing the result
            result = digit + result;
            temp += 1;
        }
 
        // Returning the result
        return result;
    }
 
    // Driver Code
    public static void Main(string []args) {
        string S = "1345";
        Console.WriteLine(generateNumber(S));
    }
 
}
 
// This code is contributed by AnkThon


输出:
1546

时间复杂度: O(N)
辅助空间: O(1)