Questions? Email sfo.lax6776@gmail.com

Join our weChat group or follow us on Facebook

©2019 by Legion of Learners

sfolax6776
Oct 19

Senior2019-Set 1 - Recursive Functions and CNS

5 comments
awesomeface259
Oct 26

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();

}

}

colinzhao777
Oct 27

1. A

2. D

3. C

 

#include <bits/stdc++.h>

using namespace std;

#define pb push_back

#define ii pair<int, int>

#define vi vector<int>

#define vii vector<ii>

#define vs vector<string>

#define ll long long

#define ull usigned ll

 

int stop(vs used, string on) {

if (on == "0") return 0;

if (used.size() > 0 && used[used.size() - 1] == on) return 1;

if (find(used.begin(), used.end(), on) != used.end()) return 2;

return 3;

}

 

int cycleLength(vs used, string on) {

for (int i = 0; i < (int)used.size(); i++) {

if (on == used[i]) return used.size() - 1 - i;

}

return 0;

}

 

int main() {

for (int z = 0; z < 10; z++) {

string in;

cin >> in;

vs used;

string on = in;

int s = stop(used,on);

while (s == 3) {

vector<char> on2;

for (int i = 0; i < (int)on.length(); i++) {

on2.pb(on[i]);

}

sort(on2.begin(), on2.end(), greater<char>());

string big(on2.begin(), on2.end());

sort(on2.begin(), on2.end());

string small(on2.begin(),on2.end());

int big2, small2;

stringstream s1;

s1 << big;

s1 >> std::hex >> big2;

stringstream s2;

s2 << small;

s2 >> std::hex >> small2;

stringstream s3;

s3 << std::hex << (big2 - small2);

on = s3.str();

transform(on.begin(), on.end(), on.begin(), ::toupper);

s = stop(used, on);

used.pb(on);

}

if (s == 0) cout << "ZERO, " << used.size() << endl;

else if (s == 1) cout << "CONSTANT, " << on << endl;

else cout << "CYCLE, " << cycleLength(used, on) << endl;

}

}

 

Steven
Oct 27

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)

kchen4000
Oct 29

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);

}

}

chris13888
6 days ago

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)

New Posts