对于任意两个正整数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".
1010 3 101010 2
Equal
#include<iostream> (720)#include<cmath> using namespace std; int weishu(int a) //计算x的位数 { int count=0; while(a>0){ a/=10; count++; } return count; } void shuzu(int x,int w,int *n) //将x存为数组 { //int num=weishu(x); int i; for(i=0;i<w;i++) { n[i]=x/pow(10,w-1-i); x=x%(int)pow(10,w-1-i); } } int main(){ int x1,x2,k1,k2,count=0; cin>>x1>>k1>>x2>>k2; int w1=weishu(x1); int w2=weishu(x2); if(w1*k1>w2*k2) cout<<"Greater"<<endl; else if(w1*k1<w2*k2) cout<<"Less"<<endl; else { int n1[9],n2[9]; shuzu(x1,w1,n1); shuzu(x2,w2,n2); int i; for(i=0;i<w1*k1;i++) { if(n1[i%w1]>n2[i%w2]) { cout<<"Greater"<<endl; break; } else if(n1[i%w1]<n2[i%w2]) { cout<<"Less"<<endl; break; } else count++; if(i!=0 && i%w1==0 && i%w2==0) break; } //cout<<count<<" "<<i<<endl; if(count==i+1||count==w1*k1) cout<<"Equal"<<endl; } return 0; }思路就是先比较结果哪个位数更多,那个就大;相等时逐位比较,比到两个数的长度的最小公倍数就可以
#思路,菜鸟解法 数值可能很大,所以先分别比较x1 x2 k1 k2 对于剩下的情况,转化为字符串,然后直接比较字符串 #include <iostream> #include <string> using namespace std; int main() { int x1,k1,x2,k2; cin>>x1>>k1>>x2>>k2; if(x1>=x2&&k1>=k2) { cout<<"Greater"<<endl; } else if(x1==x2&&k1==k2) { cout<<"Equal"<<endl; } else if(x1<=x2&&k1<=k2) { cout<<"Less"<<endl; } else { string s1,s2,mid1,mid2; mid1=to_string(x1); for(int i=0;i<k1;i++) { s1 +=mid1; } mid2=to_string(x2); for(int i=0;i<k2;i++) { s2 +=mid2; } if(s1.size()>s2.size()) { cout<<"Greater"<<endl; } else if(s1.size()==s2.size()) { if(s1>s2) { cout<<"Greater"<<endl; } else if(s1==s2) { cout<<"Equal"<<endl; } else { cout<<"Less"<<endl; } } else { cout<<"Less"<<endl; } } return 0; }
#include<iostream> #include<string> using namespace std; int main(){ int k1, k2; string s1, s2; //用string类型来存储,避免int类型溢出的情况 string S1 = "", S2 = ""; cin >> s1 >> k1 >> s2 >> k2; while(k1--) S1 += s1; //加上重复的次数,存在S1 中,下同 while(k2--) S2 += s2; int len1 = S1.length(); int len2 = S2.length(); if(len1 > len2) //当S1长度大于S2时,可直接判定S1 > S2 cout << "Greater"; else if(len1 == len2){ //相等时,可直接用比较符比较(从第一位,也就是最高位开始比较下去) if(S1 > S2) cout << "Greater"; else if(S1 == S2) cout << "Equal"; else cout << "Less"; }else{ //当S1长度小于S2时,可直接判定S1 < S2 cout << "Less"; } return 0; }
#include <bits/stdc++.h> using namespace std; int LCD(int n, int m){ int r = n*m; if(n<m) swap(n,m); while(n%m){ int t = n%m; n = m; m = t; } return r/m; } int main() { string x1,x2; int k1,k2; while(cin>>x1>>k1>>x2>>k2){ int n = x1.length(); int m = x2.length(); if(n*k1>m*k2) cout<<"Greater"<<endl; else if(n*k1<m*k2) cout<<"Less"<<endl; else{ int i=0,j=0,k=0,r=LCD(n,m); while(k<r){ if(x1[i]>x2[j]){ cout<<"Greater"<<endl; break; }else if(x1[i]<x2[j]){ cout<<"Less"<<endl; break; }else{ i = (i+1)%n; j = (j+1)%m; k++; } } if(k==r) cout<<"Equal"<<endl; } } return 0; }
importjava.math.BigInteger;
importjava.util.Scanner;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scanner scan = newScanner(System.in);
String x1 = scan.next();
intk1 = scan.nextInt();
String x2 = scan.next();
intk2 = scan.nextInt();
StringBuilder sb1 = newStringBuilder();
StringBuilder sb2 = newStringBuilder();
for(inti = 0; i < k1; i++) {
sb1.append(x1);
}
for(inti = 0; i < k2; i++) {
sb2.append(x2);
}
BigInteger bg1 = newBigInteger(sb1.toString());
BigInteger bg2 = newBigInteger(sb2.toString());
if(bg1.equals(bg2)) {
System.out.println("Equal");
}elseif(bg1.compareTo(bg2) < 0) {
System.out.println("Less");
}else{
System.out.println("Greater");
}
}
}
#include <cstdio>#include <string>#include <iostream>usingnamespacestd;voidrepeat(string &, int);int main() {int a, b;string sa, sb;cin >> sa;scanf("%d", &a);cin >> sb;scanf("%d", &b);repeat(sa, a);repeat(sb, b);if(sa.size() > sb.size()) {printf("Greater\n");} else if(sa.size() < sb.size()) {printf("Less\n");} else{if(sa == sb) {printf("Equal\n");} elseif(sa > sb) {printf("Greater\n");} else{printf("Less\n");}}return 0;}void repeat(string &s, int x) {string t = s;for(int i = 0; i < x - 1; ++i) {s += t;}}
import java.util.Arrays;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] x1 = in.next().toCharArray();
int k1 = in.nextInt();
char[] x2 = in.next().toCharArray();
int k2 = in.nextInt();
if ((x1.length * k1) < (x2.length*k2)) {
System.out.println("Less");
// System.exit();
}
else if ((x1.length * k1) > (x2.length*k2))
{
System.out.println("Greater");
//System.exit();
}
else
{
for (int i = 0; i < (x1.length * k1); i++) {
if(x1[i%x1.length]<x2[i%x2.length])
{
System.out.println("Less");
System.exit(0);
}
else if(x1[i%x1.length]>x2[i%x2.length])
{
System.out.println("Greater");
System.exit(0);
}
}
System.out.println("Equal");
}
}
}
import java.util.*; import java.math.BigDecimal; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); long x1=cin.nextLong(); int k1=cin.nextInt(); long x2=cin.nextLong(); int k2=cin.nextInt(); String s=""; String str1=String.valueOf(x1)+s; String str2=String.valueOf(x2)+s; for(int i=2;i<=k1;i++) { str1=str1+String.valueOf(x1)+s; } for(int i=2;i<=k2;i++) { str2=str2+String.valueOf(x2)+s; } BigDecimal big1 = new BigDecimal(str1); BigDecimal big2 = new BigDecimal(str2); int f=big1.compareTo(big2); if(f>0) { System.out.print("Greater"); } else if(f==0) { System.out.print("Equal"); } else { System.out.print("Less"); } } }
#include<bits/stdc++.h> using namespace std; int main(){ int x1,x2,k1,k2; scanf("%d%d%d%d",&x1,&k1,&x2,&k2); char ch1[1500],ch2[1500]; memset(ch1,0,sizeof(ch1)); memset(ch2,0,sizeof(ch2)); sprintf(ch1,"%d",x1); sprintf(ch2,"%d",x2); int len1=strlen(ch1); int len2=strlen(ch2); if(len1*k1<len2*k2){printf("Less\n");} else if(len1*k1>len2*k2){printf("Greater\n");} else{ //原先的代码这里是恰好定义ch1,ch2长度+1(即包含‘\0’)的字符数组 //来存放ch1,ch2字符串,后面便于拼接 //但是利用strcat复制完后输出ch1,ch2会发现ch2中带有乱码, //而且比较的结果也不正确 char tmp1[len1+1]; char tmp2[len2+1]; strcat(tmp1,ch1); strcat(tmp2,ch2); for(int i=1;i<k1;i++) strcat(ch1,tmp1); for(int i=1;i<k2;i++) strcat(ch2,tmp2); int cnt1=0,cnt2=0; //while(ch1[cnt1]!='\0'){cnt1++;} //while(ch2[cnt2]!='\0'){cnt2++;} //printf("\n拼接后ch1和ch2的长度分别为:%d %d\n",cnt1,cnt2); int res=strcmp(ch1,ch2); if(res>0) printf("Greater\n"); else if(res<0) printf("Less\n"); else printf("Equal\n"); //printf("%s\n%s\n",ch1,ch2); } return 0; }
x1,k1,x2,k2=input().split() def repeat(x,k): rep_list=[x for i in range(int(k))] return (''.join(rep_list)) v1=int(repeat(x1,k1)) v2=int(repeat(x2,k2)) if v1<v2: print("Less") elif v1==v2: print("Equal") else: print("Greater")
#include<iostream>
#include<string>
using namespace std;
int main() {
string x1, x2;
int k1, k2;
cin >> x1 >> k1 >> x2 >> k2;
// handle x1
string rep_x1 = x1;
for (int i = 1; i < k1; i++)
rep_x1 += x1;
// hanlde x2
string rep_x2 = x2;
for (int i = 1; i < k2; i++)
rep_x2 += x2;
if (rep_x1.size() == rep_x2.size()) {
if (rep_x1 == rep_x2) cout << "Equal" << endl;
else if (rep_x1 < rep_x2) cout << "Less" << endl;
else cout << "Greater" << endl;
} else if (rep_x1.size() < rep_x2.size()) cout << "Less" << endl;
else cout << "Greater" << endl;
return 0;
}
import java.util.*;public class Main{public static void main(String[] args) {Scanner scan = newScanner(System.in);while(scan.hasNext()) {String x1 = scan.next();int k1 = scan.nextInt();String x2 = scan.next();int k2 = scan.nextInt();String v1 = repeat(x1, k1);String v2 = repeat(x2, k2);System.out.println(compareLong(v1, v2));}}public static String repeat(String x, intk) {String string = "";for(inti = 0; i < k; i++) {string += x;}return string;}public static String compareLong(String x1, String x2) {if(x1.length() > x2.length()) {return "Greater";} elseif(x1.length() < x2.length()) {return "Less";} else{for(inti = 0; i < x1.length(); i++) {if(x1.charAt(i) - '0'< x2.charAt(i) - '0') {return "Less";} elseif(x1.charAt(i) - '0'> x2.charAt(i) - '0') {return "Greater";}}return "Equal";}}}
#include<iostream> using namespace std; int main(){ int x1,k1,x2,k2; cin>>x1>>k1>>x2>>k2; int a[200],b[200],temp=x1,a_count=0,b_count=0; while(temp>0){ a[a_count++]=temp%10; temp/=10; } temp=x2; while(temp>0){ b[b_count++]=temp%10; temp/=10; } if(b_count*k2<a_count*k1) cout<<"Greater"<<endl; else if(b_count*k2>a_count*k1) cout<<"Less"<<endl; else{ for(int i=a_count;i<a_count*k1;i++) a[i]=a[i-a_count]; for(int i=b_count;i<b_count*k2;i++) b[i]=b[i-b_count]; for(int i=a_count*k1-1;i>=0;i--){ if(a[i]<b[i]){ cout<<"Less"<<endl; break; } else if(a[i]>b[i]){ cout<<"Greater"<<endl; break; } if(i==0){ cout<<"Equal"<<endl; } } } }
import java.util.Scanner; public class aqy1 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(); int k1 = sc.nextInt(); int x2 = sc.nextInt(); int k2 = sc.nextInt(); compare(x1, k1, x2, k2); } public static void compare(int x1, int k1, int x2, int k2) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); while (k1-- > 0) sb1.append(x1 + ""); while (k2-- > 0) sb2.append(x2 + ""); int l1 = sb1.length(), l2 = sb2.length(); if (l1 > l2) { System.out.println("Greater"); return; } else if (l1 < l2) { System.out.println("Less"); return; } for (int i = 0; i < l1; ++i) if (sb1.charAt(i) > sb2.charAt(i)) { System.out.println("Greater"); return; } else if (sb1.charAt(i) < sb2.charAt(i)) { System.out.println("Less"); return; } System.out.println("Equal"); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String line = in.nextLine(); //获取整行 String[] input = line.split(" "); //分割 //获取输入 String x1 = input[0]; int k1 = Integer.parseInt(input[1]); String x2 = input[2]; int k2 = Integer.parseInt(input[3]); //拼接后的字符串 StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); //字符串拼接 for (int i = 0; i < k1; i++) { sb1.append(x1); } for (int i = 0; i < k2; i++) { sb2.append(x2); } //先从长度开始比较 if (sb1.length() < sb2.length()) { System.out.println("Less"); } else if (sb1.length() > sb2.length()) { System.out.println("Greater"); } else { //长度相等时,比较每一位的大小,从高位开始比较 boolean flag = false; for (int i = 0; i < sb1.length(); i++) { if (sb1.charAt(i) < sb2.charAt(i)) { System.out.println("Less"); flag = true; break; } else if (sb1.charAt(i) > sb2.charAt(i)) { System.out.println("Greater"); flag = true; break; } } if (!flag) { System.out.println("Equal"); } } } }