Created
September 12, 2015 17:44
-
-
Save ejcer/0cf22f4a9349f97845c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.StringTokenizer; | |
import Utilities.IO_Template.MyScanner; | |
public class testing { | |
public static void main(String[] args) { | |
MyScanner in = new MyScanner(); | |
int n = in.nextInt(); | |
numSquares(n); | |
} | |
public static int numSquares(int n) { | |
int[] dp = new int[n + 1]; | |
Arrays.fill(dp, Integer.MAX_VALUE); | |
dp[0] = 0; | |
for(int i = 0; i <= n; i++){ | |
for(int j = 1; i + j * j <= n; j++){ | |
dp[i + j * j] = Math.min(dp[i + j * j], dp[i] + 1); | |
System.out.println("i ="+i+", j^2="+j*j + ", i+j*j="+(i+j*j) +", the minimum: "+Math.min(dp[i + j * j], dp[i] + 1)); | |
for(int z = 0; z < dp.length; z++){ | |
System.out.print(dp[z]+","); | |
} | |
System.out.println(""); | |
} | |
} | |
System.out.println(dp[n]); | |
return dp[n]; | |
} | |
// -----------MyScanner class for faster input---------- | |
public static class MyScanner { | |
BufferedReader br; | |
StringTokenizer st; | |
public MyScanner() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
long nextLong() { | |
return Long.parseLong(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
String nextLine() { | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment