1. 程式人生 > >題解:CERC2015 ASCII Addition

題解:CERC2015 ASCII Addition

慶祝通過noip2018初賽,系列五題EP5.

題目描述:

題意翻譯

題目背景

現在,如果你只是用手機的相機對著它們,智慧手機應用可以即時翻譯文字,甚至解決數學問題。您的工作是實現一個更簡單的功能,回憶過去——新增兩個作為ASCII藝術的整數。

題目描述

ASCII藝術是一個字元矩陣,正好是7行高,每個字元都是點或小寫字母X。

給出了A +B形式的表示式,其中A和B都是正整數。通過將所有的表示式字元(A和B的數字以及符號)作為7 5個矩陣,將這些矩陣轉換成ASCII藝術,並將矩陣與單個字元的單個列串聯在連續的各個矩陣之間。對應於數字和+符號的精確矩陣如下:

給定一個ASCII藝術來表達A+B的形式,找到加法的結果並用ASCII藝術形式寫出。

輸入輸出格式

輸入格式:

輸入由7行組成,包含用於A+B形式的表示式的ASCII技術,其中A和B都是由至多9個十進位制數字組成的正整數,並且沒有前導零。

輸出格式:

輸出包含ASCII藝術的7行,對應於加法的結果,沒有前導零。

感謝@劍聖夜雨聲煩 提供的翻譯

題目背景

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 follows:

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 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 7 lines containing ASCII art corresponding to the result of the addition, without leading zeros.

 

輸入輸出樣例

輸入樣例#1:  複製
....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
輸出樣例#1:  複製
....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

說明

樣例:1234567+890=1235457

Central Europe Regional Contest 2015 Problem A

解題思路:

大致是一道模擬題

(慶祝通過noip2018提高組初賽第五題)(五道黑題時代結束,之後開始正常練習)

下面上程式碼:

 

 1#include<bits/stdc++.h>
2#define int long long 
3using namespace std;
4map<string,int> m;
5string st; 
6string s[7];
7int n,row,line,loc,a,b,res,ans,w,ret;
8char out[10][5000];
9bool skip;
10void Init(){
11    st="xxxxxx...xx...xx...xx...xx...xxxxxx";
12    m.insert(pair<string,int>(st,0));
13    st="....x....x....x....x....x....x....x";
14    m.insert(pair<string,int>(st,1));
15    st="xxxxx....x....xxxxxxx....x....xxxxx";
16    m.insert(pair<string,int>(st,2));
17    st="xxxxx....x....xxxxxx....x....xxxxxx";
18    m.insert(pair<string,int>(st,3));
19    st="x...xx...xx...xxxxxx....x....x....x";
20    m.insert(pair<string,int>(st,4));
21    st="xxxxxx....x....xxxxx....x....xxxxxx";
22    m.insert(pair<string,int>(st,5));
23    st="xxxxxx....x....xxxxxx...xx...xxxxxx";
24    m.insert(pair<string,int>(st,6));
25    st="xxxxx....x....x....x....x....x....x";
26    m.insert(pair<string,int>(st,7));
27    st="xxxxxx...xx...xxxxxxx...xx...xxxxxx";
28    m.insert(pair<string,int>(st,8));
29    st="xxxxxx...xx...xxxxxx....x....xxxxxx";
30    m.insert(pair<string,int>(st,9));
31}
32void solve(){
33    for (int i=0;i<7;i++)
34        cin>>s[i];
35    n=s[0].length();
36    n=n/6; skip=true;n++;
37    for (int i=1;i<=n;i++){
38        loc=(i-1)*6;
39        st="";
40        for (int j=0;j<35;j++){
41            row=j/5; line=j%5;
42            st=st+s[row][loc+line];            
43        }
44        if (skip){
45            if (!m.count(st)){
46                skip=false;
47                continue;
48            } 
49            res=m[st];
50            a=a*10+res;
51        } else {
52            res=m[st];
53            b=b*10+res;
54        }
55    }
56    ans=a+b;
57}
58void writeln(string t,int num){
59    loc=(num-1)*5;
60    for (int i=0;i<35;i++){
61        row=i/5;line=i%5;
62        out[row][line+loc]=t[i];
63    } 
64}
65void write(int num){
66    res=1;w=0;
67    while (res<=num){
68        w++;res*=10;
69    }
70    for (int i=1;i<=w;i++){
71        res/=10;
72        ret=num/res;ret%=10;
73        std::map<string,int>::iterator it;
74        for (it=m.begin();it!=m.end();it++)
75            if (it->second==ret)
76                writeln(it->first,i); 
77    }
78    for (int i=0;i<7;i++){
79        for (int j=0;j<(w*5);j++){
80            cout << out[i][j];
81            if (j%5==4&&j!=w*5-1)
82                cout << ".";
83        }
84        cout << endl;
85    }
86}
87main(){
88    Init();
89    solve();
90    write(ans);
91    return 0;
92