2011年11月22日

梅花座

/*
 * 一班30個學生男女各半
 * 需安排座位成梅花座
 * 
 * 輸出格式範例
 * 請輸入橫排人數 : 5
 * 男95	女94	男86	女82	男84	
 * 女80	男79	女73	男78	女65	
 * 男76	女63	男69	女62	男68	
 * 女51	男63	女47	男56	女41	
 * 男49	女38	男41	女25	男21	
 * 女21	男15	女19	男4	女6
 * 
 * 請輸入橫排人數 : 6
 * 男97	女96	男96	女94	男94	女91	 
 * 女67	男89	女52	男77	女50	男65	 
 * 男58	女39	男46	女37	男41	女26	 
 * 女23	男35	女17	男20	女16	男19	 
 * 男18	女16	男17	女13	男13	女7
 * 
 * 以下程式為亂數產生學生分數 分數範圍為0~100 以及性別0表女 1表男
 * 並且輸出全班成績(未排序) 請接下去寫程式
 */
import java.util.Arrays;
import java.util.Scanner;
public class Scortandplumseat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int student[][] = new int[30][2];//整班學生
		int sex = 0 , score = 1;//學生陣列代號 student[i][sex] student[i][score] 
		int sex_boy = 1 , sex_gril = 0;//性別代號
		int boy_totle = 0 , gril_totle = 0;//男女生人數統計
		
		int boy[][];//男生排序後的陣列
		int gril[][];//女生排序後的陣列
		
		int row_max = 4;//橫排人數
		int title = sex_boy;//排頭是男生還是女生
		
		//產生學生資料 以及假成績
		for(int i = 0; i < student.length ; i++){
			int sex_rand = (int)(Math.random()*2);//亂數性別 0=女 1=男
			int score_rand = (int)(Math.random()*100+1);//亂數分數 0~100
			if(sex_rand == sex_gril){
				if(gril_totle >= 15){//如果女生超過15人則重新產生亂數 
					i--;
					continue;					
				}else
					gril_totle++;//女生人數統計
			}else if(sex_rand == sex_boy){
				if(boy_totle >= 15){//如果男生超過15人則重新產生亂數
					i--;
					continue;
				}else
					boy_totle++;//男生人數統計
			}
			student[i][sex] = sex_rand;//存入該學生性別
			student[i][score] = score_rand;//存入該學生成績
		}
		
		//輸出全班成績
		System.out.println("全班成績 : ");
		System.out.println("編號\t性別\t分數");
		for(int i=0,j=0 ; i < student.length ; i++ ){
			if(student[i][sex] == sex_boy){
				System.out.println(i + "\t男\t" + student[i][score]);
			}else{
				System.out.println(i + "\t女\t" + student[i][score]);
			}
		}
		
	}
}

2011年11月11日

JavaScript 全形數字轉半形數字

function isValue(val){
    var v = val.value;
    if(v == 'NaN' || v == null || v == ''){
        return 0;
    }else{
        //全行轉半型
        result="";
        for(i=0;i < v.length;i++){        
         if( v.charCodeAt(i)== 12288){
                result+=" ";
         }else{
                if(v.charCodeAt(i) > 65280 && v.charCodeAt(i) < 65375){
                 result += String.fromCharCode(v.charCodeAt(i) - 65248);
                }else{
                 result += String.fromCharCode(v.charCodeAt(i));
                }
         }
        }
        val.value = result;
        v=result;
        return v;
    }
} 

2011年8月2日

UVa - p100

p100 問題連結

import java.io.BufferedInputStream;
import java.util.Scanner;

public class u100 {
	int max;
	int min;
	int cont;
	public u100(){
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		String s;
		while(sc.hasNext()){
			int a = sc.nextInt();
			int b = sc.nextInt();
			if(a > b){
				max = a;
				min = b;
			}else{
				max = b;
				min = a;
			}
			int contmax = 0;
			for(; min <= max ; min++){
				cont = 0;
				f(min,max);
				if(contmax < cont)
					contmax = cont;
			}
			System.out.println(a + " " + b + " " + contmax);
		}
	}
	
	public void f(int n , int j){
		//System.out.print(n + " ");
		cont++;
		if(n == 1){
			//stop
		}else{
			if(n%2==1){
				n = 3*n+1;
			}else{
				n = n/2;
			}
			f(n,j);
		}
	}
	
	public static void main(String[] args){
		new u100();
	}
}

2011年2月28日

遞迴練習 河內塔

public class tower {
	
	void tower(int n , String a , String b , String c){
		if(n == 1){
			System.out.println(a + " to " + c);
		}else{
			tower(n-1 , a , c , b);
			tower(1 , a , b , c );
			tower(n-1 , b , a , c);
		}
	}
	
	public static void main(String[] args) {
		tower t = new tower();
		t.tower(3, "B" , "C" , "A");
	}
}

遞迴練習 三角形

public class tri {
	/*
	Enter n = 5
	tri1
	*
	**
	***
	****
	*****
	tri2
	*****
	****
	***
	**
	*
	tri3
	    *
	   **
	  ***
	 ****
	*****
	tri4
	*****
	 ****
	  ***
	   **
	    * 
	*/
	public void prchar(String c , int n){
		for(int i = 0 ; i < n ; i++)
			System.out.print(c);
	}
	
	public void tri1(int n){
		if(n == 1){
			prchar("*",1);
			System.out.println();
		}else{
			tri1(n-1);
			prchar("*",n);
			System.out.println();
		}
	}
	
	public void tri2(int n){
		if(n == 1){
			prchar("*",1);
			System.out.println();
		}else{
			prchar("*",n);
			System.out.println();
			tri2(n-1);
		}
	}
	
	public void tri3(int n , int m){
		if(n == 1){
			prchar(" ",m-1);
			prchar("*",1);
			System.out.println();
		}else{
			tri3(n-1 , m);
			prchar(" ",m-n);
			prchar("*",n);
			System.out.println();
		}
	}
	
	public void tri4(int n , int m){
		if(n == 1){
			prchar(" ",m-1);
			prchar("*",1);
			System.out.println();
		}else{
			prchar(" ",m-n);
			prchar("*",n);
			System.out.println();
			tri4(n-1 , m);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.util.Scanner sc = new java.util.Scanner(System.in);
		int n;
		System.out.print("Enter n = ");
		n = sc.nextInt();
		
		tri t = new tri();

		System.out.println("tri1");
		t.tri1(n);
		System.out.println("tri2");
		t.tri2(n);
		System.out.println("tri3");
		t.tri3(n, n);
		System.out.println("tri4");
		t.tri4(n, n);
		
	}
}