課程
/后端開發(fā)
/C
/C語言入門
4-9最后一題水仙花數的答案
2018-06-18
源自:C語言入門 4-9
正在回答
#include <stdio.h>
int main()
{
? ? //定義三位數num,個位數sd,十位數td,百位數hd
? ? int num, sd, td, hd;
? ? //循環(huán)所有三位數
? ? for(? num=100 ; num<1000? ;? ? num++? )
? ? {
? ? ? ? //獲取三位數字num百位上的數字
? ? ? ? hd =? ? num/100? ? ? ? ?;
? ? ? ? //獲取三位數字num十位上的數字
? ? ? ? td =? ? num/10%10? ? ? ? ?;
? ? ? ? //獲取三位數字num個位上的數字
? ? ? ? sd =? ? num%10? ? ? ? ?;
? ? ? ? //水仙花數的條件是什么?
? ? ? ? if(? ? ?num==hd*hd*hd +td*td*td+sd*sd*sd? ? ? ? ? ? ? ?)?
? ? ? ? {
? ? ? ? ? ? printf("水仙花數字:%d\n", num);? ??
? ? ? ? }
? ? }
? ? return 0;? ??
}
? ? int num, a, b, c;//分別代表百位,十位,個位?
? ? for( num=100;num<=999;num++)
? ? ? ? a =num/100 ;
? ? ? ? b =(num-a*100)/10 ;
? ? ? ? c =num-a*100-b*10 ;
? ? ? ? if(num==a*a*a+b*b*b+c*c*c )?
? ? ? ? printf("水仙花數字:%d\n", num); ?
? ? return 0; ? ?
最后結果是:
水仙花數字:153
水仙花數字:370
水仙花數字:371
水仙花數字:407
客官你好壞
qq_S茶T栗_0 回復 客官你好壞
qq_S茶T栗_0 回復 qq_S茶T栗_0
楓中看雨 回復 客官你好壞
羅索_ 回復 客官你好壞
qq_哈哈你是誰_0 回復 客官你好壞
mengxuanmenghui 回復 客官你好壞
修裾欲溯空 回復 客官你好壞
舉報
C語言入門視頻教程,帶你進入編程世界的必修課-C語言
2 回答4-9最后一題水仙花數的答案
2 回答4-9水仙花數
3 回答if語句括號內直接用=號,為什么答案就為水仙花數字:1 水仙花數字:8 水仙花數字:729 水仙花數字:370 水仙花數字:371 水仙花數字:378 水仙花數字:1099
2 回答水仙花數
3 回答水仙花數問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優(yōu)惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-06
#include <stdio.h>
int main()
{
? ? //定義三位數num,個位數sd,十位數td,百位數hd
? ? int num, sd, td, hd;
? ? //循環(huán)所有三位數
? ? for(? num=100 ; num<1000? ;? ? num++? )
? ? {
? ? ? ? //獲取三位數字num百位上的數字
? ? ? ? hd =? ? num/100? ? ? ? ?;
? ? ? ? //獲取三位數字num十位上的數字
? ? ? ? td =? ? num/10%10? ? ? ? ?;
? ? ? ? //獲取三位數字num個位上的數字
? ? ? ? sd =? ? num%10? ? ? ? ?;
? ? ? ? //水仙花數的條件是什么?
? ? ? ? if(? ? ?num==hd*hd*hd +td*td*td+sd*sd*sd? ? ? ? ? ? ? ?)?
? ? ? ? {
? ? ? ? ? ? printf("水仙花數字:%d\n", num);? ??
? ? ? ? }
? ? }
? ? return 0;? ??
}
2018-06-18
#include <stdio.h>
int main()
{
? ? int num, a, b, c;//分別代表百位,十位,個位?
? ? for( num=100;num<=999;num++)
? ? {
? ? ? ? a =num/100 ;
? ? ? ? b =(num-a*100)/10 ;
? ? ? ? c =num-a*100-b*10 ;
? ? ? ? if(num==a*a*a+b*b*b+c*c*c )?
? ? ? ? printf("水仙花數字:%d\n", num); ?
? ? }
? ? return 0; ? ?
}
最后結果是:
水仙花數字:153
水仙花數字:370
水仙花數字:371
水仙花數字:407