1,杨辉三角
package demo5;
public class Yhsj {
public static void main(String[] args) {
// TODO Auto-generated method stub
int triangle[][]=new int [8][];
for(int i=0;i<triangle.length;i++)
{
triangle[i]=new int [i+1];
for(int j=0;j<=i;j++)
{
if(i==0||j==0||j==i)
{
triangle[i][j]=1;
}
else
{
triangle[i][j]=triangle[i-1][j-1]+triangle[i-1][j];
}
System.out.print(triangle[i][j]+" ");
}
System.out.println();
}
}
}
2,汉诺塔
package demo1;
//朱文宏 20153286 2016.10.15
public class TowersOfHanoi
{
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 );//用tempeg做过渡,把disk-1个盘子移到destinationpeg上
System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );//从起始柱子上移动最后一个盘子到目标柱子上
//用a做过度,将t上的n-1个盘子移到c上;
solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
}
public static void main( String[] args )
{
int startPeg = 1;
int endPeg = 3;
int tempPeg = 2;
int totalDisks = 3;
solveTowers( totalDisks, startPeg, endPeg, tempPeg );
}
}
3,回文数
package demo5;
import java.io.*;
public class Hws {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.out.println("输入一段话或数字:");
String str;
BufferedReader r=new BufferedReader(new InputStreamReader(System.in),256);
str=r.readLine();
System.out.println(huiwen(str,0,str.length()-1));
}
public static boolean huiwen(String s,int start,int end)
{
if(start>=end) return true;
if(s.charAt(start)==s.charAt(end))
return huiwen(s,start+1,end-1);
else
return false;
}}