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

Analyze:

  目前遇到过最恶心的模拟题之一。训练赛听说只有我是用char肝的我瞬间冒冷汗。各自边界计算弄得我头都大了。一个恶心点是此题给出的样例只有一个,但是特么自己写样例麻烦的一批。这里我提供一个自己写的样例:

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

加上题目的样例都能过的话那就基本没问题了。

里面digits的算法是通过方程得到的。字符(数字或加号)的宽度是5,假设有n个字符,那么宽度就是5n;n个字符间有n-1个'.',所以行的总长度就是5n+n-1 = 6*n-1 = len,推出n=(len+1)/6

Code

#include <cstdio>
#include <cstring>
#include <cmath>
struct Num{
const char* n[7]; //每个结构体内部是一个储存7行字符串指针的数组
}nums[11]={
{
"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..",
"....."
}
};
char ccin[7][120]; // 保存输入的式子
char ccout[7][120]; // 保存最后输出的式子
int cmp(int index){ // 判断对应方块的数字是否在预设数字里
for(int m = 0;m < 10;m ++){
bool ok = true;
for(int i = 0;i < 7;i ++)
for(int j = 0;j < 5;j ++)
if(nums[m].n[i][j]!=ccin[i][j+index]){ ok=false; break;}
if(ok) return m;
}
return -1; // 如果没找到说明此方块内是加号,返回-1
}
int ccount(int sum){ //计算和的位数
int c = 0;
while(sum){
sum /= 10; ++c;
}
return c;
}
int main()
{
while(~scanf("%s",ccin[0])){
for(int i = 1;i < 7;i ++) scanf("%s",ccin[i]);
int len = strlen(ccin[0]);
int digits = (len+1)/6; // 计算有多少个数
int sum = 0,mul = 1,i;
for(i = 0;i < digits;i ++){
int bit = cmp(len-1-(4+i*6));
if(bit<0){ len = len-1-(4+i*6)-1; break; }
sum += bit*mul; // 累加
mul *= 10;
}
digits -= i + 1; mul = 1;
for(i = 0;i < digits;i ++){
int bit = cmp(len-1-(4+i*6));
sum += bit*mul;
mul *= 10;
}
int final = ccount(sum);
for(i = 0;i < final;i ++){
int e = 1;
for(int l = 0;l < final - 1 - i;l ++)
e*=10;
int bit = sum / e;
sum-=e*bit;
for(int j = 0;j < 7;j ++){
for(int k = 0;k < 5;k ++)
ccout[j][k+i*6] = nums[bit].n[j][k];
if(i!=final-1) ccout[j][(i+1)*6-1]='.';
}
}
for(i = 0;i < 7;i ++)
printf("%s\n",ccout[i]);
memset(ccin,0,sizeof(ccin)); memset(ccout,0,sizeof(ccout));
}
return 0;
}

[C++]UVaLive7324 ASCII Addtion的更多相关文章

  1. SQL Server 中怎么查看一个字母的ascii编码或者Unicode编码

    参考文章:微信公众号文章 在sql中怎么查看一个字符的ascii编码,so easy !! select ASCII('a') SELECT CHAR(97) charNum SELECT UNICO ...

  2. perl 如何匹配ASCII码以及ASCII码转换

    匹配ASCII码:   /[:ascii:]/ ASCII码转换为数字: ord() 数字转换为ASCII码: chr()

  3. 常用ASCII CHR碼對照

    因為開發需求,把對照表留下來一下. Chr(0) Null Chr(29) 分组符 Chr(38) & Chr(48) 0 Chr(8) 退格 Chr(30) 記錄分離符號 Chr(39) ‘ ...

  4. ascii、unicode、utf、gb等编码详解

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节".再后来,他们又做了一些可以处理这 ...

  5. ASCII码而已

    题目: \u5927\u5bb6\u597d\uff0c\u6211\u662f\u0040\u65e0\u6240\u4e0d\u80fd\u7684\u9b42\u5927\u4eba\uff01 ...

  6. python2.7 报错(UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128))

    报错: 原来用的python3.5版本后来改为2.7出现了这个错误里面的中文无法显示 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 ...

  7. ASCII码、Unicode码 转中文

    ASCII码.Unicode码 转中文 在最近工作中遇到了一些汉字编码转换的处理,可以通过正则表达式及转换字符来实现转成中文 Unicode转换示例 通常为10位编码, 通过digit参数传入 pri ...

  8. ASCII字符集中的功能/控制字符

       ASCII字符集中的功能/控制字符     Function/Control Code/Character in ASCII Version: 2011-02-15 Author: gree ...

  9. ASCII和16进制对照表

    十六进制代码 MCS 字符或缩写 DEC 多国字符名 ASCII 控制字符 1 00 NUL 空字符 01 SOH 标题起始 (Ctrl/A) 02 STX 文本起始 (Ctrl/B) 03 ETX ...

随机推荐

  1. 浅谈SQL Server数据内部表现形式

    在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...

  2. 如莲开发平台(MIS基础框架、Java技术、B/S结构)

    关于     「如莲」是一套MIS类系统基础框架,主要用于各类“管理信息系统”的开发,也适合做网站后台开发.可省去开发时的框架搭建.规范约定.权限管理等基础工作,直接专注于业务功能实现.     「如 ...

  3. scrapy formRequest 表单提交

    scrapy.FormRequest 主要用于提交表单数据 先来看一下源码 参数: formdata  (dict or iterable of tuples) – is a dictionary ( ...

  4. pyhton从开始到光棍的day11

    纪念我这个开始学python的光棍天,光棍天依旧是函数,这次的函数有个小高级,比昨天的low函数稍微高级点,就是在使用函数中是可以赋值的,比如索引一个值什么的.函数还可以当做参数进行传递,把这个函数名 ...

  5. C#使用异步操作时的注意要点(翻译)

    异步操作时应注意的要点 使用异步方法返回值应避免使用void 对于预计算或者简单计算的函数建议使用Task.FromResult代替Task.Run 避免使用Task.Run()方法执行长时间堵塞线程 ...

  6. Django auth认证

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...

  7. url 的正则表达式:path-to-regexp

    概述 该工具库用来处理 url 中地址与参数,能够很方便得到我们想要的数据. js 中有 RegExp 方法做正则表达式校验,而 path-to-regexp 可以看成是 url 字符串的正则表达式. ...

  8. ccpc杭州站 赛后总结

    Ccpc杭州站赛后总结 2017年11月4号五号,我参加了ccpc杭州站的比赛,我的队友是聂少飞和王艳,在4号一点半,举行了比赛开幕式,听着教练代表的发言,听着参赛选手代表的发言,听着志愿者的发言,都 ...

  9. git添加秘钥提示Key is already in use

    种种原因,需要修改git账号的秘钥. 操作如下: 1.删除系统上.ssh下的known_hosts文件  (一般在这个路径下C:\Users\Administrator\.ssh)如果账号不是Admi ...

  10. C++ WMI获取系统硬件信息(CPU/DISK/NetWork etc)

    官网找到一个例子,根据例子修改下可以获取很多信息 #define _WIN32_DCOM #include <iostream> using namespace std; #include ...