/*输入一串打乱的喊7次数,输出准确的顺序
* 例如:输入00021 代表五个人,喊了三次过(逢7过)
* 遇到的就是7,14,17,
* 1 2 3 4 5
* 6 7 8 9 10
* 11 12 13 14 15
* 16 17
* 分别出现在2号两次和4号一次,
* 准确的顺序是02001
* */
public class SevenPass {
public static void main(String[] args) {
String str = "00021";
System.out.println(rightSort(str));
}
static String rightSort(String str){
int num = str.length();
int count = sum(str);
System.out.println("count==="+count+";num==="+num);
int[] ins = new int[num];
int c= 0;
// 遇到count次7,按照最大遇到的数字count*7遍历
for(int i=1;i<=count*7;i++){
// 遍历过程统计遇7的次数,等于count时停止遍历
if(c==count){
break;
}
// 7的倍数或含7的数字,计算一次
if(i%7==0||contains7(i)){
ins[i%num-1] +=1;
c++;
}
}
StringBuilder sb = new StringBuilder();
for(int n:ins){
sb.append(n);
}
return sb.toString();
}
static boolean contains7(int i) {
return Integer.toString(i).contains("7");
}
static int sum(String str) {
int count = 0;
for(int i =0;i<str.length();i++){
count += Integer.parseInt(String.valueOf(str.charAt(i)));
}
return count;
}
}
有没有其他算法,欢迎留言~