水题 P1181 1208 1223 1094

这几个题应该都不是什么很大问题的题代码直接丢这里了,需要的可以看看,由于当时写的时候不是很清楚sort的用法,所以一直用的集合,大家直接 Arrays.sort就好了
P1181

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int count = 1;
		int sum = 0;
		int a;
		for(int i = 0;i < n;i++){
			a = sc.nextInt();
			sum += a;
			if(sum > m){
				count++;
				sum = a;
			}
		}
		System.out.println(count);
	}

}

P1208

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		List<Marry> list = new LinkedList<Marry>();
		for(int i = 0;i < m;i++){
			int a = sc.nextInt();
			int b = sc.nextInt();
			Marry marry = new Marry(a,b);
			list.add(marry);
		}
		Collections.sort(list);
		Iterator<Marry> it = list.iterator();
		int sum = 0;
		int count = 0;
		while(it.hasNext()){
			Marry marry = it.next();
			if(sum + marry.b < n){
				sum += marry.b;
				count += marry.a * marry.b;
			}
			else{
				int ok = n - sum;
				count += marry.a*ok;
				break;
			}
		}
		System.out.println(count);
	}
}
class Marry implements Comparable{
	int a; // 单价
	int b; // 牛奶数
	Marry(int a,int b){
		this.a = a;
		this.b = b;
	}
	public int compareTo(Object b){
		Marry a = (Marry)b;
		if(this.a < a.a)
			return -1;
		else
			return 1;
	}
}

P1223

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		List<jieshui> list = new LinkedList<jieshui>();
		for(int i = 0;i < n;i++){
			int m = sc.nextInt();
			jieshui jie = new jieshui(i+1,m);
			list.add(jie);
		}
		Collections.sort(list);
		Iterator<jieshui> it = list.iterator();
		double time = 0;
		int t = 0;
		int count = 0;
		while(it.hasNext()){
			count++;
			time += t;
			jieshui jie = it.next();
			if(count != n)
			    System.out.print(jie.no+" ");
			else
				System.out.println(jie.no);
			t += jie.time;
		}
		time = time/n;
		System.out.printf("%.2f",time);
	}
}
class jieshui implements Comparable{
	int no;
	int time;
	jieshui(int no,int time){
		this.no = no;
		this.time = time;
	}
	public int compareTo(Object b){
		jieshui a = (jieshui)b;
		if(this.time < a.time)
			return -1;
		else
			return 1;
	}
}

P1094

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); //最大金额
		int m = sc.nextInt(); //礼物件数
		int a[] = new int[m];
		for(int i = 0;i < m;i++)
			a[i] = sc.nextInt();
		Arrays.sort(a);
		int count = 0;
		int k = 0;
		for(int i = m-1;i >= 0;i--){
			if(i < k)
				break;
			else if(i == k){
				count++;
				break;
			}
			if(a[i] + a[k] > n){
				count++;
			}
			else{
				count++;
				k++;
			}
		}
		System.out.println(count);
	}

}

发布了32 篇原创文章 · 获赞 5 · 访问量 862

猜你喜欢

转载自blog.csdn.net/shizhuba/article/details/104288561