(UVA) Quirksome Squares - Solution 2
If you want to see another solution for this problem, click here.
As in the previous solution, I applied Brute Force to solve this problem, and I also used the approach about exact square numbers.
The difference here is that I am transforming all the possible square numbers in a String with N-digits. Then, using these strings, I check whether they are a Quirksome Square.
import java.io.*;
import java.util.*;
class Main {
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while (line != null) {
int numDigits = Integer.parseInt(line);
int div = 1;
for (int i = 0; i < numDigits; i+=2) {
div *= 10;
}
StringBuilder end = new StringBuilder();
for (int i = 0; i < numDigits; i++) {
end.append(9);
}
int size = Integer.parseInt(end.toString());
ArrayList<String> numbers = new ArrayList<String>();
for (int i = 0; i*i <= size; i++) {
int iLenght = Integer.toString(i*i).length();
StringBuilder sb = new StringBuilder();
while (sb.toString().length() < (numDigits-iLenght)) {
sb.append(0);
}
sb.append(i*i);
numbers.add(sb.toString());
}
ArrayList<String> quirksomeSquares = new ArrayList<String>();
for (int i = 0; i < numbers.size(); i++) {
String s1 = numbers.get(i).substring(0,numbers.get(i).length()/2);
String s2 = numbers.get(i).substring(numbers.get(i).length()/2,numbers.get(i).length());
int numI = Integer.parseInt(numbers.get(i));
int sumPow = (Integer.parseInt(s1)+Integer.parseInt(s2))*(Integer.parseInt(s1)+Integer.parseInt(s2));
if (numI == sumPow) {
quirksomeSquares.add(numbers.get(i));
}
}
for (int i = 0; i < quirksomeSquares.size(); i++) {
System.out.println(quirksomeSquares.get(i));
}
line = br.readLine();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
As in the previous solution, I applied Brute Force to solve this problem, and I also used the approach about exact square numbers.
The difference here is that I am transforming all the possible square numbers in a String with N-digits. Then, using these strings, I check whether they are a Quirksome Square.
import java.io.*;
import java.util.*;
class Main {
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while (line != null) {
int numDigits = Integer.parseInt(line);
int div = 1;
for (int i = 0; i < numDigits; i+=2) {
div *= 10;
}
StringBuilder end = new StringBuilder();
for (int i = 0; i < numDigits; i++) {
end.append(9);
}
int size = Integer.parseInt(end.toString());
ArrayList<String> numbers = new ArrayList<String>();
for (int i = 0; i*i <= size; i++) {
int iLenght = Integer.toString(i*i).length();
StringBuilder sb = new StringBuilder();
while (sb.toString().length() < (numDigits-iLenght)) {
sb.append(0);
}
sb.append(i*i);
numbers.add(sb.toString());
}
ArrayList<String> quirksomeSquares = new ArrayList<String>();
for (int i = 0; i < numbers.size(); i++) {
String s1 = numbers.get(i).substring(0,numbers.get(i).length()/2);
String s2 = numbers.get(i).substring(numbers.get(i).length()/2,numbers.get(i).length());
int numI = Integer.parseInt(numbers.get(i));
int sumPow = (Integer.parseInt(s1)+Integer.parseInt(s2))*(Integer.parseInt(s1)+Integer.parseInt(s2));
if (numI == sumPow) {
quirksomeSquares.add(numbers.get(i));
}
}
for (int i = 0; i < quirksomeSquares.size(); i++) {
System.out.println(quirksomeSquares.get(i));
}
line = br.readLine();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment