package 蓝桥杯;
public class VO递归_八皇后问题 {
static int[] QueenPos=new int[8];
static int count=0;
public static void main(String[] args) {
NQueue(0);
}
static void NQueue(int k) {
int i;
if(k==8) {
count++;
System.out.println("No. "+count);
for(i=0;i<8;i++) {
for(int l=0;l<8;l++) {
if(l==QueenPos[i])
System.out.print(1+" ");
else
System.out.print(0+" ");
}
System.out.println();
}
return;
}
int j;
for(i=0;i<8;i++) {
for( j=0;j<k;j++) {
if(QueenPos[j]==i||Math.abs(QueenPos[j]-i)==Math.abs(k-j)) {
break;
}
}
if(j==k) {
QueenPos[k]=i;
NQueue(k+1);
}
}
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_表达式求值 {
static int count=0;
static String s=new String();
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
s=reader.next();
System.out.printf("%.2f",expression_value());
reader.close();
}
static double expression_value() {
double result =term_value();
boolean more =true;
while(more) {
char op=s.charAt(count);
if(op=='+'||op=='-') {
count++;
double value=term_value();
if(op=='+')result+=value;
else result-=value;
}
else more =false;
}
return result;
}
static double term_value() {
double result=factor_value();
while(true) {
char op=s.charAt(count);
if(op=='*'||op=='/') {
count++;
double value=factor_value();
if(op=='*')result*=value;
else result/=value;
}
else break;
}
return result;
}
static double factor_value() {
double result=0;
char c=s.charAt(count);
if(c=='(') {
count++;
result=expression_value();
count++;
}
else {
while(Character.isDigit(c)) {
result=10*result+c-'0';
count++;
c=s.charAt(count);
}
}
return result;
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_放苹果 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t=reader.nextInt();
while(t>0) {
int m=reader.nextInt();
int n=reader.nextInt();
System.out.println(f(m,n));
t--;
}
reader.close();
}
static int f(int m,int n) {
if(n>m)
return f(m, m);
if(m==0)
return 1;
if(n==0)
return 0;
return f(m, n-1)+f(m-n, n);
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_逆波兰表达式 {
static String[] k= new String[10];
static int i=-1;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String s=reader.nextLine();
k=s.split(" ");
System.out.printf("%f\n",exp());
reader.close();
}
static double exp() {
i++;
switch (k[i].charAt(0)) {
case '+':
return exp()+exp();
case '-':
return exp()-exp();
case '*':
return exp()*exp();
case '/':
return exp()/exp();
default:
return Double.parseDouble(k[i]);
}
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_爬楼梯 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int n=reader.nextInt();
System.out.println(stairs(n));
}
reader.close();
}
static int stairs(int n) {
if(n<0)
return 0;
if(n==0)
return 1;
return stairs(n-1)+stairs(n-2);
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_数24 {
static double[] a=new double[5];
static double EPS=1e-6;
static boolean isZero(double x) {
return Math.abs(x)<=EPS;
}
static boolean count24(double a[],int n) {
if(n==1) {
if(isZero(a[0]-24))
return true;
else
return false;
}
double[] b=new double[5];
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++){
int m=0;
for(int k=0;k<n;++k)
if(k!=i&&k!=j)
b[m++]=a[k];
b[m]=a[i]+a[j];
if(count24(b, m+1)) {
return true;
}
b[m]=a[i]-a[j];
if(count24(b, m+1)) {
return true;
}
b[m]=a[j]-a[i];
if(count24(b, m+1)) {
return true;
}
b[m]=a[i]*a[j];
if(count24(b, m+1)) {
return true;
}
if(!isZero(a[j])) {
b[m]=a[i]/a[j];
if(count24(b, m+1)) {
return true;
}
}
if(!isZero(a[i])) {
b[m]=a[j]/a[i];
if(count24(b, m+1)) {
return true;
}
}
}
}
return false;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
a[0]=1;
while(a[0]!=0) {
for(int l=0;l<4;l++)
a[l]=reader.nextDouble();
if(a[0]!=0&&a[1]!=0&&a[2]!=0&&a[3]!=0) {
if(count24(a, 4))
System.out.println("YES");
else System.out.println("NO");
}
}
reader.close();
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_数24 {
static double[] a=new double[5];
static double EPS=1e-6;
static boolean isZero(double x) {
return Math.abs(x)<=EPS;
}
static boolean count24(double a[],int n) {
if(n==1) {
if(isZero(a[0]-24))
return true;
else
return false;
}
double[] b=new double[5];
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++){
int m=0;
for(int k=0;k<n;++k)
if(k!=i&&k!=j)
b[m++]=a[k];
b[m]=a[i]+a[j];
if(count24(b, m+1)) {
return true;
}
b[m]=a[i]-a[j];
if(count24(b, m+1)) {
return true;
}
b[m]=a[j]-a[i];
if(count24(b, m+1)) {
return true;
}
b[m]=a[i]*a[j];
if(count24(b, m+1)) {
return true;
}
if(!isZero(a[j])) {
b[m]=a[i]/a[j];
if(count24(b, m+1)) {
return true;
}
}
if(!isZero(a[i])) {
b[m]=a[j]/a[i];
if(count24(b, m+1)) {
return true;
}
}
}
}
return false;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
a[0]=1;
while(a[0]!=0) {
for(int l=0;l<4;l++)
a[l]=reader.nextDouble();
if(a[0]!=0&&a[1]!=0&&a[2]!=0&&a[3]!=0) {
if(count24(a, 4))
System.out.println("YES");
else System.out.println("NO");
}
}
reader.close();
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO递归_N皇后问题 {
static int n;
static int[] QueenPos=new int[100];
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
n=reader.nextInt();
NQueue(0);
reader.close();
}
static void NQueue(int k) {
int i;
if(k==n) {
for(i=0;i<n-1;i++) {
System.out.print(QueenPos[i]+1+" ");
}
System.out.print(QueenPos[n-1]+1);
System.out.println();
return;
}
int j;
for(i=0;i<n;i++) {
for( j=0;j<k;j++) {
if(QueenPos[j]==i||Math.abs(QueenPos[j]-i)==Math.abs(k-j)) {
break;
}
}
if(j==k) {
QueenPos[k]=i;
NQueue(k+1);
}
}
}
}