标签:boolean 源代码 截图 throw ons return eth family 分享
源代码:
package yang1;
import java.math.BigInteger;
import java.util.Scanner;
public class Yang1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("求组合数C(n,k):(n为下标)");//用户输入
System.out.print("请输入n,k:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
if(n<k)
System.out.println("ERROR!");
else if(n==0&&k==0)
System.out.println("1");
else
System.out.print("C("+n+","+k+")="+Calculate(n)/Calculate(k)/Calculate(n-k));
}
public static long Calculate(int n) {
if(n==1)
{
return 1;
}
return n*Calculate(n-1);
}
}
结果截图:
源代码:
package yang2;
import java.math.BigInteger;
import java.util.Scanner;
public class Yang2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("求组合数C(n,k):(n为下标)");//用户输入
System.out.print("请输入n,k:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
if(n<k)
System.out.println("ERROR!");
else if(n==0&&k==0)
System.out.println("1");
else
System.out.print("C("+n+","+k+")="+Calculate(n)/Calculate(k)/Calculate(n-k));
}
public static long Calculate(int n) {
if(n==1)
{
return 1;
}
return n*Calculate(n-1);
}
}
结果截屏:
源代码:
package yang3;
import java.util.Scanner;
public class Yang3 {
public static void main(String[] args){
System.out.println("请输入组合数C(n,k)的两个变量n和k:");
Scanner in1=new Scanner(System.in);
int n = in1.nextInt();
Scanner in2=new Scanner(System.in);
int k = in2.nextInt();
System.out.println("结果为:"+C(n,k));
in1.close();
in2.close();
}
public static int C(int m,int a)
{
if(m<0||a<0||m<a)
return 0;
if(m==a)
return 1;
if(a==1)
return m;
return C(m-1,a)+C(m-1,a-1);
}
}
结果截屏:
程序设计流程图:
源代码:
package hannuota;
import java.util.Scanner;
public class Hannuota {
public static void solveTowers(int disks,int sourcePeg,int destinationPeg,int tempPeg)
{
if(disks == 1)
{
System.out.printf("\n%d --> %d",sourcePeg,destinationPeg);
return;
}
solveTowers(disks-1,sourcePeg,tempPeg,destinationPeg);
System.out.printf("\n%d --> %d",sourcePeg,destinationPeg);
solveTowers(disks-1,tempPeg,destinationPeg,sourcePeg);
}
public static void main(String[] args)
{
System.out.println("请输入盘子数:");
Scanner N = new Scanner(System.in);
int n = N.nextInt();
System.out.print("移动方法为:");
solveTowers(n,1,3,2);
N.close();
}
}
结果截屏:
第三题:
使用递归方法判断,某个字符串是否回文。
设计思想:
定义一个字符串,判断它是否是回文数。地冠以两个指针,一个指向最前端,一个指向最后段,依次判断第一个字符与最后一个字符是否相等,如果都相等,返回ture;若期间有不相等,则直接返回false。
程序流程图:
源代码:
package huiwen;
public class Huiwen {
public static boolean isHuiwen(String s,int i,int j)
{ if(i>j) throw new IllegalArgumentException();
if(i == j) return true;
else{ return (s.charAt(i)==s.charAt(j))&& isHuiwen(s,i+1,j-1); }
}
public static void main(String[] args) {
String test = "ABCBA";
int i=0; int j=test.length()-1;
System.out.println(test + " is Palindrome?" + Huiwen.isHuiwen(test, i, j));
}
}
结果截图:
标签:boolean 源代码 截图 throw ons return eth family 分享
原文地址:http://www.cnblogs.com/zhangfan0801/p/7664014.html