M. Nambiar设计了一种机制来处理任何给定的数字,从而生成新的结果数字。他将此机制称为“ Nambiar编号生成器”,并将所得的数字称为“ Nambiar编号”。
机制:在给定的数字中,从第一个数字开始,继续加总所有后续数字,直到数字总和的状态(偶数或奇数)与第一个数字的状态(奇数或偶数)相反。继续此形式的下一个数字,直到到达该数字的最后一个数字。因此,将总和连接起来便会产生Nambiar数。
例子:
Input: N = 9880127431
Output: 26971
First digit | Next valid consecutive digits | Resultant number |
---|---|---|
9880127431 | 9880127431 | 26 |
9880127431 | 9880127431 | 269 |
9880127431 | 9880127431 | 2697 |
9880127431 | 9880127431 | 26971 |
Input: N = 9866364552
Output: 32157
方法:对于左边第一个未使用的数字,请检查它是偶数还是奇数。如果数字是偶数,则找到从当前数字开始的连续数字的总和,该数字为奇数(如果第一个数字为奇数,则为偶数)。将这个总和连接到结果数字上,并从左边的第一个未使用的数字开始重复整个过程。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the Nambiar
// number of the given number
string numbiarNumber(string str, int i)
{
// If there is no digit to choose
if (i > str.length())
return "";
// Choose the first digit
int firstDigit = str[i] - '0';
// Chosen digit's parity
int digitParity = firstDigit % 2;
// To store the sum of the consecutive
// digits starting from the chosen digit
int sumDigits = 0;
// While there are digits to choose
while (i < str.length())
{
// Update the sum
sumDigits += (str[i] - '0');
int sumParity = sumDigits % 2;
// If the parity differs
if (digitParity != sumParity)
break;
i++;
}
// Return the current sum concatenated with the
// Numbiar number for the rest of the string
return (to_string(sumDigits) +
numbiarNumber(str, i + 1));
}
// Driver code
int main()
{
string str = "9880127431";
cout << numbiarNumber(str, 0) << endl;
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java implementation of the approach
class GFG {
// Function to return the Nambiar
// number of the given number
static String nambiarNumber(String str, int i)
{
// If there is no digit to choose
if (i >= str.length())
return "";
// Choose the first digit
int firstDigit = (str.charAt(i) - '0');
// Chosen digit's parity
int digitParity = firstDigit % 2;
// To store the sum of the consecutive
// digits starting from the chosen digit
int sumDigits = 0;
// While there are digits to choose
while (i < str.length()) {
// Update the sum
sumDigits += (str.charAt(i) - '0');
int sumParity = sumDigits % 2;
// If the parity differs
if (digitParity != sumParity) {
break;
}
i++;
}
// Return the current sum concatenated with the
// Numbiar number for the rest of the string
return ("" + sumDigits + nambiarNumber(str, i + 1));
}
// Driver code
public static void main(String[] args)
{
String str = "9880127431";
System.out.println(nambiarNumber(str, 0));
}
}
Python3
# Java implementation of the approach
# Function to return the Nambiar
# number of the given number
def nambiarNumber(Str,i):
# If there is no digit to choose
if (i >= len(Str)):
return ""
# Choose the first digit
firstDigit =ord(Str[i])-ord('0')
# Chosen digit's parity
digitParity = firstDigit % 2
# To store the sum of the consecutive
# digits starting from the chosen digit
sumDigits = 0
# While there are digits to choose
while (i < len(Str)):
# Update the sum
sumDigits += (ord(Str[i]) - ord('0'))
sumParity = sumDigits % 2
# If the parity differs
if (digitParity != sumParity):
break
i += 1
# Return the current sum concatenated with the
# Numbiar number for the rest of the String
return ("" + str(sumDigits) +
nambiarNumber(Str, i + 1))
# Driver code
Str = "9880127431"
print(nambiarNumber(Str, 0))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach.
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the Nambiar
// number of the given number
static String nambiarNumber(String str, int i)
{
// If there is no digit to choose
if (i >= str.Length)
return "";
// Choose the first digit
int firstDigit = (str[i] - '0');
// Chosen digit's parity
int digitParity = firstDigit % 2;
// To store the sum of the consecutive
// digits starting from the chosen digit
int sumDigits = 0;
// While there are digits to choose
while (i < str.Length)
{
// Update the sum
sumDigits += (str[i] - '0');
int sumParity = sumDigits % 2;
// If the parity differs
if (digitParity != sumParity)
{
break;
}
i++;
}
// Return the current sum concatenated with the
// Numbiar number for the rest of the string
return ("" + sumDigits + nambiarNumber(str, i + 1));
}
// Driver code
public static void Main(String[] args)
{
String str = "9880127431";
Console.WriteLine(nambiarNumber(str, 0));
}
}
// This code is contributed by Rajput-Ji
输出:
26971