---送分題

import java.io.*;

public class TEST {
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() > 10) { // str.length() = 字串長度
System.out.println("範圍錯誤");
continue; // 跳回迴圈開頭
}

if (str.equals("00")) { // equals = 比對,也有可能井字號時結束
System.out.println("結束程式");
return; //結束副程式
}

int count = 1, max = 0; //因為兩個做比較,所以剛開始 count 設置為 1,條件程式時 count = 1+1 
// 本身就有一個數,所以 count = 1,當後項>前項時,count 為2
// 因為有一個 i+1 所以 i 的最大值要 -1,不然會超過陣列範圍
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i + 1) >= str.charAt(i)) {
// 後項字元 >= 前項字元
count += 1;
if (count > max) {
// 要求最大非遞減,所以 count > max 時, max = count
max = count;
}
} else {
count = 1; // 當後項較小時,重新計算最大非遞減
}
}
System.out.println("最大非遞減:" + max);
}
}
}

/*
*    i= 0123456789

str = "8457268802" ;
* i=0時,str.charAt(1)='4',str.charAt(0)='8',4<8所以 count=1,max=1
* i=1時,str.charAt(2)='5',str.charAt(1)='4',5>4所以 count=2,max=2
* i=2時,str.charAt(3)='7',str.charAt(2)='5',7>5所以 count=3,max=3
* i=3時,str.charAt(4)='2',str.charAt(3)='7',2<7所以 count=1,max=3
* i=4時,str.charAt(5)='6',str.charAt(4)='2',6>2所以 count=2,max=2
* i=5時,str.charAt(6)='8',str.charAt(5)='6',8>6所以 count=3,max=3
* i=6時,str.charAt(7)='8',str.charAt(6)='8',8=8所以 count=4,max=4
* i=7時,str.charAt(8)='0',str.charAt(7)='8',0<8所以 count=1,max=4
* i=8時,str.charAt(9)='2',str.charAt(8)='0',2>0所以 count=2,max=4
* i=9時,str.charAt(10)='',str.charAt(9)='2',_<2所以 count=1,max=4
*/

---

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]);
}
}
}
}

/*
* str = "6Aa886aBAa88"; 
* 111122212334
*/

---

 

import java.io.*;
public class test3 {
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 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;//前項歸零
}
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= 01234567890
例如 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
*/

---

 

 

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");
int sum=0;
for(n=1;n<=50;n++)
{
if(a[n]==1) //抽取的數字
{
sum=sum+n;
System.out.print(n +"\t");
count=count+1;

if(count%10==0){System.out.println();}
if(count==40 ){break;}
}
}
System.out.println("加總 "+sum);
}
}

---

 

 

arrow
arrow
    全站熱搜

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