给定一个整数N ,任务是找到一个整数K ,使得通过反复去除K的最后一位形成的数字总和等于N。
例子:
Input: N = 136
Output: 123
Explanation:
The numbers formed by repeatedly removing the last digit of 123 are {123, 12, 1}.
Therefore, the sum of these numbers = 123 + 12 + 1 = 136( = N).
Input: N = 107
Output: 98
Explanation:
The numbers formed by repeatedly removing the last digit of 98 are {98, 9}.
Therefore, the sum of these numbers = 98 + 7 = 107( = N).
方法:该方法基于以下观察结果:
- 考虑K = 123 。
- 由123组成的可能数字是1、12和123。
- 现在,可以将123表示为100 + 20 +3。如果所有其他数字都类似地表示,则其思想是知道所有数字组合中每个数字的位置和频率,以将总和作为N。
Digit | Frequency of each digit | Sum | ||
units | tens | hundreds | ||
1 | 1 | 1 | 1 | 1*1 + 1*10 + 1*100 = 111 |
2 | 1 | 1 | 2*1 + 2*10 = 22 | |
3 | 1 | 3*1 = 3 |
- 现在,对于长度L的给定数N。将数字除以L数1 s可得到最高位数。
- 计算余数,这将是我们新形成的N。
- 又分为新形成的N-与(L – 1)第1号数量得到第二高的地方位,并继续,直到L变0。
请按照以下步骤解决问题:
- 令L为给定数字N中的位数。
- 将字符串str初始化为L个1 s。
- 将变量ans初始化为零,该变量将存储结果数K。
- 迭代直到字符串str不为空,然后执行以下步骤:
- 使用函数stoi()将字符串str转换为数字,并将其存储在M中。
- 用N除以M并将an更新为:
ans = ans*10 + (N/M)
- 更新N到N%M。
- 从字符串str中删除最后一个字符。
- 完成上述步骤后,打印存储在ans中的值,该值是K的必需值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the value of K
int findK(int N, int l)
{
// Stores l number of 1s
string ones = "";
while (l--) {
// Storing 1's
ones = ones + '1';
}
// Stores the value of K
int ans = 0;
// Iterate until ones is empty
while (ones != "") {
// Convert ones to number
int m = stoi(ones);
// Update the ans
ans = (ans * 10) + (N / m);
// Update N to N%m
N = N % m;
// Removing last digit from ones
ones.pop_back();
}
// Return the value of K
return ans;
}
// Driver Code
int main()
{
// Given number N
int N = 136;
// Number of digits in N
int L = to_string(N).length();
// Funtion Call
cout << findK(N, L);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to find the
// value of K
static int findK(int N,
int l)
{
// Stores l number of 1s
String ones = "";
while (l-- > 0)
{
// Storing 1's
ones += '1';
}
// Stores the value of K
int ans = 0;
// Iterate until ones is empty
while (!ones.equals(""))
{
// Convert ones to number
int m = Integer.valueOf(ones);
// Update the ans
ans = (ans * 10) + (N / m);
// Update N to N%m
N = N % m;
// Removing last digit from ones
ones = ones.substring(0,
ones.length() - 1);
}
// Return the value of K
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given number N
int N = 136;
// Number of digits in N
int L = String.valueOf(N).length();
// Funtion Call
System.out.print(findK(N, L));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for
# the above approach
# Function to find
# the value of K
def findK(N, l):
# Stores l number of 1s
ones = ""
while (l):
# Storing 1's
ones = ones + '1'
l -= 1
# Stores the value of K
ans = 0
# Iterate until ones
# is empty
while (ones != ""):
# Convert ones to number
m = int(ones)
# Update the ans
ans = (ans * 10) + (N // m)
# Update N to N%m
N = N % m
# Removing last digit from ones
ones = ones.replace(ones[-1], "", 1)
# Return the value of K
return ans
# Driver Code
if __name__ == "__main__":
# Given number N
N = 136
# Number of digits in N
L = len(str(N))
# Funtion Call
print (findK(N, L))
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find the
// value of K
static int findK(int N,
int l)
{
// Stores l number of 1s
String ones = "";
while (l-- > 0)
{
// Storing 1's
ones += '1';
}
// Stores the value of K
int ans = 0;
// Iterate until ones is empty
while (!ones.Equals(""))
{
// Convert ones to number
int m = Int32.Parse(ones);
// Update the ans
ans = (ans * 10) + (N / m);
// Update N to N%m
N = N % m;
// Removing last digit from ones
ones = ones.Substring(0,
ones.Length - 1);
}
// Return the value of K
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given number N
int N = 136;
// Number of digits in N
int L = String.Join("", N).Length;
// Funtion Call
Console.Write(findK(N, L));
}
}
// This code is contributed by Princi Singh
输出:
123
时间复杂度: O(log 10 N)
辅助空间: O(1)