首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

【akOJ】1114 水仙花數題目信息

     編輯:關於C語言

【akOJ】1114 水仙花數

題目基本信息

  • 題目編號:1114
  • 標題:水仙花數
  • 時間限制:1000 MS
  • 內存限制:65536 KB
  • 總提交數:919
  • 通過數:294
  • 原題鏈接:[akOJ 1114](https://acm.hdu.edu.cn/showproblem.php?pid=1114) (注:原文鏈接指向 HDU,可能為轉載題)

題目大意(推測)

「水仙花數」(Narcissistic Number / Armstrong Number)指 n 位數,其各位數字 n 次方之和等於該數本身。經典定義為三位數:abc = a³ + b³ + c³(如 153, 370, 371, 407)。

常見 OJ 題目要求:

  1. 輸出所有三位水仙花數。
  2. 判斷給定數是否為水仙花數。
  3. 求 n 位水仙花數(n 為輸入)。

參考解法(三位數情況)

#include 
#include 

int main() {
    for (int i = 100; i < 1000; i++) {
        int a = i / 100;
        int b = (i / 10) % 10;
        int c = i % 10;
        if (a*a*a + b*b*b + c*c*c == i) {
            printf("%d\n", i);
        }
    }
    return 0;
}

通用判斷函數

bool isNarcissistic(int num) {
    int original = num;
    int digits = 0;
    int temp = num;
    while (temp > 0) { digits++; temp /= 10; }
    int sum = 0;
    temp = num;
    while (temp > 0) {
        int d = temp % 10;
        int p = 1;
        for (int i = 0; i < digits; i++) p *= d;
        sum += p;
        temp /= 10;
    }
    return sum == original;
}

備註

原文內容極少,僅含題目元信息。以上為基於題目名稱的標準解法補充。實際提交時請以 OJ 具體題面為準。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved