踩的雷们&&知识点
- 对数组中的每一个对象必须分别进行实例化操作
https://blog.csdn.net/lxxiang1/article/details/82320720 - 对象排序
https://blog.csdn.net/ted_cs/article/details/82713706
https://blog.csdn.net/wangtaocsdn/article/details/71500500
代码
package com.ljm.demo.lx3;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
class Gys implements Comparable<Gys>{
int num=0;
int gynl=0;
int fy=0;
public int getfy(){
return fy;
}
public Gys(int num,int gynl,int fy){
this.num=num;
this.gynl=gynl;
this.fy=fy;
}
@Override
public int compareTo(Gys o){
return this.fy-o.fy;//从小到大排序
}
}
public class gyswt {
static public void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
Gys []g=new Gys[n];
for(int i=0;i<n;i++){
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
Gys g1=new Gys(a,b,c);
g[i]=g1;
}
Arrays.sort(g);
int pos=0;
int result=0;
StringBuffer str=new StringBuffer();
Out:
for(int i=0;i<m;i++){
int bh=s.nextInt();
int xy=s.nextInt();
while(xy>0){
if(g[pos].gynl>xy){
str.append(g[pos].num+" "+bh+" "+xy+",");
g[pos].gynl-=xy;
break;
}
else{
xy=xy-g[pos].gynl;
str.append(g[pos].num+" "+bh+" "+g[pos].gynl+",");
pos++;
if(pos==n)
{
result=-1;
break Out;
}
}
}
}
if(result==-1) System.out.println(-1);
else{
String str_new= str.toString();
String []arr=str_new.split(",");
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
}