history = [] while True: next = ''.join(sorted(hex(int(number, 16)-int(number[::-1], 16))[2:])[::-1]) # Generate the next number. try: if next == history[-1]: print(f'CONSTANT, {hex(next)[2:]}') break except IndexError: pass if next in history: print(f'CYCLE, {len(history)-history.index(next)}') break if next == 0: print(f'ZERO, {len(history)}') break number = next history.append(number)
Like
Unknown member
Oct 29, 2019
1a2d3cpackage acsl.KaprekarRoutine;
import java.util.*;
import java.io.*;
public class KaprekarRoutine {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("data/test.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("data/test.out")));
for(int i = 0; i < 10; i++){
String initialValue = in.readLine();
ArrayList<String> previous = new ArrayList<String>();
adc
number = ''.join(sorted(input())[::-1])
history = [] while True: next = ''.join(sorted(hex(int(number, 16)-int(number[::-1], 16))[2:])[::-1]) # Generate the next number. try: if next == history[-1]: print(f'CONSTANT, {hex(next)[2:]}') break except IndexError: pass if next in history: print(f'CYCLE, {len(history)-history.index(next)}') break if next == 0: print(f'ZERO, {len(history)}') break number = next history.append(number)
1a 2d 3c package acsl.KaprekarRoutine;
import java.util.*;
import java.io.*;
public class KaprekarRoutine {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("data/test.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("data/test.out")));
for(int i = 0; i < 10; i++){
String initialValue = in.readLine();
ArrayList<String> previous = new ArrayList<String>();
previous.add(initialValue);
int cycle = 0;
String value = initialValue;
Boolean done = true;
while(done){
String difference = subtract(value);
for(int j = 0; j < previous.size(); j++){
if(difference.equals(previous.get(j))||(difference.equals("0"))){
cycle = previous.size() - j+1;
done = false;
}
}
previous.add(value);
value = difference;
}
if(value.equals("0")){
System.out.println("ZERO, "+(previous.size()-1));
}
else{
if(cycle == 2){
System.out.println("Constant, "+value);
}
else{
if(cycle > 2){
System.out.println("Cycle, "+cycle);
}
}
}
}
in.close();
out.close();
}
public static String subtract(String value){
ArrayList<String> digits = new ArrayList<String>();
String[] temp = value.split("");
for(int i = 0; i < temp.length; i++){
digits.add(temp[i]);
}
String smallest = "";
Collections.sort(digits);
for(int i = 0; i < temp.length; i++){
smallest = smallest + digits.get(i);
}
String largest = "";
Collections.reverse(digits);
for(int i = 0; i < temp.length; i++){
largest = largest + digits.get(i);
}
int difference = Integer.parseInt(largest, 16)-Integer.parseInt(smallest, 16);
return Integer.toString(difference, 16);
}
}
shorts:
1. A
2. D
3. C
from array import array number = list(input("Enter hex number: ")) descending = list(number) descending.sort(reverse = True) ascending = list(number) ascending.sort() descending = "".join(descending) ascending = "".join(ascending) deciAsc = int(ascending, 16) deciDesc = int (descending, 16) sum = hex(deciDesc - deciAsc)[2:] desc1 = list(sum) desc1.sort(reverse = True) asc1 = list(sum) asc1.sort() desc1 = "".join(desc1) asc1 = "".join(asc1) list1 = [] for x in range(9999): list1.append(hex(int(desc1,16)-int(asc1,16))[2:]) if (hex(int(desc1,16)-int(asc1,16))[2:] == 0): print("zero, " + (x+1)) break elif (x > 1 and list1[x] == list1[x-1]): print("constant, " + (hex(int(desc1,16)-int(asc1,16))[2:])) break elif ((int(desc1,16)-int(asc1,16)) in list1): print("cycle, " + (x - list1.index(hex(int(desc1,16)-int(asc1,16))[2:]))) break sum1 = hex(int(desc1,16)-int(asc1,16))[2:] desc1 = list(sum1) desc1.sort(reverse = True) asc1 = list(sum1) asc1.sort() desc1 = "".join(desc1) asc1 = "".join(asc1) print(list1)
kevin
1. A
2. D
12. C
coding:
import java.util.*;
import java.io.*;
public class kaprekarRoutine {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("src/kaprekar"));
for (int q = 0; q < 10; q++) {
String num = in.readLine();
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
map.put(num, -1);
for (int i = 0; i > -1; i++) {
char[] chars = new char[num.length()];
for (int j = 0; j < num.length(); j++) {
chars[j] = num.charAt(j);
}
Arrays.sort(chars);
String big = "", small = "";
for (int j = 0; j < chars.length; j++) {
big = chars[j] + big;
small += chars[j];
}
int bigi = Integer.parseInt(big, 16), smalli = Integer.parseInt(small, 16), diff = bigi - smalli;
if (diff == 0) {
System.out.println("ZERO, " + (i + 1));
break;
}
num = Integer.toHexString(diff);
num = num.toUpperCase();
if (map.containsKey(num)) {
if (i - map.get(num) == 1) System.out.println("CONSTANT, " + num);
else System.out.println("CYCLE, " + (i - map.get(num)));
break;
}
map.put(num, i);
}
}
in.close();
}
}