最近,在就这斯坦福大学的公开课Programming Methodology学习Java。练习了课后的习题之余,自己也编写了一些简单的代码。贴在此处。
001--------------------------------阶乘问题------------------------------------------
/* File name:factorial.java
* Author: Huai Xiaowei
* ---------------------
* This method will help me to better understand what a method is for.
* And how does the messege is delievred in method and function.*/
import acm.program.*;
public class factorial extends ConsoleProgram{
public void run(){
int n=readInt("Enter n:");
for(int i=1;i<=n;i++){
println(i+"! = "+factorial(i));
}
}
//This is a method which calculates the factorial of a number.
public int factorial(int i){
int result=1;
for(int j=1;j<=i;j++){
result *= j;
}
return(result);
}
}
002----------------------------------组合------------------------------------------------
/*File name: Combinations.java
Author: Huai Xiaowei
--------------------------------
Using the factorial method to caculate the combinations*/
import acm.program.*;
public class Combinations extends factorial{
public void run(){
int n = readInt("Enter n:");
int k = readInt("Enter k:");
if(n < k) println("Error!");
else println("C("+n+","+k+")="+(factorial(n)/(factorial(k)*factorial(n-k))));
}
}
003----------------------------------布冯丢针问题求Pi---------------------------------------
/* File: NeedleOfBuffon
* Author: Huai Xiaowei @ Sun Yat-sen University
* There is a famous experiment in the history of probability, which bears
* the name of Buffon's needle.
* You can find a bunch of material about this important trial.
* Here we will simulate the process of throwing a needle on parallel lines.*/
import java.lang.*;
import acm.program.*;
import acm.util.*;
public class NeedleOfBuffon extends ConsoleProgram{
/* GapWidth是两条平行线间的距离
* NeedleLength是针的长度
* These two parameters can be changed to whatever you want, as
* long as NeedLength is less or equal to GapWidth.*/
private static final double PI = 3.141592653589793;
private static final double NeedleLength = 1.0;
private static final double GapWidth = 3.0;
/*To produce x and alpha randomly*/
private RandomGenerator rgen = RandomGenerator.getInstance();
/*x是针的中点到线的距离*/
public void run(){
int nCount = 0;
int nThrow = readInt("Input the times you want to throw this needle:");
for(int i=0;i<nThrow;i++){
double x = rgen.nextDouble(0,GapWidth/2);
double alpha = rgen.nextDouble(0, 180.0);
alpha = alpha*PI/180.0;
if(x <= NeedleLength*Math.sin(alpha)/2.0)nCount++; /*这时针落在线上*/
}
println("Times when needle is on a line:"+nCount);
/* l is the length of the needle,d is the distance between two lines
* p=2*l/(Pi*d) */
double mypi = 2.0*(double)nThrow*NeedleLength/(nCount*GapWidth);
println("PI got by this method has a value of "+mypi);
}
}
004--------------------------------两种方法求最大公约数---------------------------------------
/* File name: gcd.java
* Author: Huai Xiaowei @ Sun Yat-sen University
* gcd is a short for Greatest Common Divisor
* There are two algorithmic methods to get the gcd of two numbers
* One is called the 'brute force' approach
* The other is Euclid's algorithm.
* I have to admit the wisdom of ancient Greeks!*/
import acm.program.*;
import java.lang.*;
public class gcd extends ConsoleProgram{
public void run(){
int n1 = readInt("Enter the 1st number:");
int n2 = readInt("Enter the 2nd number:");
int a = bruteForce(n1,n2);
int b = EuclidAlgorithm(n1,n2);
println(n1+","+n2+" have the greatest common divisor "+a+".(using bruteForce)");
println(n1+","+n2+" have the greatest common divisor "+b+".(using EuclidAlgorithm)");
}
/* The brute force method to get gcd*/
private int bruteForce(int n1, int n2){
int guess = Math.min(n1, n2);
while(true){
if(n1%guess==0 && n2%guess==0) break;
else guess--;
}
return guess;
}
/* The Acient's wisdom*/
private int EuclidAlgorithm(int x, int y){
int r = x % y;
while(r != 0){
x = y;
y = r;
r = x % y;
}
return y;
}
}