4

#yyds干货盘点# 解决名企真题:循环数比较

 2 years ago
source link: https://blog.51cto.com/u_15488507/5474198
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#yyds干货盘点# 解决名企真题:循环数比较

原创

97的风 2022-07-15 09:49:50 博主文章分类:面试题 ©著作权

文章标签 i++ java 代码实现 文章分类 Java 编程语言 阅读数215

1.简述:

描述

对于任意两个正整数x和k,我们定义repeat(x, k)为将x重复写k次形成的数,例如repeat(1234, 3) = 123412341234,repeat(20,2) = 2020.牛牛现在给出4个整数x1, k1, x2, k2, 其中v1 = (x1, k1), v2 = (x2, k2),请你来比较v1和v2的大小。

输入描述:

输入包括一行,一行中有4个正整数x1, k1, x2, k2(1 ≤ x1,x2 ≤ 10^9, 1 ≤ k1,k2 ≤ 50),以空格分割

输出描述:

如果v1小于v2输出"Less",v1等于v2输出"Equal",v1大于v2输出"Greater".

示例1
1010 3 101010 2
Equal

2.代码实现:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
String[] array = a.split(" ");
if(array.length != 4) {
System.out.println("输入不合法! " + array.length);
} else {
StringBuilder sb1 = new StringBuilder();
int k1 = Integer.valueOf(array[1]);
for (int i=0; i< k1; i++) {
sb1.append(array[0]);
}

StringBuilder sb2 = new StringBuilder();
int k2 = Integer.valueOf(array[3]);
for (int i=0; i< k2; i++) {
sb2.append(array[2]);
}

BigInteger v1 = new BigInteger(sb1.toString());

BigInteger v2 = new BigInteger(sb2.toString());
int num = v1.compareTo(v2);
if (num < 0) {
System.out.println("Less");
} else if (num == 0) {
System.out.println("Equal");
} else if (num > 0) {
System.out.println("Greater");
}
}
}
}
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK