在 C++ 中組成總計為 n 所需的最小字母數。
問題陳述
給定整數 n,設 a = 1,b = 2,c= 3,……,z = 26。任務是找出組成總計為 n 所需的最小字母數。
If n = 23 then output is 1 If n = 72 then output is 3(26 + 26 + 20)
演算法
1. If n is divisible by 26 then answer is (n/26) 2. If n is not divisible by 26 then answer is (n/26) + 1
示例
#include <iostream>
using namespace std;
int minRequiredSets(int n){
if (n % 26 == 0) {
return (n / 26);
} else {
return (n / 26) + 1;
}
}
int main(){
int n = 72;
cout << "Minimum required sets: " << minRequiredSets(n) << endl;
return 0;
}輸出
當你編譯和執行上述程式時,它會生成以下輸出 −
Minimum required sets: 3
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP