題目:計算最大連續非遞減的數字長度                                      程式檔名:test04.java

說明:求輸入大於0的正整數N(最多為十個位數),輸出最大連續非遞減的數字長度。程式中必須有須有判斷範圍的程式,若是超出題目所訂定的字串長度則要求重新輸入。輸入一字元“0”時即結束此程式。

(1).如輸入8 4 5 7 2 6 8 8 0 2

輸出  4

(2).如輸入1 2 3 4 5 6 7 8 9 0

輸出 9

畫面輸出結果:

INPUT==>12345678901

OUTPUT ==>輸出:範圍錯誤,請重新輸入。

 

INPUT==>8457268802

OUTPUT ==> 4

 

INPUT==>1234567890

OUTPUT ==> 9

 

INPUT==>     ß代表程式結束。

 

  -----------

 

import java.io.*;

public class test0611 {
public static void main(String[] args)throws Exception {
BufferedReader buf ;

buf= new BufferedReader(new InputStreamReader(System.in));
String str =buf.readLine();
if( str.length() > 10){System.out.println("範圍錯誤");
return;}
}

}

---

import java.io.*;

public class test0611 {
public static void main(String[] args) throws Exception {
BufferedReader buf;
while (true) {
buf = new BufferedReader(new InputStreamReader(System.in));
String str = buf.readLine();
if (str.length() > 10) {
System.out.println("範圍錯誤");
return;
}
}

}
}

--Console

 

123
123546
12345789
1234567890
12345678901
範圍錯誤

---

 

import java.io.*;

public class test0611 {
public static void main(String[] args) throws Exception {
BufferedReader buf;
while (true) {
buf = new BufferedReader(new InputStreamReader(System.in));
String str = buf.readLine();
if (str.length() > 10) {
System.out.println("範圍錯誤");
continue;
}
if (str.equals("0") ) {
System.out.println("程式結束");
return;
}
}
}
}

 ---

--Console

123
12345678901
範圍錯誤
123
0
程式結束

---

 

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
*/

 --------------------02

 

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
*/

 

 

arrow
arrow
    全站熱搜

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