在 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

更新于: 31-Oct-2019

60 次浏览

开启你的职业生涯

完成课程后获得认证

开始
广告
© . All rights reserved.