首页 > 试题广场 >

循环数比较

[编程题]循环数比较
  • 热度指数:15491 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
对于任意两个正整数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

Python 解法

x1, k1, x2, k2 = map(int, input().split())
v1, v2 = int(str(x1) * k1), int(str(x2) * k2)
if v1 == v2:
    print("Equal")
elif v1 > v2:
    print("Greater")
else:
    print("Less")
发表于 2019-02-24 18:49:01 回复(4)
#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;
}
思路就是先比较结果哪个位数更多,那个就大;相等时逐位比较,比到两个数的长度的最小公倍数就可以
发表于 2020-03-21 12:35:06 回复(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;
}

发表于 2019-05-14 19:56:44 回复(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;
}

发表于 2019-05-07 13:50:00 回复(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;
}

发表于 2019-02-11 02:13:46 回复(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");

        }

    }

}
编辑于 2018-09-12 21:41:35 回复(1)
不知为啥 感觉还是c/c++方便
#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;
    }
}

发表于 2018-09-11 20:59:55 回复(3)


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");
        }
    }
}

发表于 2020-05-24 14:11:46 回复(0)
Java   看代码
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");
		}
		

	}

}

发表于 2019-11-05 16:12:54 回复(0)
#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;
}
上面是出错的代码,把字符数组tmp1和tmp2的定义修改为char tmp1[11]; char tmp2[11]; 后,即数组长度修改成固定长度(x1,x2最大的数字位数)后,能够通过全部样例
希望有大佬能帮忙解释一下上面出错的缘由,是strcat()我没有使用正确,还是定义数组出现了问题。
另外,这里是出错时使用的样例,以及拼接后所得到的字符串(ch2中带乱码)结果:
输入样例:1010 3 101010 2
期待输出:Equal
实际测试输出:(从上到下依次为(1)拼接后ch1,ch2的长度,(2)比较结果 (3)ch1的内容 (4)ch2的内容)


编辑于 2019-08-11 01:50:20 回复(0)
#include<iostream>
#include<string>

//思路:想到用string来逐个对比,避免了大整数
using namespace std;

int main()
{
    string x1, x2;//这里很关键,直接读进来就是string类型,不然要int转string很麻烦
    int k1, k2;
    cin >> x1 >> k1 >> x2 >> k2;
    //用一个状态来表示比较结果
    enum resultstate{equal=0,greater,less};
    string resultoutput[3] = { "Equal","Greater","Less" };
    int result=equal;

    if (x1.size()*k1 > x2.size()*k2)result=greater;
    else if (x1.size()*k1 < x2.size()*k2)result=less;
    else  //位数相等,逐个比较
    {
        string v1, v2;
        for (int i = 0; i < k1; i++)v1 += x1;
        for (int i = 0; i < k2; i++)v2 += x2;
        //compare
            if (v1 > v2)result = greater;//string可以直接比较
            else if (v1 < v2)result = less;
    }
    cout << resultoutput[result] << endl;

    system("pause");
    return 0;
}


编辑于 2019-04-02 22:11:24 回复(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")

发表于 2019-03-18 19:37:23 回复(0)
Python做这个题有点开挂的感觉

x1, k1, x2, k2 = input().split()
v1 = int(x1 * int(k1))
v2 = int(x2 * int(k2))
if v1 > v2:
    print("Greater")
elif v1 == v2:
    print("Equal")
else:
    print("Less")

发表于 2019-03-18 09:48:21 回复(0)
#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;
}
发表于 2019-01-22 22:54:02 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        try(Scanner in = new Scanner(System.in)){
            String s1 = in.next();
            int count1 = in.nextInt();
            String s2 = in.next();
            int count2 = in.nextInt();
            if(s1.length() * count1 > s2.length() * count2) System.out.println("Greater");
            else if(s1.length() * count1 < s2.length() * count2) System.out.println("Less");
            else System.out.println(helper(s1,count1,s2,count2));
        }
    }
    public static String helper(String s1,int count1,String s2,int count2){
        StringBuffer sb1 = new StringBuffer(s1),sb2 = new StringBuffer(s2);
        for(int i = 1;i < count1;i++){
            sb1.append(s1);
        }
        for(int i = 1;i < count2;i++){
            sb2.append(s2);
        }
        String res1 = sb1.toString(),res2 = sb2.toString();
        if(res1.compareTo(res2) < 0) return "Less";
        else if(res1.compareTo(res2) == 0) return "Equal";
        else return "Greater";
    }
}

发表于 2019-01-15 21:05:16 回复(1)
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";
        }
    }
}

发表于 2018-08-29 09:23:19 回复(1)
我的回答有段错误,可能是有数组越界之类的错误,但我也不知道哪里错了,只能拜托大神帮忙看看了
 #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;
            }
        }
    }
}

发表于 2019-03-03 16:24:32 回复(2)
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");
    }
}

发表于 2019-01-17 13:53:28 回复(3)
def repeat(x,k):
    x=str(x)
    x=x*k
    return int(x)
x1,k1,x2,k2=map(int,input().split())
v1=repeat(x1,k1)
v2=repeat(x2,k2)
if v1<v2:
    print('Less')
elif v1==v2:
    print('Equal')
else:
    print('Greater')

编辑于 2024-04-12 13:39:42 回复(0)
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");
            }
        }
    }
}
发表于 2023-08-17 10:29:20 回复(0)