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