我是靠谱客的博主 虚心海燕,最近开发中收集的这篇文章主要介绍codeforces 632C (练习Java排序),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目:http://codeforces.com/contest/632/problem/C
套了Petr的Java板子,来练习一下,没想到写个排序都那么费劲,,

  • 1.在Comparator中重载比较函数
  • 2.重写一个String类,重载比较函数

1.在Comparator中重载比较函数 代码:

import java.io.OutputStream;//输入输出流
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.Comparator;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            String [] str = new String[n];
            for(int i = 0;i < n;i++)
                str[i] = in.next();


            Arrays.sort(str,new Comparator<String>(){
                @Override
                public int compare(String a, String b) {
                    return (a+b).compareTo(b+a);
                }
            });
            for(int i = 0;i < n;i++)
                out.print(str[i]);
            out.println();
        }

        private void fun(){

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

1.在Comparator中重载比较函数

和上面类似的:

import java.io.OutputStream;//输入输出流
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.Comparator;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            String [] str = new String[n];
            for(int i = 0;i < n;i++)
                str[i] = in.next();

            Comparator<String> cmp = new Comparator<String>(){
                @Override
                public int compare(String a, String b) {
                    return (a+b).compareTo(b+a);
                }
            };
            Arrays.sort(str,cmp);
            for(int i = 0;i < n;i++)
                out.print(str[i]);
            out.println();
        }

        private void fun(){

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

2.重写一个String类,重载比较函数:

import java.io.OutputStream;//输入输出流
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.Comparator;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            S[] str = new S[n];
            for(int i = 0;i < n;i++)
                str[i] = new S(in.next());

            Arrays.sort(str,0,n);
            for(int i = 0;i < n;i++)
                System.out.print(str[i].s);
            //out.println();
        }

        private void fun(){

        }
    }

    static class S implements Comparable<S>{
        String s;
        S(String ss){
            s = ss;
        }
        public int compareTo(S p) {
            return (s+p.s).compareTo(p.s+s);
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

最后

以上就是虚心海燕为你收集整理的codeforces 632C (练习Java排序)的全部内容,希望文章能够帮你解决codeforces 632C (练习Java排序)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部