2017年11月26日 星期日

[C_ST33-易] 英文字的分數(C++)

[C_ST33-易] 英文字的分數

問題描述:
前陣子很流行一個勵志遊戲,把所有的英文字母,轉成分數來看, a 算 1 、 b 算 2 、 c 算 3 、以此類推下去,例如; attitude ( 態度 ) 為 100 分 (1+20+20+9+20+21+4+5) , talent( 天才 ) 為 72 分 (20+1+12+5+14+20)
輸入說明:
輸入一個英文單字
輸出說明:
輸出一個數字
範例:
Sample InputSample Output
attitude100


  1. #include <iostream>  
  2. #include <ctype.h>  
  3. #include<string>  
  4. #include <stdio.h>  
  5. #include <string.h>  
  6.   
  7. using namespace std;  
  8.   
  9. int main() {  
  10.     // [C_ST33-易] 英文字的分數  
  11.     string text;  
  12.     int grade = 0;  
  13.     getline(cin, text);  
  14.     char chText[text.size()];  
  15.     strcpy(chText, text.c_str());  
  16.     for(int i = 0;i < text.size();i++)  
  17.     {  
  18.         chText[i] = tolower(chText[i]);  
  19.         grade = grade + (chText[i]-96);  
  20.     }  
  21.     cout << grade << endl;  
  22.   
  23.     return 0;  
  24. }  

沒有留言:

張貼留言