KEDA xunfei qiuzhao's written test programming questions and KEDA xunfei's written test Programming

Source: Internet
Author: User

KEDA xunfei qiuzhao's written test programming questions and KEDA xunfei's written test Programming
Topic Description: quarrel with teammates

Ideas:

It feels like finding a regular question: Because the leftmost LL and rightmost R will never be deleted, so what we need to do is to subtract the number of people from the first R from the last L position.

Code implementation:
Import java. util. extends; public class Main {public static void main (String [] args) {Using SC = new using (System. in); String queue = SC. next (); int lCount = 0, rCount = queue. length ()-1, count = 0; for (int I = 0; I <queue. length (); I ++) {if (queue. charAt (I) = 'R') // find the first R break; lCount ++;} for (int j = queue. length ()-1; j> = 0; j --) {if (queue. charAt (j) = 'l') // find the last L break; rCount --;} System. out. println (lCount + "" + rCount); count = queue. length ()-(rCount-lCount); if (count> queue. length () count = queue. length (); System. out. println (count );}}

Merge into or one line of implementation:

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        String queue = sc.next();        int count = 0;        count = queue.length() - (queue.lastIndexOf("L") - queue.indexOf("R"));        System.out.println(count);    }}
Title: ball games

Ideas:

Overall, everyone may have a clear idea, but the trouble is that the input is the key to splitting the input.

Tips tips: pay special attention to the requirements of the final ranking: first, win the ball based on the same points, and then the same number of players. Therefore, you must note when writing the sorting function.

Code implementation:
Import java. util. arrays; import java. util. comparator; import java. util. imports; import java. util. treeMap; public class laoxu {class Team {String name; int grade; int realwin; int in; public Team (String name, int grade, int realwin, int in) {this. name = name; this. grade = grade; this. realwin = realwin; this. in = in;} public void setGrade (int grade) {this. grade = grade;} public void setRealwin (int Realwin) {this. realwin = realwin;} public void setIn (int in) {this. in = in ;}} public static void main (String [] args) {nation SC = new Nation (System. in); laoxu main = new laoxu (); while (SC. hasNext () {String ns = SC. nextLine (); int n = Integer. valueOf (ns); Team [] win = new Team [n]; // name of n teams TreeMap <String, Team> map = new TreeMap <> (); for (int I = 0; I <n; I ++) {String cname = SC. nextL Ine (); Team team = main. new Team (cname, 0, 0, 0); map. put (cname, team);} int len = (n * (n-1)/2; // process all the competition results for (int I = 0; I <len; I ++) {// into a space into the team str [0] and the score str [1] were processed // A-B String [] str = SC. nextLine (). split (""); String [] name = str [0]. split ("-"); String cname1 = name [0]; // A String cname2 = name [1]; // B String [] grad = str [1]. split (":"); int c1 = Integer. valueOf (grad [0]); // 1 int c2 = Integer. valueOf (grad [1]); // 2 int c = c1-c2; // score gap, used to calculate the final score int grade1 = 0; // score 3 1 0 int realwin1 = 0; // Number of winning goals int in1 = c1; // Number of balls int grade2 = 0; int realwin2 = 0; int in2 = c2; if (c = 0) {// draw grade1 = 1; grade2 = 1;} if (c> 0) {grade1 = 3; realwin1 = c ;} if (c <0) {grade2 = 3; realwin2 = c2-c1;} // you can specify the score, number of BITs, and number of BITs. Team = map. get (cname1); team. setGrade (team. grade + g Rade1); team. setIn (team. in + in1); team. setRealwin (team. realwin + realwin1); map. put (cname1, team); Team team2 = map. get (cname2); team2.setGrade (team2.grade + grade2); team2.setIn (team2.in + in2); team2.setRealwin (team2.realwin + realwin2); map. put (cname2, team2);} int I = 0; for (String key: map. keySet () {win [I ++] = map. get (key) ;}// custom sorting comparator. Sort rules: // first, score by point, second, and last number of players. sort (win, new Comparator <Team> () {public int compare (Team o1, Team o2) {int grade = o2.grade-o1.grade; int realwin = o2.realwin-o1.realwin; int in = o2.in-o1.in; if (grade! = 0) return grade; else if (realwin! = 0) return realwin; else return in ;}}); String [] name = new String [n/2]; for (I = 0; I <n/2; I ++) name [I] = win [I]. name; Arrays. sort (name); // outputs for (I = 0; I <n/2; I ++) System in alphabetical order for the team name. out. println (name [I]) ;}}
Course conflict

Ideas:

Using map's key-Value Pair feature, the key stores the time information and the value stores the course information of the corresponding time. Because there may be multiple courses (that is, conflicts) at the same time, therefore, the value is stored in ArrayList.

After worker completes processing, traverse the map to find the list size of the corresponding time (that is, there are several courses at the same time). If it is greater than 1, there is a conflict; if none, "YES" is output ".

Attention attention should be paid to the problem output in chronological order, so you need to use TreeMap.

Code implementation:
import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Scanner;import java.util.TreeMap;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        Map<String, List<String>> map = new TreeMap<String, List<String>>();        int n = sc.nextInt();        for (int i = 0; i < n; i++) {            String time = sc.next(), code = sc.next();            if (!map.containsKey(time))                map.put(time, new ArrayList<String>());            map.get(time).add(code);        }        for (String time : map.keySet()) {            if (map.get(time).size() > 1) {                System.out.printf(time);                for (String code : map.get(time))                    System.out.printf(" " + code);                System.out.printf("\n");            }        }    }}

--Happy to share and make progress together. Please add
--Any comments greatly appreciated
--We sincerely welcome your discussion! QQ: 1138517609

Copyright Disclaimer: Thank you for visiting us! Welcome to guidance, errata, discussion and reprint, reprinted please indicate the source of http://blog.csdn.net/u011489043/article/details/79558566

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.