查詢字串字母值的個位數之和
為了找到字串字母值的個位數之和,我們將探討字串的字母值,並將數值分配給字母表中的字母。我們將深入瞭解這個概念和一個示例來闡述所涉及的步驟、此過程背後的演算法、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。
結論
我們探討了查詢字串中存在的順序屬性的個位數之和這一有趣的想法。使用這種方法,我們可以輕鬆地在字母和數值之間切換,並應用數學運算來生成壓縮且清晰的結果。我們採用了將字串轉換為大寫或小寫的做法,以保持一致性。我們透過將它們分配的值加起來來量化字母的累積意義。最後,透過將總和減少到個位數,我們可以壓縮和簡化輸出。使用這種方法,可以正確地表示和分析字串中包含的字母值。