P2 - 2021/22 - DN06 - Starc Aljaz¶
class DN06 {
public static void main (String[] args) {
String str = "";
for (int i=0;i<args[0].length();i++) str += "a";
while (true) {
if (bsdChecksum(str) == bsdChecksum(args[0])) {
System.out.println(str);
break;
} else {
str = flip(str);
}
}
}
static String flip (String niz) {
int index = niz.length() - 1;
while (index + 1 >= 0) {
int cAscii = (int) niz.charAt(index);
if (cAscii == 122) {
niz = niz.substring(0, index) + "a" + niz.substring(index + 1);
index -= 1;
} else {
int nAscii = cAscii + 1;
return niz.substring(0, index) + ((char) nAscii) + niz.substring(index + 1);
}
}
return niz;
}
static int bsdChecksum (String niz) {
// https://en.wikipedia.org/wiki/BSD_checksum
int checksum = 0;
for (int i = 0; i<niz.length(); i++) {
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += niz.charAt(i);
checksum &= 0xffff;
}
return checksum;
}
}
Zadnja posodobitev:
April 5, 2022