Archive for the ‘100(下)程式設計(二)’ Category

0608Prog

基本Java程式參數及檔案讀寫

基本Java程式參數及檔案讀寫

輸入文字的切割,轉換及格式化輸出

輸入文字的切割,轉換及格式化輸出

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
public class TestClassA {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String filename = args[0];
		FileReader fileReader;
		BufferedReader bufferedReader;
		try
		{
			fileReader = new FileReader(filename);
			bufferedReader = new BufferedReader(fileReader);
			String line = bufferedReader.readLine();
			while (line!=null && line.length()> 0)
			{
				//System.out.println(line);
				String[] fields = line.split(",");
				String  name = fields[0].trim();
				int unit = Integer.parseInt(fields[1].trim());
				int hour = Integer.parseInt(fields[2].trim());
				int total = unit * hour;
				System.out.printf("%10s, %4dx%3d=%4d", name, unit, hour, total);
				System.out.println();
				line = bufferedReader.readLine();
			}
 
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
 
 
	}
 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

8
六月

程式設計二程式 06/01: Java程式Ex5

   Posted by: admin Tags: , , ,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.*;
import java.util.*;
 
public class Ex5Class {
	static Random random = new Random();
	public static void main(String[] args) {
		String filename1, filename2;
		filename1 = args[0];
		filename2 = args[1];
 
 
		FileReader fileReader;
		BufferedReader bufferedReader;
		FileWriter fileWriter;
		PrintWriter printWriter;
		try
		{
			fileReader = new FileReader(filename1);
			bufferedReader = new BufferedReader(fileReader);
			fileWriter = new FileWriter(filename2);
			printWriter = new PrintWriter(fileWriter);
 
			int[] bingos = NumGen();
			System.out.printf("bingo numbers: %2d, %2d, %2d, %2d, %2d, %2d", 
					bingos[0],bingos[1],bingos[2],bingos[3],bingos[4],bingos[5]);
			printWriter.printf("bingo numbers: %2d, %2d, %2d, %2d, %2d, %2d", 
					bingos[0],bingos[1],bingos[2],bingos[3],bingos[4],bingos[5]);
			System.out.println();
			printWriter.println();
			Arrays.sort(bingos);
 
			String line = bufferedReader.readLine();
			int[] stakes = new int[6];
			while (line !=null && line.length() >0)
			{
				String[] numStr = line.split(",");
				stakes[0] = Integer.parseInt(numStr[0].trim());
				stakes[1] = Integer.parseInt(numStr[1].trim());
				stakes[2] = Integer.parseInt(numStr[2].trim());
				stakes[3] = Integer.parseInt(numStr[3].trim());
				stakes[4] = Integer.parseInt(numStr[4].trim());
				stakes[5] = Integer.parseInt(numStr[5].trim());
				Arrays.sort(stakes);
				int matchNumber = matchSorted(bingos, stakes);
				//System.out.println(matchNumber);
				System.out.printf("[%d] %3d %3d %3d %3d %3d %3d", matchNumber, 
						stakes[0], stakes[1], stakes[2], stakes[3], stakes[4], stakes[5]);
				printWriter.printf("[%d] %3d %3d %3d %3d %3d %3d", matchNumber, 
						stakes[0], stakes[1], stakes[2], stakes[3], stakes[4], stakes[5]);
				System.out.println();
				printWriter.println();
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
			printWriter.close();
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
 
	}
	static int match(int[] aa, int[] bb)
	{
		int matchNums = 0;
		for (int i=0; i < 6; i++)
		{
			for (int j=0; j <6; j++)
			{
				if (aa[i]==bb[j])
					matchNums++;
			}
		}
		return matchNums;
	}
	static int matchSorted(int[] aa, int[] bb)
	{
		int matchNums = 0;
		for (int i=0, j=0; i < 6 && j < 6;)
		{
			if (aa[i]==bb[j])
			{
				matchNums++; i++; j++;
			}
			else if (aa[i] >= bb[j]) j++;
			else i++;
		}
		return matchNums;
	}
	static int[] NumGen()
	{
		int[] numList = new int[6];
		int[] ball = new int[49];
		for (int j=0; j <49; j++)
		{
			ball[j] = random.nextInt(100000);
		}
		for (int i=0; i< 6; i++)
		{
			int max= -1;
			int select = -1;
			for (int j=0; j <49; j++)
			{
				if (ball[j] > max)
				{
					max = ball[j];
					select = j;
				}
			}
			numList[i] = select + 1;
			ball[select] = -1;
		}
		return numList;
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

1
六月

程式設計二程式 06/01: Java程式Ex5

   Posted by: admin Tags: , , ,

import java.io.*;
import java.util.*;
public class Ex5Class {
	static Random random = new Random();
	public static void main(String[] args) {
		String filename1, filename2;
		filename1 = args[0];
		filename2 = args[1];
 
		//FileReader fileReader;
		//BufferedReader bufferedReader;
		//FileWriter fileWriter;
		//PrintWriter printWriter;
		try
		{
			fileReader = new FileReader(filename1);
			bufferedReader = new BufferedReader(fileReader);
			fileWriter = new FileWriter(filename2);
			printWriter = new PrintWriter(fileWriter);
 
			int[] bingos = NumGen();
			System.out.printf("bingo numbers: %2d, %2d, %2d, %2d, %2d, %2d",
					bingos[0],bingos[1],bingos[2],bingos[3],bingos[4],bingos[5]);
			printWriter.printf("bingo numbers: %2d, %2d, %2d, %2d, %2d, %2d",
					bingos[0],bingos[1],bingos[2],bingos[3],bingos[4],bingos[5]);
			System.out.println();
			printWriter.println();
			//Arrays.sort(bingos);
 
			String line = bufferedReader.readLine();
			int[] stakes = new int[6];
			while (line !=null &amp;&amp; line.length() &gt;0)
			{
				String[] numStr = line.split(",");
				stakes[0] = Integer.parseInt(numStr[0].trim());
				stakes[1] = Integer.parseInt(numStr[1].trim());
				stakes[2] = Integer.parseInt(numStr[2].trim());
				stakes[3] = Integer.parseInt(numStr[3].trim());
				stakes[4] = Integer.parseInt(numStr[4].trim());
				stakes[5] = Integer.parseInt(numStr[5].trim());
				//Arrays.sort(stakes);
				int matchNumber = matchSorted(bingos, stakes);
				//System.out.println(matchNumber);
				System.out.printf("[%d] %3d %3d %3d %3d %3d %3d", matchNumber,
						stakes[0], stakes[1], stakes[2], stakes[3], stakes[4], stakes[5]);
				printWriter.printf("[%d] %3d %3d %3d %3d %3d %3d", matchNumber,
						stakes[0], stakes[1], stakes[2], stakes[3], stakes[4], stakes[5]);
				System.out.println();
				printWriter.println();
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
			printWriter.close();
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
 
	}
	static int match(int[] aa, int[] bb)
	{
		int matchNums = 0;
		for (int i=0; i &lt; 6; i++)
		{
			for (int j=0; j &lt;6; j++)
			{
				if (aa[i]==bb[j])
					matchNums++;
			}
		}
		//return matchNums;
	}
	static int matchSorted(int[] aa, int[] bb)
	{
		int matchNums = 0;
		for (int i=0, j=0; i &lt; 6 &amp;&amp; j &lt; 6;)
		{
			if (aa[i]==bb[j])
			{
				matchNums++; i++; j++;
			}
			else if (aa[i] &gt;= bb[j]) j++;
			else i++;
		}
		//return matchNums;
	}
	static int[] NumGen()
	{
		int[] numList = new int[6];
		int[] ball = new int[49];
		for (int j=0; j &lt;49; j++)
		{
			ball[j] = random.nextInt(100000);
		}
		for (int i=0; i&lt; 6; i++)
		{
			int max= -1;
			int select = -1;
			for (int j=0; j &lt;49; j++)
			{
				if (ball[j] &gt; max)
				{
					max = ball[j];
					select = j;
				}
			}
			numList[i] = select + 1;
			ball[select] = -1;
		}
		//return numList;
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

0601Prog

Java程式的資料比對演算法設計

Java程式的資料比對演算法設計

資料排序及不排序比對的複雜度

資料排序及不排序比對的複雜度

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark