這樣不可以么
#include?<stdio.h>int?main(){????//定義三位數num,個位數sd,十位數td,百位數hd????int?num,?sd,?td,?hd;????//循環所有三位數????for(hd=0,td=0,sd=0;hd<10&&td<10&&sd<10;hd++,td++,sd++)????{????????num=hd*100+td*10+sd;????????if(num==hd*hd*hd+td*td*td+sd*sd*sd)?????????{????????????printf("水仙花數字:%d\n",?num);????????????}????}????return?0;????}
2019-11-26
好厲害??!?
2019-11-08
你這樣寫的話,個位數,十位數,百位數是同時+1的,相當于你只判斷了000,111,222,333,444,555,666,777,888,999。如果真要這么寫,需要三重for循環,把個位十位百位分開,像下面這樣,而且百位要從1開始
#include <stdio.h>
int main(){??? //定義三位數num,個位數sd,十位數td,百位數hd
??? int num, sd, td, hd;??? //循環所有三位數
??? for(hd=1;hd<10;hd++)
??????? {
??????????? for(td=0;td<10;td++)
??????????? {
? ? ? ? ? ? ? ? for(sd=0;sd<10;sd++)
? ? ? ? ? ? ?? {
??????????? num=hd*100+td*10+sd;
??????? if(num==hd*hd*hd+td*td*td+sd*sd*sd)
??????? {
??????????? printf("水仙花數字:%d\n", num);
? ? ? ? }
? ? ? ? ? ? ?? }
? ? ? ? ? }
??? }
??? return 0;
??? }