码迷,mamicode.com
首页 > 其他好文 > 详细

Colorful Number

时间:2014-11-21 10:26:50      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   strong   

Problem

A number can be broken into different sub-sequence parts. Suppose, a number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorful number, since product of every digit of a sub-sequence are different. That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20 (3*2*4)= 24 (2*4*5)= 40
But 326 is not a colorful number as it generates 3 2 6 (3*2)=6 (2*6)=12.
You have to write a function that tells if the given number is a colorful number or not.

Solution

 1 public static boolean colorfulNumber(int num) {
 2     //if(num == null) return false;
 3 
 4     //convert num to arraylist
 5     List<Integer> al = new ArrayList<Integer>();
 6     while(num > 0) {
 7         al.add(num%10);
 8         num /= 10;
 9     }
10     System.out.println(al);
11     
12     //store res into hashset
13     Set<Integer> hs = new HashSet<Integer>();
14     for(int i=1; i<al.size(); i++) {
15         for(int j=0; j<al.size()-i+1; j++) {
16             int pro = al.get(j);
17             for(int k=j+1; k<j+i; k++) {
18                 pro = pro * al.get(k);
19             }
20             System.out.println(pro);
21             if(hs.contains(pro)) return false;
22             else hs.add(pro);
23         }
24     }
25     System.out.println(hs);
26     return true;
27 }

 

Colorful Number

标签:style   blog   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/superbo/p/4112136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!