查找字符串的字母值的个位数总和
给定大小为N的字符串S ,任务是通过给定字符串中所有字母的顺序总和获得的值的重复数字总和来找到一位数和。
The order of alphabets is given by the position at which they occur in English Alaphabets.
例子:
Input: S = “geek”
Output: 1
Explanation:
The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.
The single digit sum obtained by sum of 28 = 2 + 8 = 10 = 1 + 0 = 1.
Input: S = “GeeksforGeeks”
Output: 7
方法:给定问题可以通过首先通过添加值(S[i] – 'a' + 1)或(S[i] – 'A来找到给定字符串S中存在的所有字母的顺序和' + 1)如果S[i]是小写或大写字符。找到sum的值后,使用本文讨论的方法找到该值的一位数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
int findTheSum(string str)
{
string alpha;
// Traverse the given string
for (int i = 0; i < str.length(); i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z')
|| (str[i] >= 'a' && str[i] <= 'z'))
alpha.push_back(str[i]);
}
// Stores the sum of order of values
int score = 0, n = 0;
for (int i = 0; i < alpha.length(); i++) {
// Find the score
if (alpha[i] >= 'A' && alpha[i] <= 'Z')
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
// Return the resultant sum
return n;
}
// Driver Code
int main()
{
string S = "GeeksforGeeks";
cout << findTheSum(S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static int findTheSum(char []str)
{
String alpha="";
// Traverse the given String
for (int i = 0; i < str.length; i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z')
|| (str[i] >= 'a' && str[i] <= 'z'))
alpha+=(str[i]);
}
// Stores the sum of order of values
int score = 0, n = 0;
for (int i = 0; i < alpha.length(); i++) {
// Find the score
if (alpha.charAt(i) >= 'A' && alpha.charAt(i) <= 'Z')
score += alpha.charAt(i) - 'A' + 1;
else
score += alpha.charAt(i) - 'a' + 1;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
// Return the resultant sum
return n;
}
// Driver Code
public static void main(String[] args)
{
String S = "GeeksforGeeks";
System.out.print(findTheSum(S.toCharArray()));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
def findTheSum(str):
alpha = ""
# Traverse the given string
for i in range(0, len(str)):
# If character is an alphabet
if ((str[i] >= 'A' and str[i] <= 'Z') or (str[i] >= 'a' and str[i] <= 'z')):
alpha += str[i]
# Stores the sum of order of values
score = 0
n = 0
for i in range(0, len(alpha)):
# Find the score
if (alpha[i] >= 'A' and alpha[i] <= 'Z'):
score += ord(alpha[i]) - ord('A') + 1
else:
score += ord(alpha[i]) - ord('a') + 1
# Find the single digit sum
while (score > 0 or n > 9):
if (score == 0):
score = n
n = 0
n += score % 10
score = score // 10
# Return the resultant sum
return n
# Driver Code
if __name__ == "__main__":
S = "GeeksforGeeks"
print(findTheSum(S))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
static int findTheSum(string str)
{
string alpha = "";
// Traverse the given string
for (int i = 0; i < str.Length; i++) {
// If character is an alphabet
if ((str[i] >= 'A' && str[i] <= 'Z')
|| (str[i] >= 'a' && str[i] <= 'z'))
alpha += (str[i]);
}
// Stores the sum of order of values
int score = 0, n = 0;
for (int i = 0; i < alpha.Length; i++) {
// Find the score
if (alpha[i] >= 'A' && alpha[i] <= 'Z')
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
// Find the single digit sum
while (score > 0 || n > 9) {
if (score == 0) {
score = n;
n = 0;
}
n += score % 10;
score /= 10;
}
// Return the resultant sum
return n;
}
// Driver Code
public static void Main()
{
string S = "GeeksforGeeks";
Console.WriteLine(findTheSum(S));
}
}
// This code is contributed by ukasp.
Javascript
输出:
7
时间复杂度: O(N)
辅助空间: O(1)