Created
December 7, 2018 14:42
-
-
Save zwfang/96edce116ae8d4bb7fc0bf69647aad69 to your computer and use it in GitHub Desktop.
水仙花数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// narcissisticNumber | |
// | |
// Created by fzw on 10/7/18. | |
// Copyright © 2018 n. All rights reserved. | |
// | |
#include <stdio.h> | |
void narcissisticNumber(int m, int n); | |
int main(int argc, const char * argv[]) { | |
// insert code here... | |
narcissisticNumber(100, 200); | |
return 0; | |
} | |
void narcissisticNumber(int m, int n) { | |
if (m > n) { | |
return; | |
} | |
int flag = 0; | |
for (int o = m, i, j, k = 0; o <= n; o++) { | |
i = o / 100; | |
j = o / 10 % 10; | |
k = o % 10; | |
if (i*i*i + j*j*j + k*k*k == o) { | |
flag = 1; | |
printf("%d ", o); | |
} | |
} | |
if (flag == 0) { | |
printf("no\n"); | |
} else { | |
printf("\n"); | |
} | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 找出m~n之间的水仙花数。 | |
function narcissisticNumber(m, n){ | |
for (var i = m;i <= n;i++) { | |
let sum = 0; | |
let arr = i.toString().split(""); | |
for (var j = 0;j < arr.length;j++) { | |
// console.log(arr[j]); | |
var num = parseFloat(arr[j]); | |
var z = Math.pow(num,3); | |
sum += z; | |
} | |
if (i === sum) { | |
console.log(i); | |
} else { | |
console.log(null); | |
} | |
} | |
} | |
narcissisticNumber(1, 154); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment