京东笔试 幸运数 还有更好的解法吗?但提交通过0%,哪里有错
import java.io.*; import java.util.*; class Test { } public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int tt = cin.nextInt(); int j = 0; int k; for(j = 1;j<=tt;j++) { k = cin.nextInt(); if(k <= 0){ } //简单处理特殊情况 if(k == 1){ System.out.println(4); }else if(k == 2){ System.out.println(7); }else{ int t = k+1;//把k+1,然后直接转换成二进制 char[] chars = Integer.toBinaryString(t).toCharArray(); char[] str = new char[chars.length-1];//由于输出是大数,使用字符数组存储 int i; //算法核心,除了第一个外,0对应4,1对应7. for(i=1;i<chars.length;i++){ if(chars[i] == '0'){ str[i-1] = '4'; }else{ str[i-1] = '7'; } } //输出 for(i=0;i<str.length;i++){ System.out.print(str[i]); } System.out.println(); } } } } /* 本地输入 3 5 100 1000000000 输出: 74 744747 77477744774747744747444444447 */
#京东#