T1153,T1121,T1154
- T1153
- T1121
- T1154
- 计蒜客网址
T1153
蒜术师给了你一个 1010 个整数的序列,要求对其重新排序。排序要求:
奇数在前,偶数在后;
奇数按从大到小排序;
偶数按从小到大排序。
解析:使用 ArrayList 存储奇数数据和偶数数据 调用 Collections 中的sort(List,Comparator) 根据指定的比较器对List进行排序
package com.java3;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;public class T1153 {public static void main(String[] args) {Scanner scan = new Scanner(System.in);ArrayList<Integer> oddList = new ArrayList<>();ArrayList<Integer> evenList = new ArrayList<>();for(int i = 0; i < 10;i++){int temp = scan.nextInt();if(temp % 2 == 0){evenList.add(temp);}else{oddList.add(temp);}}//奇数按从大到小排序Collections.sort(oddList, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return -(o1 - o2);}});//偶数按从小到大排序Collections.sort(evenList, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}});for(int i = 0; i < oddList.size();i++){System.out.print(oddList.get(i) + " ");}for(int i = 0; i < evenList.size();i++){System.out.print(evenList.get(i) + " ");}}
}
4 7 3 13 11 12 0 47 34 98
47 13 11 7 3 0 4 12 34 98
T1121
输入一行单词序列,相邻单词之间由 1 个或多个空格间隔,请对应地计算各个单词的长度。
注意,如果有标点符号(如连字符,逗号,句号),标点符号算作与之相连的词的一部分。没有被空格间隔开的符号串,都算作单词。
解析:使用scan.nextLine() 接受这一行单词序列,使用split(" \\s+ ")对单词序列进行分割 使用ArrayList接受每个分割部分返回的长度
package com.java3.ch3;import java.util.ArrayList;
import java.util.Scanner;public class T1121 {public static void main(String[] args) {Scanner scan = new Scanner(System.in);String sentence = scan.nextLine();String [] arrayStr = sentence.split("\\s+");ArrayList strLength = new ArrayList();for(int i = 0;i < arrayStr.length;i++){strLength.add(arrayStr[i].length());}for(int i = 0;i < strLength.size();i++){if(i == strLength.size() - 1){System.out.print(strLength.get(i));}else{System.out.print(strLength.get(i) + ",");}}}
}
She was born in 1990-01-02 and from Beijing city.
3,3,4,2,10,3,4,7,5
T1154
蒜头君和朋友们去爬香山,为美丽的景色所陶醉,想合影留念。如果他们站成一排,男生全部在左(从拍照者的角度),并按照从矮到高的顺序从左到右排,女生全部在右,并按照从高到矮的顺序从左到右排,请问他们合影的效果是什么样的(所有人的身高都不同)?
艹,数据接收不进去
2021-5-4:之前测试测错了
package com.java3.ch3;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;public class T1154 {public static void main(String[] args) {Scanner scan = new Scanner(System.in);ArrayList<Double> maleList = new ArrayList<>();ArrayList<Double> femaleList = new ArrayList<>();//数据接收int amount = scan.nextInt();//scan.nextLine();for(int i = 0;i < amount;i++){String gender = scan.next();double height = scan.nextDouble();if(gender.equals("male")){maleList.add(height);}else{femaleList.add(height);}}//测试数据接收
// for(Double temp:maleList){
// System.out.println("male's height is " + temp);
// }
// for(Double temp:femaleList){
// System.out.println("female's height is " + temp);
// }//使用 Collections.sort()对List进行排序//失误了, new Comparator() 需要返回整数 输入的是浮点型我还没有想出来怎么做
// Collections.sort(maleList, new Comparator<Double>() {
// @Override
// public int compare(Double o1, Double o2) {
// return (int)(o1 - o2)*100;
// }
// });
//
// Collections.sort(femaleList, new Comparator<Double>() {
// @Override
// public int compare(Double o1, Double o2) {
// return (int)(o2 - o1)*100;
// }
// });//使用冒泡排序对集合进行排序//maleList 从矮到高for(int i = 0;i < maleList.size();i++){for(int j = 0;j < maleList.size() - 1 - i;j++){if(maleList.get(j) > maleList.get(j + 1)){Double temp = maleList.get(j);maleList.set(j,maleList.get(j + 1));maleList.set(j + 1,temp);}}}//femaleList 从高到矮for(int i = 0;i < femaleList.size();i++){for(int j = 0;j < femaleList.size() - 1 - i;j++){if(femaleList.get(j) < femaleList.get(j + 1)){Double temp = femaleList.get(j);femaleList.set(j,femaleList.get(j + 1));femaleList.set(j + 1,temp);}}}//输出数据 保留两位小数for(Double temp:maleList){System.out.print(String.format("%.2f",temp) + " ");}for(Double temp:femaleList){System.out.print(String.format("%.2f",temp) + " ");}}
}
6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56
1.65 1.72 1.78 1.70 1.61 1.56
计蒜客网址
直达链接