Colorful Number

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 }
时间: 2024-12-07 02:47:15

Colorful Number的相关文章

Cipe Coding Summary Part1

1. Colorful Number:A numbercan be broken into different sub-sequence parts. Suppose a number 3245 can bebroken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorfulnumber, since product of every digit of a sub-sequence are different

CodeForces 505B Mr. Kitayuta&#39;s Colorful Graph

Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 505B Description Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The

codeforces 505B Mr. Kitayuta&#39;s Colorful Graph(水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge

B. Mr. Kitayuta&#39;s Colorful Graph (CF #286 (Div. 2) 并查集)

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the g

CF 505B Mr. Kitayuta&#39;s Colorful Graph(最短路)

题意  求两点之间有多少不同颜色的路径 范围比较小  可以直接floyd #include<cstdio> #include<cstring> using namespace std; const int N = 105; int d[N][N][N], ans; int main() { int a, b, c, n, m, q; while(~scanf("%d%d", &n, &m)) { memset(d, 0, sizeof(d));

Codeforces Round #286 (Div. 2) B. Mr. Kitayuta&#39;s Colorful Graph +foyd算法的应用

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the g

Colorful Tree

Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3373    Accepted Submission(s): 1461 Problem Description There is a tree with n nodes, each of which has a type of color represent

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q