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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | import java.lang.reflect.Array; import java.util.*; class Rectangle { int x, y; double height, width; double getArea() { return height * width; } void print() { System.out.println("以(橫座標 ,縱坐標)=(" + x + "," + y + ")為起點"); System.out.println("以高為:" + height + "寬為:" + width + "的矩形"); System.out.println("矩形面積為:" + getArea()); } } class Rectangle1 { int x, y; double height, width; double getArea(double height, double width) { return height * width; } void print(int x, int y, double height, double width) { System.out.println("以(橫座標 ,縱坐標)=(" + x + "," + y + ")為起點"); System.out.println("以高為:" + height + "寬為:" + width + "的矩形"); System.out.println("矩形面積為:" + getArea(height, width)); } } class Rectangle2 { int x, y; double height, width; Rectangle2(int i, int j, double h, double w) { x = i; y = j; height = h; width = w; } double getArea() { return height * width; } void print() { System.out.println("以(橫座標 ,縱坐標)=(" + x + "," + y + ")為起點"); System.out.println("以高為:" + height + "寬為:" + width + "的矩形"); System.out.println("矩形面積為:" + getArea()); } } public class test20131029 { public static void main(String[] a) { Rectangle r1 = new Rectangle(); r1.x = 0; r1.y = 5; r1.height = 25.5; r1.width = 35.5; r1.print(); Rectangle1 r2 = new Rectangle1(); r2.print(1, 10, 20.5, 30.5); Rectangle2 r3 = new Rectangle2(2, 3, 11.5, 22.5); r3.print(); int[] arri = { 1, 5, 9, 2, 6, 7, 3, 4, 8 }; int[] arri2 = arri; Arrays.sort(arri); arri2[0] = 0; System.out.println("排列並且替換完的矩陣:"); for (int i : arri) { System.out.print(i); } int[] arri3 = new int[15]; int odd = 0, even = 0; int[] arreven = new int[15]; int[] arrodd = new int[15]; for (int i = 0; i < 15; i++) { arri3[i] = (int) (Math.random() * 10) + 1; } System.out.println("亂數產生的矩陣:"); for (int i : arri3) { System.out.print(i + "\t"); } System.out.println(); for (int i = 0; i < 15; i++) { if (arri3[i] % 2 == 0) { arreven[even] = arri3[i]; even++; } else { arrodd[odd] = arri3[i]; odd++; } } int count1 = 0; int count2 = 0; int count3 = 0; System.out.println("奇偶交叉排列:"); for (int i = 0; i < 15; i++) { if (i == 0 || count1 < even) { System.out.print(arreven[i] + "\t"); count1++; count3++; if (count3 % 5 == 0) { System.out.println(); } } if (count2 < odd) { System.out.print(arrodd[i] + "\t"); count2++; count3++; if (count3 % 5 == 0) { System.out.println(); } } } Scanner in = new Scanner(System.in); System.out .println("請選擇執行項目(1)費氏數列(2)最大公因數(3)最小公倍數(4)次方(5)階層(6)排列(7)和(8)交叉乘積(9)矩陣和"); int choose = in.nextInt(); if (choose < 9 && choose > 0) { System.out.println("請輸入第一個數:"); int enter = in.nextInt(); System.out.println("請輸入第二個數:"); int enter1 = in.nextInt(); if (choose == 1) { print(fidon(enter)); System.out.println(); } else if (choose == 2) { print(gcd(enter, enter1)); System.out.println(); } else if (choose == 3) { print(lcm(enter, enter1)); System.out.println(); } else if (choose == 4) { print(pow(enter, enter1)); System.out.println(); } else if (choose == 5) { print(step(enter)); System.out.println(); } else if (choose == 6) { print(select(enter, enter1)); System.out.println(); } else if (choose == 7) { print(sum(enter)); System.out.println(); } else if (choose == 8) { print(CrossM(enter)); } System.out.println(); } if (choose == 9) { print(sum(arri, arri.length)); } } static void print(int i) { System.out.print(i); } public static int fidon(int i) { // 費氏 if (i < 0) { return -1; } else if (i == 0) { return 0; } else if (i == 1) { return 1; } else return fidon(i - 1) + fidon(i - 2); } public static int gcd(int i, int j) { // 最大公因數 int sum = 0; if (i < 0 || j < 0) { return -1; } else if (i >= j) { sum = i % j; if (sum == 0) { return j; } else { i = j; j = sum; return gcd(i, j); } } else { sum = j % i; if (sum == 0) { return i; } else { j = i; i = sum; return gcd(j, i); } } } public static int lcm(int i, int j) { // 最小公倍數 return (i / gcd(i, j) * j); } public static int pow(int i, int j) { // 次方 if (j < 0) { return -1; } else if (j == 0) { return 1; } else { return pow(i, j - 1) * i; } } public static int step(int i) { // 階層 if (i < 0) { return -1; } else if (i == 0) { return 0; } else if (i == 1) { return 1; } else { return step(i - 1) * i; } } public static int select(int i, int j) { // C幾取幾 if (i <= 0 || j <= 0 || i < j) { return -1; } else if (i == j) { return 1; } else { return (step(i) / (step(j) * step(i - j))); } } public static int sum(int i) { // 合 if (i < 0) { return -1; } else if (i == 0) { return 0; } else if (i == 1) { return 1; } else { return i + sum(i - 1); } } public static int CrossM(int i) { // 交叉乘積 int sum = 0; if (i < 0) { return -1; } else if (i == 0) { return 0; } else { sum = i * (i - 1); return sum + CrossM(i - 1); } } public static int sum(int[] arri, int i) { if (i < 1) { return 0; } else { return arri[i - 1] + sum(arri, i - 1); } } } |
Direct link: https://paste.plurk.com/show/1721243