// Topcoder SRM 466 DIV 2, L2: LotteryCheating
import java.util.*;
class TopcoderSolution {
public static void main(String[] args) {
LotteryCheating obj = new LotteryCheating();
System.out.println(
obj.minimalChange("7654321")
);
}
}
// change to public before submit
public class LotteryCheating {
List<String> winnums;
public int minimalChange(String ID) {
int len = ID.length();
winnums = new ArrayList<String>();
for (long i = 0; i <= 100000; i++) {
long sqr = i * i;
String sn = String.valueOf(sqr);
if (sn.length() > len)
break;
String s = "0000000000" + sn;
s = s.substring(s.length() - len);
winnums.add(s);
}
int min = Integer.MAX_VALUE;
for (String s : winnums) {
min = Math.min(min, CountDiff(s, ID));
}
return min;
}
private int CountDiff(String s, String id) {
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != id.charAt(i))
cnt++;
}
return cnt;
}
}
No comments:
Post a Comment