UVALive 7324 ASCII Addition (模拟)

ASCII Addition

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127407#problem/A

Description


Nowadays, there are smartphone applications that instantly translate text and even solve math problems
if you just point your phone’s camera at them. Your job is to implement a much simpler functionality
reminiscent of the past — add two integers written down as ASCII art.
An ASCII art is a matrix of characters, exactly 7 rows high, with each individual character either
a dot (.) or the lowercase letter ‘x’.
An expression of the form a+b is given, where both a and b are positive integers. The expression is
converted into ASCII art by writing all the expression characters (the digits of a and b as well as the ‘+’
sign) as 7 × 5 matrices, and concatenating the matrices together with a single column of dot characters
between consecutive individual matrices. The exact matrices corresponding to the digits and the ‘+’
sign are as folows:

xxxxx ....x xxxxx xxxxx x...x xxxxx xxxxx xxxxx xxxxx xxxxx .....
x...x ....x ....x ....x x...x x.... x.... ....x x...x x...x ..x..
x...x ....x ....x ....x x...x x.... x.... ....x x...x x...x ..x..
x...x ....x xxxxx xxxxx xxxxx xxxxx xxxxx ....x xxxxx xxxxx xxxxx
x...x ....x x.... ....x ....x ....x x...x ....x x...x ....x ..x..
x...x ....x x.... ....x ....x ....x x...x ....x x...x ....x ..x..
xxxxx ....x xxxxx xxxxx ....x xxxxx xxxxx ....x xxxxx xxxxx .....

Given an ASCII art for an expression of the form a + b, find the result of the addition and write it
out in the ASCII art form.

Input


The input file contains several test cases, each of them as described below.
Input consists of exactly 7 lines and contains the ASCII art for an expression of the form a + b,
where both a and b are positive integers consisting of at most 9 decimal digits and written without
leading zeros.

Output


For each test case, output 7 lines containing ASCII art corresponding to the result of the addition,
without leading zeros.

Sample Input

....x.xxxxx.xxxxx.x...x.xxxxx.xxxxx.xxxxx.......xxxxx.xxxxx.xxxxx
....x.....x.....x.x...x.x.....x.........x...x...x...x.x...x.x...x
....x.....x.....x.x...x.x.....x.........x...x...x...x.x...x.x...x
....x.xxxxx.xxxxx.xxxxx.xxxxx.xxxxx.....x.xxxxx.xxxxx.xxxxx.x...x
....x.x.........x.....x.....x.x...x.....x...x...x...x.....x.x...x
....x.x.........x.....x.....x.x...x.....x...x...x...x.....x.x...x
....x.xxxxx.xxxxx.....x.xxxxx.xxxxx.....x.......xxxxx.xxxxx.xxxxx

Sample Output

....x.xxxxx.xxxxx.xxxxx.x...x.xxxxx.xxxxx
....x.....x.....x.x.....x...x.x.........x
....x.....x.....x.x.....x...x.x.........x
....x.xxxxx.xxxxx.xxxxx.xxxxx.xxxxx.....x
....x.x.........x.....x.....x.....x.....x
....x.x.........x.....x.....x.....x.....x
....x.xxxxx.xxxxx.xxxxx.....x.xxxxx.....x


题意:


用题所示的格式输入 A + B 的式子,要以相同格式输出结果.


题解:


对每个数字存一下,把输入串分割成若干个数字(或'+'),然后暴力判读入的每个数即可.
注意string没有赋初值时候不能直接对某个位置赋值,直接用重定义过的'+'连接字符即可.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

string num[11][7] = {
    {"xxxxx",
     "x...x",
     "x...x",
     "x...x",
     "x...x",
     "x...x",
     "xxxxx"}
     ,
    {"....x",
     "....x",
     "....x",
     "....x",
     "....x",
     "....x",
     "....x",}
     ,
    {"xxxxx",
     "....x",
     "....x",
     "xxxxx",
     "x....",
     "x....",
     "xxxxx"}
     ,
    {"xxxxx",
     "....x",
     "....x",
     "xxxxx",
     "....x",
     "....x",
     "xxxxx"}
     ,
    {"x...x",
     "x...x",
     "x...x",
     "xxxxx",
     "....x",
     "....x",
     "....x"}
     ,
    {"xxxxx",
     "x....",
     "x....",
     "xxxxx",
     "....x",
     "....x",
     "xxxxx"}
     ,
    {"xxxxx",
     "x....",
     "x....",
     "xxxxx",
     "x...x",
     "x...x",
     "xxxxx"}
     ,
    {"xxxxx",
     "....x",
     "....x",
     "....x",
     "....x",
     "....x",
     "....x",}
     ,
    {"xxxxx",
     "x...x",
     "x...x",
     "xxxxx",
     "x...x",
     "x...x",
     "xxxxx"}
     ,
    {"xxxxx",
     "x...x",
     "x...x",
     "xxxxx",
     "....x",
     "....x",
     "xxxxx"}
     ,
    {".....",
     "..x..",
     "..x..",
     "xxxxx",
     "..x..",
     "..x..",
     "....."}
};

int match(string cur[]) {
    for(int i=0; i<10; i++) {
        int flag = 1;
        for(int j=0; j<7; j++) {
            for(int k=0; k<5; k++)
                if(num[i][j][k] != cur[j][k]) {
                    flag = 0; break;
                }
        }
        if(flag) return i;
    }
    return -1;
}

string data[10];
string print[10];

int main(int argc, char const *argv[])
{
    //IN;

    while(cin >> data[0])
    {
        for(int i=1; i<7; i++)
            cin >> data[i];
        int len = data[0].size();
        int n = (len + 1) / 6;

        LL a=0,b=0; int flag = 0;
        int start = 0;
        string cur[10];
        for(int i=1; i<=n; i++) {
            for(int j=0; j<7; j++) {
                cur[j].clear();
                for(int k=start; k<start+5; k++) {
                    cur[j] += data[j][k];
                }
            }
            int dig = match(cur);
            if(dig == -1) {
                flag = 1;
                start += 6;
                continue;
            }
            if(!flag) {
                a = a*10LL + dig;
            } else {
                b = b*10LL + dig;
            }
            start += 6;
        }

        LL Ans = a + b;
        vector<int> ans; ans.clear();
        while(Ans) {
            int di = Ans % 10LL;
            Ans /= 10LL;
            ans.push_back(di);
        }
        int sz = ans.size();

        for(int i=0; i<10; i++) print[i].clear();
        for(int i=sz-1; i>=0; i--) {
            for(int j=0; j<7; j++) {
                for(int k=0; k<5; k++) {
                    print[j] += num[ans[i]][j][k];
                }
                if(i) print[j] += '.';
            }
        }

        for(int i=0; i<7; i++) {
            cout << print[i] << endl;
        }

    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5757847.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值