1. 程式人生 > >杭電oj 水仙花數

杭電oj 水仙花數

otto 測試 輸入數據 杭電 int script lag div pri

Problem Description 春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,他是這樣定義的:
“水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=1^3+5^3+3^3。
現在要求輸出所有在m和n範圍內的水仙花數。

Input 輸入數據有多組,每組占一行,包括兩個整數m和n(100<=m<=n<=999)。

Output 對於每個測試實例,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
如果給定的範圍內不存在水仙花數,則輸出no;
每個測試實例的輸出占一行。

Sample Input 100 120 300 380

Sample Output no 370 371
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m,n,i,a,b,c;
    int flag = 0;//用來控制輸出空格
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        for(i=m;i<=n;i++)
        {
            a 
= i/100; b = (i-a*100)/10; c = i-a*100-b*10; if(i==(a*a*a+b*b*b+c*c*c)) {if(flag==1)printf(" ");//輸出嚴格控制,最後一個數後面不能跟空格 printf("%d",i); flag = 1;} } if(flag==0) {printf("no\n");}
else printf("\n"); flag=0; } return 0; }

杭電oj 水仙花數