给定整数N ,任务是通过执行以下操作减少数量并形成键:
- 提取数字的最高有效位:
- 如果数字是偶数:将连续的数字相加,直到数字总和为奇数。
- 如果数字是奇数:将连续的数字相加,直到数字的总和为偶数。
- 对所有剩余的数字重复该过程。
- 最后,将计算得出的总和串联起来以得到密钥。
例子:
Input: N = 1667848270
Output: 20290
Explanation:
Step 1: First Digit(= 1) is odd. So, add up the next digits until the sum is even.
Therefore, digits 1, 6, 6, and 7 are added up to form 20.
Step 2: Next digit(= 8) is even. So, add up the next digits until the sum is odd.
Therefore, digits 8, 4, 8, 2, and 7 are added up to form 29.
Step 3: Last digit(= 0) is even.
Therefore, the final answer after concatenating the results will be: 20290
Input: N = 7246262412
Output: 342
Explanation:
Step 1: First Digit(= 7) is odd. So, add up the next digits until the sum is even.
Therefore, digits 7, 2, 4, 6, 2, 6, 2, 4, and 1 are added up to form 34.
Step 2: Last digit(= 2) is even.
Therefore, the final answer after concatenating the results will be: 342.
方法:想法是迭代数字的数字并检查数字的奇偶性。如果是偶数,则继续进行下一个数字,直到遇到奇数。对于奇数位,请添加连续的数字,直到数字的总和为偶数为止。最后,将计算得出的总和串联起来以获得所需的密钥。
下面是上述方法的实现:
C++
// C++ program of the
// above approach
#include
using namespace std;
// Function to find the key
// of the given number
int key(int N)
{
// Convert the integer
// to String
string num = "" + to_string(N);
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for(j = 0; j < num.length(); j++)
{
// Check if digit is even or odd
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for(i = j; j < num.length(); j++)
{
add += num[j] - 48;
// Check if sum becomes odd
if (add % 2 == 1)
break;
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = (int)floor(log10(add) + 1);
ans *= (pow(10, digit));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else
{
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for(i = j; j < num.length(); j++)
{
add += num[j] - 48;
// Check if sum becomes even
if (add % 2 == 0)
{
break;
}
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = (int)floor(
log10(add) + 1);
ans *= (pow(10, digit));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.length())
{
return ans;
}
else
{
return ans += num[num.length() - 1] - 48;
}
}
// Driver code
int main()
{
int N = 1667848271;
cout << key(N);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program of the
// above approach
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
// Function to find the key
// of the given number
static int key(int N)
{
// Convert the integer
// to String
String num = "" + N;
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for (j = 0; j < num.length(); j++) {
// Check if digit is even or odd
if ((num.charAt(j) - 48) % 2 == 0) {
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48;
// Check if sum becomes odd
if (add % 2 == 1)
break;
}
if (add == 0) {
ans *= 10;
}
else {
int digit = (int)Math.floor(
Math.log10(add) + 1);
ans *= (Math.pow(10, digit));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else {
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48;
// Check if sum becomes even
if (add % 2 == 0) {
break;
}
}
if (add == 0) {
ans *= 10;
}
else {
int digit = (int)Math.floor(
Math.log10(add) + 1);
ans *= (Math.pow(10, digit));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.length()) {
return ans;
}
else {
return ans += num.charAt(
num.length() - 1)
- 48;
}
}
// Driver Code
public static void main(String[] args)
{
int N = 1667848271;
System.out.print(key(N));
}
}
Python3
# Python3 program of the
# above approach
import math
# Function to find the key
# of the given number
def key(N) :
# Convert the integer
# to String
num = "" + str(N)
ans = 0
j = 0
# Iterate the num-string
# to get the result
while j < len(num) :
# Check if digit is even or odd
if ((ord(num[j]) - 48) % 2 == 0) :
add = 0
# Iterate until odd sum
# is obtained by adding
# consecutive digits
i = j
while j < len(num) :
add += ord(num[j]) - 48
# Check if sum becomes odd
if (add % 2 == 1) :
break
j += 1
if (add == 0) :
ans *= 10
else :
digit = int(math.floor(math.log10(add) + 1))
ans *= (pow(10, digit))
# Add the result in ans
ans += add
# Assign the digit index
# to num string
i = j
else :
# If the number is odd
add = 0
# Iterate until odd sum
# is obtained by adding
# consecutive digits
i = j
while j < len(num) :
add += ord(num[j]) - 48
# Check if sum becomes even
if (add % 2 == 0) :
break
j += 1
if (add == 0) :
ans *= 10
else :
digit = int(math.floor(math.log10(add) + 1))
ans *= (pow(10, digit))
# Add the result in ans
ans += add
# assign the digit index
# to main numstring
i = j
j += 1
# Check if all digits
# are visited or not
if (j + 1) >= len(num) :
return ans
else :
ans += ord(num[len(num) - 1]) - 48
return ans
N = 1667848271
print(key(N))
# This code is contributed by divyesh072019
C#
// C# program of the
// above approach
using System;
class GFG{
// Function to find the key
// of the given number
static int key(int N)
{
// Convert the integer
// to String
String num = "" + N;
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for (j = 0; j < num.Length; j++)
{
// Check if digit is even or odd
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
// Check if sum becomes odd
if (add % 2 == 1)
break;
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = (int)Math.Floor(
Math.Log10(add) + 1);
ans *= (int)(Math.Pow(10, digit));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else
{
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
// Check if sum becomes even
if (add % 2 == 0)
{
break;
}
}
if (add == 0)
{
ans *= 10;
}
else {
int digit = (int)Math.Floor(
Math.Log10(add) + 1);
ans *= (int)(Math.Pow(10, digit));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.Length)
{
return ans;
}
else
{
return ans += num[num.Length - 1] - 48;
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 1667848271;
Console.Write(key(N));
}
}
// This code is contributed by Rajput-Ji
20291
时间复杂度: O(N)
辅助空间: O(1)