程序设计:求组合数,汉诺塔问题,判断回文字符串

 一、求组合数

设计思想:

方法一:根据组合数公式,再设计一个阶乘方法,进行程序设计求解。

流程图

程序源码:

import java.util.Scanner;

public class YangHuiSanJiao
{

  public static int jiecheng(int n)
  {

    int sum=1;
    if(n==0)
      return (n+1);
    else
     {
      for(int i=n;i>0;i--)
      sum*=i;
      return sum;
     }
  }
  public static void zuheshu()
  {

    Scanner in=new Scanner(System.in);

    int bottom,top;
    System.out.print("请输入组合数C n^k中n的值:");
    bottom=in.nextInt();
    if(bottom>=0)
    {
      if(bottom==0)
        System.out.println("组合数C 0^0的值为:"+(bottom+1));
      else
        {
          System.out.print("请输入组合数C n^k中k的值:");
          top=in.nextInt();
          if(bottom>=top)
            System.out.print("组合数C "+bottom+"^"+top+"中的值为:"
              +(jiecheng(bottom))/jiecheng(top)/jiecheng(bottom-top));
          else
            System.exit(0);
         }
      }

   }
  public static void main(String[] args)
  {
  zuheshu();

  }

}

程序截图:

方法二:

设计思路:

设计杨辉三角程序,杨辉三角的数即为对应的组合数的值,a[i][j]=a[i-1][j-1]+a[i-1][j]的值,最后利用递推,求出结果。

流程图:

 

程序源码:

import java.util.Scanner;

public class YangHuiSanJiao
{

  public static void zuheshu()
  {
    Scanner in=new Scanner(System.in);
    System.out.println("请输入组合数C n^k的n和k:");
    int x,k;
    x=in.nextInt()+1;
    k=in.nextInt();
    int num[][] = new int[x][x];//这个数组有几层
    for(int m=0;m<x;m++)//主要是对数组进行赋值
      {
        for(int n=0;n<=m;n++)//每一层的个数都是小于等于层数的,m代表层数,n代表着第几个数
          {
            if(n==0||m==n)//每一层的开头都是1,m==n的时候也是1,必须要这个,凡事都得有个开头
              {
                num[m][n]=1;
              }
            else
              num[m][n]=num[m-1][n-1]+num[m-1][n];//这个就是递推的方法
          }
      }
      System.out.println("组合数C "+(x-1)+"^"+k+"的值是"+num[x-1][k]);
    }
  public static void main(String[] args)
  {
    zuheshu();
  }

}

程序截图:

二、汉诺塔问题

问题描述:

如下图所示,从左到右有A、B、C三根柱子,其中A柱子上面有从小叠到大的n个圆盘,现要求将A柱子上的圆盘移到C柱子上去,期间只有一个原则:一次只能移到一个盘子且大盘子不能在小盘子上面,求移动的步骤和移动的次数

设计思路:

 利用递归算法,  把n-1个盘子由A 移到 B; 把第n个盘子由 A移到 C;  把n-1个盘子由B 移到 C;

 流程图:

 

程序源码:

import java.util.Scanner;

public class HanoiTower
{
  static int m=0;
  public static void print(int n,char M,char N)
  {
    System.out.println("第"+(++m)+"次移动:把"+n+"号圆盘从"+M+"移动到"+N);
  }
  public static void move(int n,char A,char B,char C)
  {
    if(n==1)
      print(1,A,C);
    else
    {
      move(n-1,A,C,B);
      print(n,A,C);
      move(n-1,B,A,C);
    }
   }
  public static void main(String[] args)
  {
    Scanner in=new Scanner(System.in);
    char A='A';
    char B='B';
    char C='C';
    System.out.print("请输入盘子的数目:");
    int n=in.nextInt();
    move(n,A,B,C);
    System.out.println(" 一共移动了"+m+"次,吧A上的圆盘全部移动到了C上");
    in.close();
  }

}

 程序截图:

三、判断混问字符串

设计思路:

输入一个字符串str,调用String包内的charAt()函数,定义int cout=0;用for循环:如果str。charAt(i)==str.charAt(str.length()-1j-i),cout++,最后如果count==str。length(),输出结果是回文字符串,否则不是会为每年字符串。

流程图:

 

 

程序源码:

import java.util.Scanner;

public class Palindrome
{
  public static int PanDuan(String str)
   {
    int count=0;
    for(int i=0;i<str.length();i++)
      {
        if(str.charAt(i)==str.charAt(str.length()-1-i))
        count++;
      }
    if(count==str.length())
      return 1;
    else
      return 0;
    }
  public static void main(String[] args)
  {
    System.out.println("请输入一个字符串:");
    Scanner in=new Scanner(System.in);
    String str=in.next();
    PanDuan(str);
    if(PanDuan(str)==1)
      System.out.print("是回文字符串");
    else
      System.out.print("不是回文字符串");
  }

}

 程序截图:

 

原文地址:https://www.cnblogs.com/2016-zck/p/7658968.html