求大佬解一题Java数组题

Went1972 2019-10-16 04:42:58
跪求解,Java数组题,想知道怎么写
...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-10-16
  • 打赏
  • 举报
回复

import java.util.Scanner;
import java.util.stream.IntStream;

public class demo1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int[] s=new int[t*2];
IntStream.range(0, t).forEach(i->{
s[2*i]=sc.nextInt();
s[2*i+1]=sc.nextInt();
});
IntStream.range(0, t).forEach(i->{
IntStream.range(i+1, t).forEach(j->{
if(s[2*j+1]>s[2*i+1]) {
int k=s[2*i];
int l=s[2*i+1];
s[2*i]=s[2*j];
s[2*i+1]=s[2*j+1];
s[2*j]=k;
s[2*j+1]=l;
}
});
});
IntStream.range(0, t).forEach(i->{
System.out.print(s[2*i]+" "+s[2*i+1]+"\r\n");
});
}
}

不能留白 2019-10-16
  • 打赏
  • 举报
回复
引用 3 楼 qq_45757090 的回复:
[quote=引用 1 楼 不能留白 的回复:]我写的double型,有需要的你改成int型就行

package eg4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Tests {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生总人数:");
		int n = sc.nextInt();
		ArrayList<Student> students = new ArrayList<Student>();
		for (int i = 0; i < n; i++) {
			Student student = new Student(sc.nextDouble(), sc.nextDouble());
			students.add(student);
		}
		Collections.sort(students);
		System.out.println("排序后的成绩为:");
		for (Student student : students) {
			System.out.println(student.getXuehao() + " " + student.getChengji());
		}
	}
}
class Student implements Comparable<Student> {

	private double xuehao;
	private double chengji;

	public Student(double xuehao, double chengji) {
		this.xuehao = xuehao;
		this.chengji = chengji;

	}
	
	public double getXuehao() {
		return xuehao;
	}

	public void setXuehao(double xuehao) {
		this.xuehao = xuehao;
	}

	public double getChengji() {
		return chengji;
	}

	public void setChengji(double chengji) {
		this.chengji = chengji;
	}


	@Override
	public int compareTo(Student s) {
		int key = 0;
		if (s.getChengji() < this.getChengji())
			key = 1;
		else if (s.getChengji() == this.getChengji()) {
			if (s.getXuehao() < this.getXuehao())
				key = 1;
		}

		return key;
	}
}

大佬有没有简单点的方法,就用for,if 什么什么的我们还没学到这么后面[/quote] 首先,if和for都是最基础的 if是判断 for是循环 其次,解决这道题肯定要用到if和for的,没有比较,你怎么排序
Went1972 2019-10-16
  • 打赏
  • 举报
回复
引用 4 楼 淡青の月 的回复:
import java.util.*;

public class Rank {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = Integer.parseInt(input.nextLine());
int[] sno = new int[N];
int[] ach = new int[N];
for (int i = 0; i < N; i++) {
String s = input.nextLine();
String[] num = s.split(" ");
sno[i] = Integer.parseInt(num[0]);
ach[i] = Integer.parseInt(num[1]);
}
bubbleSort(sno, ach);
for (int i = 0; i < N; i++) {
System.out.println(sno[i] + " " + ach[i]);
}

}

public static void bubbleSort(int[] sno, int[] ach) {
boolean needNextPass = true;
for (int k = 1; k < ach.length && needNextPass; k++) {
needNextPass = false;
for (int i = 0; i < ach.length - k; i++) {
if (ach[i] > ach[i + 1]) {
int temp = ach[i];
int t = sno[i];
ach[i] = ach[i + 1];
sno[i] = sno[i + 1];
ach[i + 1] = temp;
sno[i + 1] = t;
needNextPass = true;
}

}
}
for (int i = 0; i < ach.length - 1; i++) {
if (ach[i] == ach[i + 1]) {
if (sno[i + 1] < sno[i]) {
int t = sno[i];
sno[i] = sno[i + 1];
sno[i + 1] = t;
}
}
}
}
}
这应该很基础,都能看懂
谢谢真的很谢谢
淡青の月 2019-10-16
  • 打赏
  • 举报
回复
import java.util.*;

public class Rank {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = Integer.parseInt(input.nextLine());
int[] sno = new int[N];
int[] ach = new int[N];
for (int i = 0; i < N; i++) {
String s = input.nextLine();
String[] num = s.split(" ");
sno[i] = Integer.parseInt(num[0]);
ach[i] = Integer.parseInt(num[1]);
}
bubbleSort(sno, ach);
for (int i = 0; i < N; i++) {
System.out.println(sno[i] + " " + ach[i]);
}

}

public static void bubbleSort(int[] sno, int[] ach) {
boolean needNextPass = true;
for (int k = 1; k < ach.length && needNextPass; k++) {
needNextPass = false;
for (int i = 0; i < ach.length - k; i++) {
if (ach[i] > ach[i + 1]) {
int temp = ach[i];
int t = sno[i];
ach[i] = ach[i + 1];
sno[i] = sno[i + 1];
ach[i + 1] = temp;
sno[i + 1] = t;
needNextPass = true;
}

}
}
for (int i = 0; i < ach.length - 1; i++) {
if (ach[i] == ach[i + 1]) {
if (sno[i + 1] < sno[i]) {
int t = sno[i];
sno[i] = sno[i + 1];
sno[i + 1] = t;
}
}
}
}
}
这应该很基础,都能看懂
Went1972 2019-10-16
  • 打赏
  • 举报
回复
引用 1 楼 不能留白 的回复:
我写的double型,有需要的你改成int型就行

package eg4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Tests {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生总人数:");
		int n = sc.nextInt();
		ArrayList<Student> students = new ArrayList<Student>();
		for (int i = 0; i < n; i++) {
			Student student = new Student(sc.nextDouble(), sc.nextDouble());
			students.add(student);
		}
		Collections.sort(students);
		System.out.println("排序后的成绩为:");
		for (Student student : students) {
			System.out.println(student.getXuehao() + " " + student.getChengji());
		}
	}
}
class Student implements Comparable<Student> {

	private double xuehao;
	private double chengji;

	public Student(double xuehao, double chengji) {
		this.xuehao = xuehao;
		this.chengji = chengji;

	}
	
	public double getXuehao() {
		return xuehao;
	}

	public void setXuehao(double xuehao) {
		this.xuehao = xuehao;
	}

	public double getChengji() {
		return chengji;
	}

	public void setChengji(double chengji) {
		this.chengji = chengji;
	}


	@Override
	public int compareTo(Student s) {
		int key = 0;
		if (s.getChengji() < this.getChengji())
			key = 1;
		else if (s.getChengji() == this.getChengji()) {
			if (s.getXuehao() < this.getXuehao())
				key = 1;
		}

		return key;
	}
}

大佬有没有简单点的方法,就用for,if 什么什么的我们还没学到这么后面
Went1972 2019-10-16
  • 打赏
  • 举报
回复
谢谢谢谢太感谢了
不能留白 2019-10-16
  • 打赏
  • 举报
回复
我写的double型,有需要的你改成int型就行

package eg4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Tests {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生总人数:");
		int n = sc.nextInt();
		ArrayList<Student> students = new ArrayList<Student>();
		for (int i = 0; i < n; i++) {
			Student student = new Student(sc.nextDouble(), sc.nextDouble());
			students.add(student);
		}
		Collections.sort(students);
		System.out.println("排序后的成绩为:");
		for (Student student : students) {
			System.out.println(student.getXuehao() + " " + student.getChengji());
		}
	}
}
class Student implements Comparable<Student> {

	private double xuehao;
	private double chengji;

	public Student(double xuehao, double chengji) {
		this.xuehao = xuehao;
		this.chengji = chengji;

	}
	
	public double getXuehao() {
		return xuehao;
	}

	public void setXuehao(double xuehao) {
		this.xuehao = xuehao;
	}

	public double getChengji() {
		return chengji;
	}

	public void setChengji(double chengji) {
		this.chengji = chengji;
	}


	@Override
	public int compareTo(Student s) {
		int key = 0;
		if (s.getChengji() < this.getChengji())
			key = 1;
		else if (s.getChengji() == this.getChengji()) {
			if (s.getXuehao() < this.getXuehao())
				key = 1;
		}

		return key;
	}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧