Checking Cube (Thuật toán Quay lui BackTracking)
package Advance;
import java.io.FileInputStream;
import java.util.Scanner;
public class CheckingCube {
public static void main(String[] args) throws Exception {
new CheckingCube().inIt();
}
int D;
int Result;
private void inIt() throws Exception {
Scanner sc = new Scanner(System.in);
int T;
T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
D = sc.nextInt();
Result = 0;
backtrack(0, 50, 0);
System.out.println("#" + test_case + " " + Result);
}
}
private void backtrack(int total, int num, int c) {
if (c > 5) {
return;
}
if (total > D) {
return;
}
if (total == D) {
Result++;
return;
}
for (int i = num; i >= 0; i--) {
backtrack(total + (i * i * i), i, c + 1);
}
}
}
import java.io.FileInputStream;
import java.util.Scanner;
public class CheckingCube {
public static void main(String[] args) throws Exception {
new CheckingCube().inIt();
}
int D;
int Result;
private void inIt() throws Exception {
Scanner sc = new Scanner(System.in);
int T;
T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
D = sc.nextInt();
Result = 0;
backtrack(0, 50, 0);
System.out.println("#" + test_case + " " + Result);
}
}
private void backtrack(int total, int num, int c) {
if (c > 5) {
return;
}
if (total > D) {
return;
}
if (total == D) {
Result++;
return;
}
for (int i = num; i >= 0; i--) {
backtrack(total + (i * i * i), i, c + 1);
}
}
}
Nhận xét
Đăng nhận xét