首页 > 试题广场 >

还是A+B

[编程题]还是A+B
  • 热度指数:6373 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
读入两个小于10000的正整数A和B,计算A+B。需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1。

输入描述:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A B K",相邻两数字有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。


输出描述:
对每个测试用例输出1行,即A+B的值或者是-1。
示例1

输入

1 2 1
11 21 1
108 8 2
36 64 3
0 0 1

输出

3
-1
-1
100

python solution:

while True:
    try:
        a, b, c = input().split()
        if a!="0" or b!="0":
            if int(a[-int(c):]) == int(b[-int(c):]) or c=="0":
                print(-1)
            else:
                print(int(a)+int(b))
    except:
        break
发表于 2017-10-06 22:03:29 回复(0)
#include <iostream>
using namespace std;
int main()
{
	int a = 0, b = 0, k = 0;
	while (cin>>a>>b>>k && 0 != a && 0 != b)
	{
		int tmpA = a, tmpB = b, ans = -1;
		while (k--)
		{
			if (tmpA % 10 != tmpB % 10) { ans = a + b; break; }
			tmpA /= 10; tmpB /= 10;
		}
		cout << ans << endl;
	}
	return 0;
}

发表于 2020-02-19 21:51:04 回复(0)
#include<stdio.h>//末尾k位全相等时输出-1
int main()
{
    int a,b,k,sum,key,aa,bb,i;
    while(scanf("%d%d%d",&a,&b,&k)!=EOF)
    {
        if(a==0&&b==0) break;
        key=0;//0代表相同
        aa=a;bb=b;
          for(i=1;i<=k;i++)//k个位都进行比较
          {
              if(aa%10==bb%10)
              { aa/=10;bb/=10; }//上一位
              else
              { key=1;break;  }//表示不相同
          }
        if(key==0)  sum=-1;
        else        sum=a+b;
        printf("%d\n",sum);
    }
}

编辑于 2020-04-16 14:59:41 回复(1)
这题超级坑比,注意题意指的是在k位以内数不相同的输出相加,都相同的输出-1,样例给人错觉以为只要有相同的位数就输出-1
#include<bits/stdc++.h>
using namespace std;
bool fun(int a,int b,int k){
    for(int i=1;i<=k;i++){
        if(a%10!=b%10)return true;
        a/=10;
        b/=10;
    }
    return false;
}
int main(){
    int a,b,k;
    while(~scanf("%d %d %d",&a,&b,&k)){
        if(a==0&&b==0)break;
        fun(a,b,k)?printf("%d\n",a+b):printf("-1\n");
    }
    return 0;
}


发表于 2020-03-04 17:35:57 回复(0)
#include<stdio.h>
#include<math.h>
int main()
{
    int A,B,k,c,d,t;
    while(scanf("%d %d %d",&A,&B,&k)!=EOF)
    {
        if(A==0&&B==0)
            break;
        double s=pow(10,k); /**int t=pow(10,2)得到的t为99*/
        t=(int)s;
        c=A%t;
        d=B%t;
        if(c==d)
            printf("-1\n");
        else printf("%d\n",A+B);
    }
    return 0;
}

发表于 2017-03-06 12:38:08 回复(0)

好思路,有被惊艳到。@vammm

#include 
using namespace std;
int main()
{    
    int A,B,K;
    while(scanf("%d %d %d",&A,&B,&K)!=EOF&&!(A==0&&B==0))
    {
        if((A-B)%(int)(pow((double)10,K))==0)
            cout<<"-1"<<endl;
        else
            cout<<A+B<<endl;
    }
    return 0;
}
发表于 2021-01-29 19:46:34 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int k = scanner.nextInt();
            System.out.println((a-b)%(Math.pow(10,k))==0?"-1":a+b);
        }
    }
}


发表于 2020-03-14 17:10:46 回复(0)
#include<iostream>
using namespace std;
int main(){
    int a,b,k;
    while(cin>>a>>b>>k){
        if(a==0&&b==0)
            break;
        int num1[8]={0},num2[8]={0},count1=0,count2=0,temp1=a,temp2=b;
        while(temp1>0){//获取a的每一位
            num1[count1++]=temp1%10;
            temp1/=10;
        }
        while(temp2>0){//获取b的每一位
            num2[count2++]=temp2%10;
            temp2/=10;
        }
        bool judge=1;
        for(int i=0;i<k;i++)//比较是否前k位是否相同,judge作为输出结果方式判断
            if(num1[i]!=num2[i]){
                judge=0;
                break;
            }
        if(!judge) cout<<a+b<<endl;
        else cout<<"-1"<<endl;
    }
}

发表于 2020-01-12 15:41:04 回复(0)
#include <stdio.h>
//FBI warning:样例给出的108 8 2有错


int main(void){

    int a,b,k,tempa,tempb,aa,bb;

    while(scanf("%d %d %d",&a,&b,&k) != EOF){

        if(a == 0 && b == 0) return 0;

        else if(a == 0 && b != 0) printf("%d\n",a+b);

        else if(a != 0 && b == 0) printf("%d\n",a+b);

        else{

            tempa = a;

            tempb = b;

            while(tempa && tempb && k){

                aa = tempa % 10;

                bb = tempb % 10;

                if(aa != bb) break;

                else {

                    k --;

                    tempa /= 10;

                    tempb /= 10;

                }

            }

        }

        if(k == 0){

            printf("-1\n");

        }

        else printf("%d\n",a+b);

    }

    return 0;

}

发表于 2019-03-10 19:40:43 回复(0)
try:
    while True:
        num = input().split()
        if num[0] == num[1] == '0':
            break
        digitNum = min(int(num[2]),len(num[0]),len(num[1]))
        if num[0][-digitNum:] == num[1][-digitNum] or digitNum == 0:
            print(-1)
        else:
            print(int(num[0])+int(num[1]))
except Exception:
    pass
编辑于 2018-09-24 22:18:31 回复(0)
# include <stdio.h>
using namespace std;

int main()
{
    int i;
    int a,b,k;
    int tmpa,tmpb;
    while(scanf("%d%d%d",&a,&b,&k)!=EOF)
    {
        int tmpa=a,tmpb=b;
        int flag=0;
        if(a==0&&b==0)  break;
        for(i=0;i<k;i++)
        {
            //printf("%d %d %d\n",i,tmpa,tmpb);
            if(tmpa%10!=tmpb%10)
            {
                flag=1;
                break;
            }
            tmpa=tmpa/10;
            tmpb=tmpb/10;
        }
        if(flag==0) printf("-1\n");
        else    printf("%d\n",a+b);
    }
    return 0;
}
 
发表于 2018-02-25 13:33:18 回复(0)
#include<stdio.h>
#include<math.h>
int main (){//the shorter,the better.
    int A,B,K,t;//末尾K位
    for(;~scanf("%d%d%d",&A,&B,&K)&&(t=pow(10,K),A||B)&&printf("%d\n",A%t==B%t?-1:A+B););
} 
坑爹啊,理解为末尾第K位了
末尾第K位的代码如下:
#include<stdio.h>
int main (){//the shorter,the better.
    int A,B,K;char a[4],b[4];//末尾第K位
    for(;~scanf("%d%d%d",&A,&B,&K)&&(A||B)&&printf("%d\n",K<=4&&a[sprintf(a,"%04d",A)-K]==b[sprintf(b,"%04d",B)-K]?-1:A+B););
}
编辑于 2018-01-14 20:19:18 回复(0)
#include<iostream>
#include<string>
#include<map>
#include<cmath>
using namespace std;
int main()
{    
    int A,B,K;
    while(scanf("%d %d %d",&A,&B,&K)!=EOF)
    {
        if((A-B)%(int)(pow((double)10,K))==0)
            cout<<"-1"<<endl;
        else
            cout<<A+B<<endl;
    }
    return 0;
}

发表于 2018-03-10 09:37:46 回复(2)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()) {
			
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			int k = scanner.nextInt();
			
			if (a == 0 && b == 0) {
				break;
			}
			
			String aString = Integer.toString(a);
			String bString = Integer.toString(b);
			
			String aPost = "";
			String bPost = "";
			
			if (k <= aString.length()) {
				for (int i = aString.length() - k; i < aString.length(); i++) {
					aPost += Character.toString(aString.charAt(i));
				}
			}else {
				aPost = Integer.toString(a);
			}
			
			if (k <= bString.length()) {
				for (int i = bString.length() - k; i < bString.length(); i++) {
					bPost += Character.toString(bString.charAt(i));
				}
			}else {
				bPost = Integer.toString(b);
			}
			
			
			if (aPost.equals(bPost)) {
				System.out.println(-1);
			}else {
				System.out.println(a+b);
			}
		}
	}
}

发表于 2024-03-24 19:19:38 回复(0)
注意两个数的位数小于K的情况
#include <bits/stdc++.h>
using namespace std;

int main(){
	string sa,sb;
	int k;
	while(cin>>sa>>sb>>k)
	{
		int res;
		if(sa=="0"&&sb=="0")	break;
		int asize=sa.size();
		int bsize=sb.size();
		k=min(k,asize);
		k=min(k,bsize);
		if(sa.substr(sa.size()-k)==sb.substr(sb.size()-k))	res=-1;
		else
		{
			res=stoi(sa)+stoi(sb);
		}
		cout<<res<<endl;
	}
	return 0;
}

发表于 2024-03-17 20:22:57 回复(0)
#include <cmath>
#include <iostream>
using namespace std;

int main() {
    int a, b, k;
    while (cin >> a >> b >> k) {
        int power = pow(10, k);     //10^k
        cout << (a % power == b % power ? -1 : a + b) << endl;
    }
    return 0;
}

发表于 2024-03-03 12:03:58 回复(0)
#include <cstdio>
#include <cmath>

int LastK(int n ,int k){ //求末尾K位
    int remain = 0 , K = 0;
    for(int i = 0; i < k; ++i){ //k次
        remain = n%10;
        n /= 10;
        K = K + remain*pow(10,i);
    }
    return K;
}


int main(){
    int a,b,k;
    while(scanf("%d %d %d",&a,&b,&k) != EOF){
        if(a ==0 & b==0) break;

        if(LastK(a,k) == LastK(b,k)){  
            printf("-1\n");
        }else{
            printf("%d\n",a+b);
        }
    }
    return 0;
}

发表于 2023-03-27 22:48:33 回复(0)
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int a, b, k;
    while (cin >> a >> b >> k) {
        if (a == 0 && b == 0) {//如果都是0则结束
            break;
        }
        int dig = pow(10, k);
        if (a % dig == b % dig) {//后k位一样,直接比较求余是不是一样就可以了
            cout << "-1" << endl;
        } else {
            cout << a + b << endl;
        }
    }
}

发表于 2023-03-26 09:58:12 回复(0)
#include <iostream>
using namespace std;

bool check(int a, int b, int k){
    while (k-- > 0){
        if (a % 10 != b % 10) return false;
        a /= 10;
        b /= 10;
    }
    return true;
}

int main() {
    int a, b, k;
    while (cin >> a >> b >> k && a != 0 && b != 0){
        if (check(a, b, k)){
            cout << -1 << endl;
            continue;
        }
        cout << a + b << endl;
    }
}
发表于 2023-03-18 17:59:30 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, m, k;
    while (cin >> n >> m >> k) {
        if (n == 0 && m == 0) {
            break;
        }
        int temp_n = n, temp_m = m;
        int flag = 1;
        while (k--) {
            int ansn = temp_n % 10;
            int ansm = temp_m % 10;
            if (ansn != ansm) {
                flag = 0;
                break;
            }
            temp_n /= 10;
            temp_m /= 10;
        }
        if (flag == 1) {
            cout << -1 << endl;
        } else {
            cout << n + m << endl;
        }
    }
    return 0;
}

发表于 2023-03-07 09:30:35 回复(0)

问题信息

难度:
64条回答 8160浏览

热门推荐

通过挑战的用户

查看代码