zoukankan      html  css  js  c++  java
  • Yet another A + B

    time limit per test
    0.25 s
    memory limit per test
    64 MB
    input
    standard input
    output
    standard output

    You are given three numbers. Is there a way to replace variables A, B and C with these numbers so the equality A + B = C is correct?

    Input

    There are three numbers X1, X2 and X3 (1 ≤ Xi ≤ 10100), each on a separate line of input.

    Output

    Output either "YES", if there is a way to substitute variables A, B and C with given numbers so the equality is correct, or "NO" otherwise.

    Examples
    Input
    1
    2
    3
    Output
    YES
    Input
    1
    2
    4
    Output
    YES
    Input
    1
    3
    5
    Output
    NO

    数据处理一下,用java简单,用c++麻烦点

    c++:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define Max 105
    using namespace std;
    int flag = 0;
    char s[3][Max],ans[3][Max];
    char s1[Max],s2[Max];
    char *add(char *x,char *y)
    {
        if(strlen(x) < strlen(y))
        {
            strcpy(s1,y);
            strcpy(s2,x);
        }
        else
        {
            strcpy(s1,x);
            strcpy(s2,y);
        }
        int d = 0,i = 0;
        for(i = 0;i < strlen(s2);i ++)
        {
            d += s1[i] - '0' + s2[i] - '0';
            s1[i] = d % 10 + '0';
            d /= 10;
        }
        while(s1[i])
        {
            d += s1[i] - '0';
            s1[i ++] = d % 10 + '0';
            d /= 10;
        }
        if(d)
        {
            s1[i ++] = d % 10 + '0';
            d /= 10;
            s1[i] = '';
        }
        return s1;
    }
    void dfs(int k)
    {
        if(flag)return ;
        if(k == 3)
        {
            if(strcmp(add(ans[0],ans[1]),ans[2]) == 0)flag = 1;
            return ;
        }
        for(int i = 0;i < 3;i ++)
        {
            strcpy(ans[k],s[i]);
            dfs(k + 1);
        }
    }
    int main()
    {
        for(int i = 0;i < 3;i ++)
        {
            cin>>s[i];
            reverse(s[i],s[i] + strlen(s[i]));
            for(int j = strlen(s[i]);j >= 1;j --)
            {
                if(s[i][j - 1] != '0')
                {
                    s[i][j] = '';
                    break;
                }
            }
        }
        dfs(0);
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

    java:

    import java.util.Scanner;
    import java.math.BigInteger;;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int flag = 0;
            BigInteger d;
            BigInteger a[] = new BigInteger[3];
            for(int i = 0;i < 3;i ++)
                a[i] = sc.nextBigInteger();
            for(int i = 0;i < 3;i ++) {
                if(flag == 1)break;
                for(int j = 0;j < 3;j ++) {
                    if(flag == 1)break;
                    for(int k = 0;k < 3;k ++) {
                        if(flag == 1)break;
                        d = a[i];
                        if(a[i].add(a[j]).equals(a[k]))flag = 1;
                        a[i] = d;
                    }
                }
            }
            if(flag == 1)System.out.println("YES");
            else System.out.println("NO");
        }
    }
  • 相关阅读:
    SQL GUID和自增列做主键的优缺点
    python __future__ 的几种特性
    数据库中文乱码处理
    Android_Intent意图详解
    Windows Server 2012 R2超级虚拟化之六 Hyper-v Replica 2.0和Live migrations
    如今网站定位,需立足于用户
    Hibernate 数据的批量插入、更新和删除
    paip.提升用户体验---论文本编辑器的色彩方案
    时间管理方法学习
    网站优化:从搜索引擎到社交网络的艰难转变
  • 原文地址:https://www.cnblogs.com/8023spz/p/8029529.html
Copyright © 2011-2022 走看看