Problem Statement for TheLuckySum
来源:互联网 发布:java divide用法 编辑:程序博客网 时间:2024/05/21 09:38
Problem Statement for TheLuckySum Problem Statement John thinks 4 and 7 are lucky digits, and all other digits are not lucky. A lucky number is a number that contains only lucky digits in decimal notation. Some numbers can be represented as a sum of only lucky numbers. Given an int n, return a int[] whose elements sum to exactly n. Each element of the int[] must be a lucky number. If there are multiple solutions, only consider those that contain the minimum possible number of elements, and return the one among those that comes earliest lexicographically. A int[] a1 comes before a int[] a2 lexicographically if a1 contains a smaller number at the first position where they differ. If n cannot be represented as a sum of lucky numbers, return an empty int[] instead. Constraints - n will be between 1 and 1,000,000,000, inclusive. Examples
0) 11 Returns: {4, 7 } It is simple: 11 = 4 + 7.
1) 12 Returns: {4, 4, 4 } Now we need three summands to get 12.
2) 13 Returns: { } And now we can not get 13 at all.
3) 100 Returns: {4, 4, 4, 44, 44 }
Solution
I got a idea,every lucky num is consisted of several 4s or/and several 7s,so i think may be we can resolve it by this. Below is the my code,
getSingleLuckArray() is get lucky int [] with one lucky digit.
getLuckArray is get lucky int [] with two lucky digits, and it will invoke getSingleLuckArray(),
maybe it will get lots of int[] arrays, but we just get the array with fewest nums.
import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;public class LuckySum { /** * @param args */ public static void main(String[] args) { int [] result = getLuckArray(2800); System.out.print('{'); if(result != null) { for (int i = 0; i < result.length; i++) { System.out.print(result[i]); if(i!= result.length-1) { System.out.print(','); } } } System.out.print('}'); } public static int[] getLuckArray(int sum) { int [] retVal = null; int countOf4s = sum/4, countOf7s = sum/7; ArrayListresult = null, temp = null; for (int i = countOf7s; i >=0; i--) { for(int j=0; j<=countOf4s; j++) { if((i*7 + j*4) == sum) { temp = new ArrayList (); temp.addAll(getSingleLuckArray(i*7, 7)); temp.addAll(getSingleLuckArray(j*4, 4)); if(result == null) { result = temp; } else { if(result.size() > temp.size()) { result = temp; } } } } } if(result != null) { retVal = new int[result.size()]; for (int i = 0; i < retVal.length; i++) { retVal[i] = result.get(i); } Arrays.sort(retVal); } return retVal; } public static ArrayList getSingleLuckArray(int sum, int luckDiginal) { int countOfLuckNum = sum / luckDiginal; int bitCount = Integer.toString(countOfLuckNum).length(); ArrayList al = new ArrayList (); int bitValue = 0; for(int i=bitCount; i>0; i--) { bitValue = getLuckNums(i, luckDiginal); while(true) { if(bitValue <= sum) { al.add(bitValue); sum = sum - bitValue; } else { break; } } } return al; } public static int getLuckNums(int bitCount, int luckDiginal) { int luckNums = 0; StringBuffer sb = new StringBuffer(); for(int i=bitCount; i>0; i--) { sb.append("1"); } luckNums = Integer.parseInt(sb.toString()) * luckDiginal; return luckNums; }}
It will print out "{4,7,7,7,444,777,777,777}"
- Problem Statement for TheLuckySum
- Problem Statement for TheLuckySum (自己写的二叉树实现的4,7幸运数字算法。记录下来)
- Python[二]:For statement
- about the for statement
- JavaScript For...In Statement
- The Enhanced 'for' Statement
- for/in statement
- URAL 1881. Long problem statement
- for循环的init-statement
- Understanding Python's "for" statement
- the for statement in python
- C++11 Range for Statement
- Traveling Salesman Problem-Statement of work
- URAL 1881Long problem statement 模拟
- Explain plan for last executed statement
- too large for try statement(解决方案)
- Performance tuning for Data Selection Statement
- Performance tuning for Data Selection Statement
- Silverlight 2 Beta 2 中的 ClientAccessPolicy.xml 文件
- JNDI概述
- Web开发中慎用直接在递交请求的action里直接设置中文参数
- 为了jQuery方便使用,项目用上utf-8
- 汉字编码及区位码查询算法
- Problem Statement for TheLuckySum
- 用串口操作手机收发短信总结
- 最佳商机来临,龙芯抓得住吗?
- udp的字符传输
- 项目管理中的需求变更控制分析
- 网球基本技巧
- Saawariya(爱人)
- 一部电影的片尾曲
- 如何做好软件需求捕获