注册 登录
编程论坛 C语言论坛

输入一个三位数,判断是不是水仙花数(各位数字的立方和等于这个三位数本身)

wgxlogo 发布于 2021-11-09 14:15, 1569 次点击
请各位不吝赐教
4 回复
#2
rjsp2021-11-09 15:16
程序代码:
#include <stdbool.h>

bool foo( unsigned n )
{
    unsigned a = n/100%10;
    unsigned b = n/10%10;
    unsigned c = n/1%10;
    return n>=100 && n<1000 && a*a*a+b*b*b+c*c*c==n;
}

bool bar( unsigned n )
{
    return n==153 || n==370 || n==371 || n==407;
}
#3
diycai2021-11-10 14:41
程序代码:
#include <stdio.h>
void main()
{
int a,i,array[]={153,370,371,407,0};
while(scanf("%d",&a))if(!(a>=0[array]&&a<=array[sizeof(array)/sizeof(int)-2]?i=0:printf("no\n")))for(;i[array];i++,!i[array]?!a?printf("yes\n"):printf("no\n"):0)if(!(i[array]^a))a=0;
}
#4
ojyy2021-11-11 21:15
#include<stdio.h>
int main()
{
    int x;
    printf("输入一个三位数:");
    scanf("%d",&x);
    int a,b,c;
    if(x>=100 && x<1000)
    {
      a=x/100%10;
      b=x/10%10;
      c=x%10;
    if(x==a*a*a+b*b*b+c*c*c)
      printf("%d是水仙花数\n",x);
    else
      printf("%d不是水仙花数\n",x);
    }
    return 0;
}
#5
Hhu_TF2021-11-12 00:13
程序代码:
#include <stdio.h>

int main() {
    int a, b, c, x;
    printf("请输入一个三位数:");
    scanf("%d", &x);      //输入值设为x
    if (100 <= x && x <= 1000) {
        a = x / 100;      //a为x的百位数
        b = x / 10 % 10;  //b为x的十位数
        c = x % 10;       //c为x的个位数
        if (a * a * a + b * b * b + c * c * c == x) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    } else {
        printf("输入错误!\n");  //提醒非三位数的错误输入
    }
    return 0;
}
1