Ai
1 Star 0 Fork 0

学C语言的枫子/Java_test

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_04_05.java 5.06 KB
一键复制 编辑 原始数据 按行查看 历史
学C语言的枫子 提交于 2024-04-05 11:26 +08:00 . Java刷题
package test_01;
public class test_04_05 {
/* public class Fraction {
private int fenZi;
private int fenMu;
public Fraction() {
}
public Fraction(int fenZi, int fenMu) {
super();
this.fenZi = fenZi;
this.fenMu = fenMu;
}
public int getFenZi() {
return fenZi;
}
public void setFenZi(int fz) {
this.fenZi = fz;
}
public int getFenMu() {
return fenMu;
}
public void setFenMu(int fm) {
this.fenMu = fm;
}
public void huaJian() {
if (fenZi == 0) {
fenMu = 1;
}
int yueshu = getYueshu();
if (fenMu > 0) {
fenZi /= yueshu;
fenMu /= yueshu;
} else {
fenZi /= -yueshu;
fenMu /= -yueshu;
}
}
private int getYueshu() {
int a = Math.abs(fenZi);
int b = Math.abs(fenMu);
int smaller = Math.min(a, b);
for (int i = smaller; i > 1; i--) {
if (a % i == 0 && b % i == 0) {
return i;
}
}
return 1;
}
public String toString() {
if (fenMu == 1) {
return fenZi + "";
}
return fenZi + "/" + fenMu;
}
public void jia(Fraction a) {
this.fenZi = this.fenZi * a.getFenMu() + a.getFenZi() * this.fenMu;
this.fenMu = this.fenMu * a.getFenMu();
}
public void jian(Fraction a) {
this.fenZi = this.fenZi * a.getFenMu() - a.getFenZi() * this.fenMu;
this.fenMu = this.fenMu * a.getFenMu();
}
}
*/
/* Scanner sc = new Scanner(System.in);
ArrayList<MixedFraction> list = new ArrayList<>();
Collections.addAll(list, new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()));
for (MixedFraction num : list) {
System.out.print(num + " ");
}
System.out.println();
Collections.reverse(list);
for (MixedFraction num : list) {
System.out.print(num + " ");
}
System.out.println();
HashSet<MixedFraction> hashSet = new HashSet<>(list);
System.out.println(list.size() - hashSet.size() + 1);
TreeSet<MixedFraction> treeSet = new TreeSet<>(list);
for (MixedFraction num : treeSet) {
System.out.print(num + " ");
}
System.out.println();
MixedFraction maxMixedFraction = new MixedFraction("0");
MixedFraction target = new MixedFraction("4[1/3]");
for (MixedFraction num : treeSet) {
if (num.compareTo(target) <= 0) {
if (num.compareTo(maxMixedFraction) >= 0) maxMixedFraction = num;
}
}
System.out.print(maxMixedFraction + " ");
}
class MixedFraction extends Fraction implements Comparable<MixedFraction> {
private int zengShu;
public int getZengShu() {
return zengShu;
}
public void setZengShu(int zengShu) {
this.zengShu = zengShu;
}
public String toString() {
return getZengShu() + "[" + super.toString() + "]";
}
public MixedFraction(String text) {
Pattern pattern = Pattern.compile("\\d+|-\\d+");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
if (this.zengShu == 0) {
setZengShu(Integer.parseInt(matcher.group()));
} else if (this.getFenZi() == 0) {
setFenZi(Integer.parseInt(matcher.group()));
} else if (this.getFenMu() == 0) {
setFenMu(Integer.parseInt(matcher.group()));
}
}
}
public int compareTo(MixedFraction o) {
return (int) ((double) ((this.getZengShu() * this.getFenMu()) + this.getFenZi()) / this.getFenMu() - (double) ((o.getZengShu() * o.getFenMu()) + o.getFenZi()) / o.getFenMu());
}
public int hashCode() {
int result = Integer.toString(getZengShu()).hashCode();
result = 17 * result + Integer.toString(getFenZi()).hashCode();
result = 17 * result + Integer.toString(getFenMu()).hashCode();
return result;
}
public boolean equals(Object obj) {
if (!(obj instanceof MixedFraction)) {
return false;
}
MixedFraction MixedObj = (MixedFraction) obj;
if (this == MixedObj) {
return true;
}
return MixedObj.zengShu == this.zengShu && MixedObj.getFenZi() == this.getFenZi() && MixedObj.getFenMu() == this.getFenMu();
}
*/
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/learning-c-language-feng/java_test.git
git@gitee.com:learning-c-language-feng/java_test.git
learning-c-language-feng
java_test
Java_test
master

搜索帮助