hdoj简单题目(二)

hdoj1391 简单找规律题目,如果点存在,则在直线y=x或者y=x-2上,如果x为偶数,结果为x+y,否则为x+y-1.

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x, y; int res; while(0 != n--) { x = sc.nextInt(); y = sc.nextInt(); if(y==x || y == x-2) { if(x%2 == 0) { res = x+y; } else { res = x+y-1; } } else { res = -1; } if(res == -1) { System.out.println("No Number"); } else { System.out.println(res); } } } }

 

hdoj1280 本以为数据量是很大的,简单的求结果排序是会超时的,但事实证明没有超时。

import java.util.*; public class Main1280 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n,m; int[] num; int[] res; while(sc.hasNextInt()) { n = sc.nextInt(); m = sc.nextInt(); num = new int[n]; res = new int[n*(n-1)/2]; for(int i=0; i<num.length; i++) { num[i] = sc.nextInt(); } Arrays.sort(num); int index=0; for(int j=0; j<num.length-1; j++) { for(int k=j+1; k<num.length; k++) { res[index] = num[j]+num[k]; index ++; } } Arrays.sort(res); for(int i=res.length-1; i>=res.length-m; i--) { if(i == res.length-m) { System.out.println(res[i]); } else { System.out.print(res[i]+" "); } } } } }

 

hdoj1236 这个题主要是输入的数据结构比较复杂,处理好输入问题就解决了,没有难度。

import java.util.*; public class Main1236 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n,m,line; int[] score; List<Student> al = new ArrayList<Student>(); Student stu; while(true) { n = sc.nextInt(); if(n == 0) { return; } else { m = sc.nextInt(); line = sc.nextInt(); score = new int[m]; for(int i=0; i<score.length; i++) { score[i] = sc.nextInt(); } for(int i=0; i<n; i++) { String no = sc.next(); int nn = sc.nextInt(); int total = 0; for(int j=0; j<nn; j++) { int mm = sc.nextInt(); total += score[mm-1]; } if(total >= line) { stu = new Student(no); stu.setScore(total); al.add(stu); } } Collections.sort(al); System.out.println(al.size()); for(int i=0; i<al.size(); i++) { System.out.println(al.get(i).stuNo+" "+al.get(i).score); } al.clear(); } } } static class Student implements Comparable { String stuNo; int score; public Student(String stuNo) { this.stuNo = stuNo; } public void setScore(int score) { this.score = score; } public int compareTo(Object o) { Student other = (Student)o; if(this.score > other.score) { return -1; } else if(this.score < other.score) { return 1; } else { if(this.stuNo.compareTo(other.stuNo)>0) { return 1; } else if(this.stuNo.compareTo(other.stuNo)<0){ return -1; } else { return 0; } } } } }

 

hdoj1209 题目很简单,就是把时间按时钟夹角排序,输出排在中间的那个的时间,没有ac???

import java.util.*; public class Main1209 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String times; Clock clock; List<Clock> al = new ArrayList<Clock>(); while(0 != t--) { for(int i=0; i<5; i++) { times = sc.next(); clock = new Clock(times); al.add(clock); } Collections.sort(al); System.out.println(al.get(2).times); al.clear(); } } static class Clock implements Comparable { String times; double angle; public Clock(String _times) { times = _times; String[] s = times.split(":"); angle = 30*Integer.parseInt(s[0])-5.5*Integer.parseInt(s[1]); while(angle > 360) { angle -= 360; } angle = angle>180 ? (360-angle) : angle; } public int compareTo(Object o) { Clock c = (Clock)o; if(this.angle > c.angle) { return 1; } else if(this.angle < c.angle) { return -1; } else { if(this.times.compareTo(c.times) > 1) { return 1; } else { return -1; } } } } }

 

hdoj1029 题目本身很好理解,如果仅仅是做出来的话,也相当的简单,一个统计就完成了,当然这样做会消耗很多空间和时间,

这里想到了类似hash应用的东西,每个整数作为数组的下标,其值用来统计出现的次数,这样操作就快多了。

import java.util.*; public class Main1029 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; int[] num = new int[32768]; int res = 0; while(sc.hasNextInt()) { n = sc.nextInt(); for(int i=0; i<n; i++) { int m = sc.nextInt(); num[m] ++; if(num[m] >= (n+1)/2) { res = m; } } System.out.println(res+""); } } }

你可能感兴趣的:(数据结构,object,String,Class,360,import)