import java.io.*;

public class test2 {
public static void main(String[] args) throws IOException {
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
while (true) { // 無窮迴圈
String str = buf.readLine(); // 鍵盤輸入一行

if (str.length() > 15) { // str.length() = 字串長度
System.out.println("範圍錯誤");
continue; // 跳回迴圈開頭
}
if (str.equals("0")) { // equals = 比對,也有可能井字號時結束
System.out.println("結束程式");
return; // 結束副程式
}

int count = 0, max = 0, a[] = new int[str.length()];
// 先抓取一個字元,再將此字元和全部的字元做比較
// 若字元相同,則count+1
for (int i = 0; i < str.length(); i++) { // 先抓取一個字元
count = 0;
for (int j = 0; j < str.length(); j++) {
if (str.charAt(j) == str.charAt(i)) { // 此字元和全部字元做比較
count++;
a[j] = count;// 編號j處存取 count 值
}
}
System.out.print(a[i]);
}
for (int i = 0; i < str.length(); i++) {
System.out.print(a[i]);
}

}
}
}
---
import java.io.*;
public class test3 {
public static void main(String[] args) throws Exception {
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
while (true) { // 無窮迴圈
String str = buf.readLine(); // 鍵盤輸入一行

if (str.length() > 15) { // str.length() = 字串長度
System.out.println("範圍錯誤");
continue; // 跳回迴圈開頭
}

if (str.equals("00")) { // equals = 比對,也有可能井字號時結束
System.out.println("結束程式");
return; // 結束副程式
}
int a[]=new int [str.length()],i,sum = 0;
for(i=0;i<str.length();i=i+2){
a[i]=str.charAt(i)-48;}
for(i=0;i<str.length();i++){
if(str.charAt(i)=='*'){
//乘的時候字元相同,乘積放在後項
a[i+1]=a[i-1]*a[i+1];
a[i-1]=0;//前項歸零
}
}
for(i=0;i<str.length();i=i+2){ //索引編號是偶數元素做加總
sum += a[i];
}
System.out.println("運算式結果為: "+sum);
}
}
}
/* 編號i= 0123456789
* 例如 str="2+6*8*2+5*9"
* a[i]= 2 6 8 2 5 9 //a[i]取編號02468
* //i=3時,a[4]=48,a[2]=0
* //i=5時,a[6]=96,a[4]=0
* //i=9時,a[10]=45,a[8]=0
* a[0]=2,a[2]=0,a[4]=0,a[6]=96,a[8]=0,a[10]=45
*
*
*/
---------------------TEST5

題目:隨機選取 程式檔名:test10.java
說明:請由50位學生中抽出40位學生,抽出來的座號不可重覆,假設學生的座號是由1號到50號。輸出在瑩幕上,輸出包括排序前及排序後的學生座號,以tab隔開。若有排序指令,不得使用排序指令。
畫面輸出結果:
排序前:
8 19 47 38 21 49 46 2 50 24
23 1 15 48 37 17 39 6 42 3
11 27 18 12 28 31 40 34 5 35
44 26 20 16 22 45 33 4 32 41

排序後:
1 2 3 4 5 6 8 11 12 15
16 17 18 19 20 21 22 23 24 26
27 28 31 32 33 34 35 37 38 39
40 41 42 44 45 46 47 48 49 50


---


--Console

2+6*8*2+5*9
運算式結果為: 143

----------

import java.io.*;
import java.util.*;
public class test4 {

public static void main(String args[])throws IOException
{
Random rd=new Random();//建立亂數物件 Random為類別,rd為Random的物件
int a[]=new int[51];//編號1到編號50
int i=0,count=0,n=0;
System.out.println("排序前");
while(true)
{
n=rd.nextInt(50);//抽出0到49的數字
n=n+1;//+1後,n的值為 1到50
if(a[n]==0)//a[n]==0表示,n這個數字尚未被選取
{
System.out.print(n +"\t");
count=count+1;//抽出的數目+1
a[n]=1;//a[n]=1表示這個數字已被選取,不能再被選
if(count%10==0){System.out.println();}//抽出10的倍數要換行
if(count==40 ){break;}//抽出40個跳出迴圈
}

}
System.out.println("排序後"+"\n");

for(n=1;n<=50;n++)
{
if(a[n]==1)
{
System.out.print(n +"\t");
count=count+1;

if(count%10==0){System.out.println();}
if(count==40 ){break;}
}
}
}
}

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()