C++语言编程 检查一个3位数是水仙花数

检查一个3位数是水仙花数输入:一个数字,比如 371输出:是的,这个数字是一个水仙花数,
如果不是则输出 这个数字不是水仙花数。

 #include <iostream> 
using namespace std;
int main()
{
    int a, b, c, y, n = 0;
    cout << "请输入三位数字:" << endl;
    cin >> n;

    a = n % 1000 / 100;  //求第一位数 
    b = n % 100 / 10;    //求第二位数
    c = n % 10 / 1;      //求第三位数 
    y = a*a*a + b*b*b + c*c*c;
    if (y == n) cout << n << "是水仙花数" << endl;
    else cout << n << "不是水仙花数" << endl;


    system("pause");
    return 0;
}

#include
using namespace std;
int main()
{
int n;
cin >> n;
int sum = 0, m = n;
while (m != 0)
{
int x = m % 10; //截取n的个位
m /= 10; //去除n的个位
sum += x * x * x;
}
if (sum == n)
cout << "Yes\n";
else
cout << "No\n";
system("pause");
return 0;
}


#include
int main()
{
int i,a,b,c;
for(i=100;i<1000;i++)
{
a=i/100;
b=(i/10)%10;
c=i%10;
if((a*a*a+b*b*b+c*c*c)==i)
printf("%d\n",i);
}
return(0);
}
这是打印1000以内的水仙花数,希望对您有帮助。

#include
void main()
{
int i,j,k,n;
printf("请输入一个三位整数\n");

i=n/100;
j=n/10%10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%-5d是水仙花数",n);
}
else
{
printf("%-5d不是水仙花数",n);
}
printf("\n");
}

#include <iostream> 
using namespace std;
int main()
{
    int a, b, c, y, n = 0;
    cin >> n;

    a = n % 1000 / 100;  //求第一位数 
    b = n % 100 / 10;    //求第二位数
    c = n % 10 / 1;      //求第三位数 
    y = a*a*a + b*b*b + c*c*c;
    if (y == n) cout << n << "是水仙花数" << endl;
    else cout << n << "不是水仙花数" << endl;


    system("pause");
    return 0;
}

#include
using namespace std;
int main()
{
int a, b, c, y, n = 0;
count << "三位数:" << endl;
cin >> n;

a = n % 1000 / 100;  
b = n % 100 / 10;   
c = n % 10 / 1;  
y = a*a*a + b*b*b + c*c*c;
if (y == n) count << n << "是" << endl;
else cout << n << "不是" << endl;


system("pause");
return 0;

}


 #include <iostream> 
using namespace std;
int main()
{
    int a, b, c, y, n = 0;
    cout << "请输入三位数字:" << endl;
    cin >> n;
 
    a = n % 1000 / 100;  //求第一位数 
    b = n % 100 / 10;    //求第二位数
    c = n % 10 / 1;      //求第三位数 
    y = a*a*a + b*b*b + c*c*c;
    if (y == n) cout << n << "是水仙花数" << endl;
    else cout << n << "不是水仙花数" << endl;
 
 
    system("pause");
    return 0;
}

希望对您有帮助qwq
思路:

  • 运用数位分离的方法
  • 水仙花的判定条件:
    个位的三次方+十位的三次方+百位的三次方 = 自己本身

代码:

#include <iostream>
using namespace std;
int main()
{
    int N,g,s,b;
    cin >> N;
    g = N % 10;
    s = N / 10 % 10;
    b = N / 100;
    if(g*g*g + s*s*s + b*b*b == N)
    {
        cout << "yes" ;
    }
    else
    {
        cout << "no" ;
    }
}

附赠:

  • 153
  • 370
  • 371
  • 407