日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

题库练习2(随机数去重排序、分割字符串、进制转换)

發布時間:2024/10/14 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 题库练习2(随机数去重排序、分割字符串、进制转换) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

    A的ASCII碼是65,a的ASCII碼是97。

    ?

    總結

    以上是生活随笔為你收集整理的题库练习2(随机数去重排序、分割字符串、进制转换)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。