zoukankan      html  css  js  c++  java
  • Java练习 SDUT-1239_水仙花数

    水仙花数

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Problem Description

    春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,是这样定义的:
    “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。
    现在要求输出所有在m和n范围内的水仙花数。

    Input

    输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

    Output

    对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
    如果给定的范围内不存在水仙花数,则输出no;
    每个测试实例的输出占一行。

    Sample Input

    100 120
    300 380

    Sample Output

    no
    370 371

    把100到999的水仙花数打表保存就好(其实就四个153 370 371 407)
    然后与输入的范围比较,输出就可以了。

    import java.util.*;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner cin = new Scanner(System.in);
    		int i,n,m,num,a[] = new int[10000];
    		int f,b[] = new int [10];
    		num = 0;
    		for(i=100;i<=999;i++)
    			if(judge(i)==1)
    				a[num++] = i;
    		//153 370 371 407;
    		while(cin.hasNext())
    		{
    			f = 0;
    			n = cin.nextInt();
    			m = cin.nextInt();
    			for(i=0;i<num;i++)
    				if(a[i]>=n&&a[i]<=m)
    					b[f++] = a[i];
    			if(f==0)
    				System.out.println("no");
    			else
    				for(i=0;i<f;i++)
    					if(i==f-1)
    						System.out.println(b[i]);
    					else
    						System.out.print(b[i]+" ");
    		}
    		cin.close();
    	}
    	public static int judge(int x)
    	{
    		int a,b,c;
    		a = x / 100;
    		b = x / 10 % 10;
    		c = x % 10;
    		if(a*a*a+b*b*b+c*c*c==x)
    			return 1;
    		return 0;
    	}
    }
    
  • 相关阅读:
    PHP webserver 之 soap wsdl
    PHP webserver 之 soap 生成wsdl文件
    PHP webserver 之 soap non-wsdl
    CodeForces 729A Interview with Oleg (模拟)
    CodeForces 727A Transformation: from A to B (DFS)
    POJ 3111 K Best (二分)
    POJ 2456 Aggressive cows (二分)
    POJ 1064 Cable master(二分)
    POJ
    Codeforces 869B
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9706415.html
Copyright © 2011-2022 走看看