1. 隨機數去重排序
明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應着不同的學生的學號。然後再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作(同一個測試用例裏可能會有多組數據,希望大家能正確處理)。
1.1 分析
去重:Set
排序:TreeSet
多組數據,要使用sc.hasNext()
import java.util.Scanner;
import java.util.TreeSet;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int i=0;
TreeSet<Integer> set=new TreeSet<Integer>();
while(i<n){
set.add(sc.nextInt());
i++;
}
while(!set.isEmpty())
System.out.println(set.pollFirst());
}
}
}
2.分割字符串
- 連續輸入字符串,請按長度爲8拆分每個字符串後輸出到新的字符串數組;
- 長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
2.1 分析
分爲三種情況:
- 長度小於8;
- 後面直接append 0 即可
- 等於8
- 直接輸出
- 大於8
- 分割
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
getResult(str1);
getResult(str2);
}
public static void getResult(String str){
if(str==null)
return;
int len=str.length();
StringBuffer sb=new StringBuffer(str);
if(len<8){
int i=len;
while(i<8){
sb.append("0");
i++;
}
System.out.println(sb.toString());
}
else if(len==8)
System.out.println(str);
else{
List<String> list=new ArrayList<String>();
int k=0;
for(;k+8<=len;k+=8){
list.add(str.substring(k,k+8));
}
if(k!=len){
StringBuffer sb2=new StringBuffer(str.substring(k));
int num=8-(len-k)%8;
for(k=0;k<num;k++)
sb2.append("0");
list.add(sb2.toString());
}
for(int j=0;j<list.size();j++)
System.out.println(list.get(j));
}
}
}
注:
1.substring(int beginindex) sustring(int beginindex,int endindex)
- beginIndex -- 起始索引(包括), 索引從 0 開始。
- endIndex -- 結束索引(不包括)。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
getResult(str1);
getResult(str2);
}
public static void getResult(String str){
while(str.length()>=8){
System.out.println(str.substring(0,8));
str=str.substring(8);
}
if(str.length()<8&&str.length()>0){
str+="00000000";
System.out.println(str.substring(0,8));
}
}
}
3. 進制轉換
寫出一個程序,接受一個十六進制的數值字符串,輸出該數值的十進制字符串。(多組同時輸入 )
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()){
String str=sc.nextLine();
getResult(str);
}
}
public static void getResult(String str){
char[] chs=str.toCharArray();
int len=chs.length-1;
int result=0;
int num=0;
for(int i=len;i>1;i--){
switch (chs[i]){
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
num=chs[i]-'7';
result+=num*((int)Math.pow(16,(len-i)));
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num=chs[i]-'0';
result+=num*((int)Math.pow(16,(len-i)));
break;
}
}
System.out.println(result);
}
}
注:
1.switch用法
package codeAnal;
public class SwitchDemo {
public static void main(String[] args) {
stringTest();
breakTest();
defautTest();
}
/*
* default不是必須的,也可以不寫
* 輸出:case two
*/
private static void defautTest() {
char ch = 'A';
switch (ch) {
case 'B':
System.out.println("case one");
break;
case 'A':
System.out.println("case two");
break;
case 'C':
System.out.println("case three");
break;
}
}
/*
* case語句中少寫了break,編譯不會報錯
* 但是會一直執行之後所有case條件下的語句,並不再進行判斷,直到default語句
* 下面的代碼輸出: case two
* case three
*/
private static void breakTest() {
char ch = 'A';
switch (ch) {
case 'B':
System.out.println("case one");
case 'A':
System.out.println("case two");
case 'C':
System.out.println("case three");
default:
break;
}
}
/*
* switch用於判斷String類型
* 輸出:It's OK!
*/
private static void stringTest() {
String string = new String("hello");
switch (string) {
case "hello":
System.out.println("It's OK!");
break;
default:
System.out.println("ERROR!");
break;
}
}
}
2.ASCII