查找字符串字母值的个位数之和
为了找到字符串字母值的个位数之和,我们将探讨字符串的字母值,并将数值分配给字母表中的字母。我们将深入了解这个概念和一个示例来阐述所涉及的步骤、此过程背后的算法、C++ 中的示例代码实现,最后简要总结此技术的意义。
概念
这个想法围绕着将数值与每个字母相关联并执行算术运算来计算个位数之和,即'A'=1 或'a'=1,'B'=2 或'b'=2,依此类推。通过将字符串转换为大写或小写,我们确保一致性并消除任何差异,即转换为一种情况将帮助我们轻松有效地评估和计算所需的结果。对数值求和使我们能够捕获字母的累积值。最后,将总和减少到个位数简化并提供了所需的结果。
算法
将字符串作为输入。
将字符串转换为大写或小写以确保和保持一致性。
为输入字符串的每个字母分配数值→'A'对应1,'B'对应2,'C'对应3,依此类推,直到'Z'对应26。
通过迭代字符串中的每个字母,计算步骤 2 中分配给字母的值之和。
将步骤 3 中计算出的总和减少到个位数→如果步骤 3 中获得的总和有多位数,则继续通过将每个数字相加来减少它,直到达到个位数结果。
以单词“LIFE”为例 -
将字符串转换为大写或小写字母。
为字符串的字母赋予数值,例如 L = 12、I = 9、F = 6 和 E = 5。
计算值的总和:12+9+6+5= 32
将总和简化为个位数:3 + 2 = 5。
因此,“LIFE”的个位数总和为 5。
示例
以下是不同编程语言中上述方法的实现:C、C++、Java 和 Python -
#include <stdio.h> int alphabet_sum(char* st){ int total_sum = 0; // Iterate through each character in the string while (*st != '\0') { char ch = *st; // Convert lowercase to uppercase for maintaining consistency if (ch >= 'a' && ch <= 'z') { ch = ch - 'a' + 'A'; } // Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1. total_sum += ch - 'A' + 1; // Move to the next character in the string st++; } // Keep summing the digits until it becomes a single digit while (total_sum > 9) { int digits_sum = 0; // Calculate the sum of digits while (total_sum != 0) { digits_sum += total_sum % 10; // Calculate remainder (i.e., ones place digit) total_sum /= 10; // Calculate quotient (i.e., tens place digit) } // Update the total sum total_sum = digits_sum; } return total_sum; } int main(){ char input_str[] = "LIFE"; int answer = alphabet_sum(input_str); printf("The single digit sum of the alphabetical values is: %d\n", answer); return 0; }
输出
The single digit sum of the alphabetical values is: 5
#include <iostream> #include <string> using namespace std; int alphabet_sum(string& st) { int total_sum = 0; for (char ch : st) { if (ch >= 'a' && ch <= 'z') { ch=toupper(ch); // Convert lowercase to uppercase for maintaining consistency } total_sum += ch - 'A' + 1; //calculates the sum by subtracting the ASCII value of 'A' from character and adding 1. } while (total_sum > 9) { int digits_sum = 0; while (total_sum != 0) { digits_sum += total_sum % 10; //calculates remainder i.e. ones place digit total_sum /= 10; //gives quotient i.e tens place digit } total_sum = digits_sum; } return total_sum; } int main() { string input_str="LIFE"; int answer = alphabet_sum(input_str); cout << "The single digit sum of the alphabetical values is: " << answer << endl; return 0; }
输出
The single digit sum of the alphabetical values is: 5
public class AlphabetSum { static int alphabetSum(String st) { int totalSum = 0; // Iterate through each character in the string for (char ch : st.toCharArray()) { // Convert lowercase to uppercase for maintaining consistency if (ch >= 'a' && ch <= 'z') { ch = Character.toUpperCase(ch); } // Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1. totalSum += ch - 'A' + 1; } // Keep summing the digits until it becomes a single digit while (totalSum > 9) { int digitsSum = 0; // Calculate the sum of digits while (totalSum != 0) { digitsSum += totalSum % 10; // Calculate remainder (i.e., ones place digit) totalSum /= 10; // Calculate quotient (i.e., tens place digit) } // Update the total sum totalSum = digitsSum; } return totalSum; } public static void main(String[] args) { String inputStr = "LIFE"; int answer = alphabetSum(inputStr); System.out.println("The single digit sum of the alphabetical values is: " + answer); } }
输出
The single digit sum of the alphabetical values is: 5
def alphabet_sum(st): total_sum = 0 # Iterate through each character in the string for ch in st: # Convert lowercase to uppercase for maintaining consistency if 'a' <= ch <= 'z': ch = ch.upper() # Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1. total_sum += ord(ch) - ord('A') + 1 # Keep summing the digits until it becomes a single digit while total_sum > 9: digits_sum = 0 # Calculate the sum of digits while total_sum != 0: digits_sum += total_sum % 10 # Calculate remainder (i.e., ones place digit) total_sum //= 10 # Calculate quotient (i.e., tens place digit) # Update the total sum total_sum = digits_sum return total_sum if __name__ == "__main__": input_str = "LIFE" answer = alphabet_sum(input_str) print("The single digit sum of the alphabetical values is:", answer)
输出
The single digit sum of the alphabetical values is: 5
测试用例
Test case → Input String = "GURU"
解释
alphabet_sum 函数使用输入字符串“GURU”调用。
该函数遍历字符串的每个字符,只考虑字母字符。对于遇到的每个字母字符,它使用内置字符串函数将任何小写字符转换为大写,以将字符转换为大写。
'G' 转换为大写,其值添加到总和中:7 + 0 = 7
'U' 转换为大写,其值添加到总和中:7 + 21 = 28
'R' 转换为大写,其值添加到总和中:28 + 18 = 46
'U' 转换为大写,其值添加到总和中:46 + 21 = 67
由于获得的总和 67 不是个位数,因此代码进入 while 循环以将总和减少到个位数。
内部 while 循环重复添加总和的各个数字,即 6 + 7 = 13。
上一步中获得的简化总和为 13,它再次经历上一步中解释的过程,即 1+3= 4,这是一个个位数答案,因此从 alphabet_sum 函数返回。
输入字符串“GURU”中字母值的个位数之和在主函数中显示在控制台上,结果为 result = 4。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
我们探讨了查找字符串中存在的顺序属性的个位数之和这一有趣的想法。使用这种方法,我们可以轻松地在字母和数值之间切换,并应用数学运算来生成压缩且清晰的结果。我们采用了将字符串转换为大写或小写的做法,以保持一致性。我们通过将它们分配的值加起来来量化字母的累积意义。最后,通过将总和减少到个位数,我们可以压缩和简化输出。使用这种方法,可以正确地表示和分析字符串中包含的字母值。