3 位奧西里斯數 C 程式?


在此我們將看到奧西里斯數字。奧西里斯數字是這種數字,它們等於其自身數字的子樣本的排列之和。假設數字為 132。那麼如果我們計算 {12 + 21 + 13 + 31 + 23 + 32},這同樣也是 132。因此,該數是奧西里斯數字。我們必須檢查給定的數字是否是奧西里斯數字。

方法很簡單。如果我們分析這些數字,每個數字出現兩次,因此它們在個位數和十位數上。因此,我們可以透過用 11 乘以它們來進行檢查。

演算法

isOsirisNumber(n) −

Begin
   a := last digit
   b := second digit
   c := first digit
   digit_sum := a + b + c
   if n = (22 * digit_sum), then
      return true
   end if
   return false
End

示例

#include
using namespace std;
bool isOsirisNumber(int n) {
   int a = n % 10;
   int b = (n / 10) % 10;
   int c = n / 100;
   int sum = a + b + c;
   if (n == (22 * sum)) {
      return true;
   }
   return false;
}
int main() {
   int n = 132;
   if (isOsirisNumber(n))
      cout << "This is Osiris number";
   else
      cout << "This is Not Osiris number";
}

輸出

This is Osiris number

更新於: 19-8-2019

102 次瀏覽

開啟您的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.