老哥们,c++打印水仙花数只能用do...while写吗?
我用while循环和if都写不出来,程序根本不走字!大哥们能帮我看看是哪里出了问题吗
#include<iostream>
using namespace std;
int main()
{
int i = 100;
int a = 0;
int b = 0;
int c = 0;
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
while (a * a * a + b * b * b + c * c * c == i&&i<1000)
{
cout << i << endl;
i++;
}
system("pause");
return 0;
} for item in range(100, 1000):
ge = item%10 #个位
shi = item//10%10 #十位
bai = item//100#百位
#print(bai,shi,ge)
#判断
if ge**3+shi**3+bai**3==item:
print(item)
这是我用pycharm写的,虽然语言不一样,但是可以用if显得简洁很多啊
#include<iostream>
using namespace std;
int main(void)
{
int a , b , c , i ;
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) cout << i << endl ;
}
system("pause") ;
}
编译、运行实况
D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
153
370
371
407
请按任意键继续. . .
D:\00.Excise\C> jackz007 发表于 2021-3-26 01:27
编译、运行实况
就是说要用到for是吧,我明白了,谢谢老哥 int main()
{
int a = 0, b = 0, c = 0;
for(int i = 100; i < 1000; i++)
{
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
cout << i << endl;
}
}
return 0;
} {:10_331:}来迟了 本帖最后由 yuxijian2020 于 2021-3-26 09:20 编辑
我也来晚了,那我就多给你整几个{:10_256:}
#include<iostream>
using namespace std;
void WhileLoop()
{
int a = 0, b = 0, c = 0, i = 100;
while (i < 1000)
{
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
if ((a * a * a + b * b * b + c * c * c) == i)
printf_s("%d\n", i);
i += 1;
}
}
void DoWhileLoop()
{
int i = 100;
int a = 0, b = 0, c = 0;
do
{
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
if ((a * a * a + b * b * b + c * c * c) == i)
printf_s("%d\n", i);
i += 1;
} while (i < 1000);
}
int main()
{
printf_s("WhileLoop-------\n");
WhileLoop();
printf_s("DoWhileLoop-------\n");
DoWhileLoop();
return 0;
}
WhileLoop-------
153
370
371
407
DoWhileLoop-------
153
370
371
407
页:
[1]