1. 程式人生 > >攜程筆試

攜程筆試

inpu tokenizer star 除了 ioe aps int() 攜程 hide

1 判斷鏈表有環

2

技術分享圖片
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class z2 {
    //  [1,2,3,4,5]

    static StreamTokenizer in = new
StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } static String next() throws
IOException { in.nextToken(); return (String) in.sval; } public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); String str = input.nextLine(); int k = input.nextInt(); str = str.substring(1, str.length() - 1); String [] s
= str.split(","); for(int i = 0; i < s.length / k; i++) { // 需要翻轉的次數 // if((k & 1) != 0) { // 奇數,中間的不需要翻轉 int start = i * k; int end = start + k - 1; String tmp = null; while(start <= end) { tmp = s[start]; s[start] = s[end]; s[end] = tmp; start++; end--; } // } } System.out.print("["); for(int i = 0; i < s.length; i++) { if(i != s.length - 1) { System.out.print(s[i] + ","); }else { System.out.print(s[i] + "]"); } } System.out.println(); } }
View Code

3.

技術分享圖片
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class z3 {

    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static String next() throws IOException {
        in.nextToken();
        return (String) in.sval;
    }

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String[] s = new String[n];
        for (int i = 0; i < n; i++) {
            s[i] = input.next();
//            System.out.println(s[i]);
        }

        int num = 0;
        // path的前面一定是有/的,我根據前面的/來判定輸出的數
        for (int i = 0; i < n; i++) {
            num = 1;
            if (s[i].charAt(s[i].length() - 1) == ‘/‘) {
//                System.out.println(s[i] + " 最後是/");
                s[i] = s[i].substring(0, s[i].length() - 1);
//                System.out.println(s[i] + " 最後的/  已經去除了");
            }
            for (int j = 0; j < i; j++) {
                if (s[i].equals(s[j])) {
                    num++;
                }
            }
            String[] str = s[i].split("/");

            for (int k = 1; k < str.length; k++) {
                if (k == 1 || k == str.length - 1) {
                    System.out.print(1);
                } else {
                    System.out.print(num);
                }
            }
            System.out.print(" ");

        }

    }

}
View Code

攜程筆試