码迷,mamicode.com
首页 > 其他好文 > 详细

201/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】

时间:2018-08-01 11:39:52      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:can   lse   close   现在   opened   alt   相加   content   return   

问题 A: A + B 普拉斯

在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下: 
技术分享图片 
现在我们用01来构成这些数字
当宝儿姐输入A + B 时(log10(A)<50,log10(B)<50,且A,B均为正整数),你来计算A+B的和C,并按格式在屏幕上打印C。

输入

每组输入包括两个非负整数 A,B(log10(A)<50,log10(B)<50),已EOF结束输入 

输出

按格式在屏幕中打印C,数字之间相隔三列0。 

样例输入

3 8

样例输出

0010000000100
0110000001100
0010000000100
0010000000100
0010000000100
0010000000100
0111000001110

解析思路:

1、首先按照大数相加,把结果求出来!方法:开两个数组暴力模拟!

2、坑点:最后需要把大数的结果按照正常的顺序输出,并且是转化成01字符串!

3、01串事先存在一个二维字符串中,第一维表示0--9以及空格(三列零),第二维表示每个代表的数字(0--9以及空格)的横坐标,得到具体的横坐标后就一层一层地输出即可!把二维字符串想象成蛋糕,输出时就像横着切蛋糕一样,一层一层地去切!

4、题意,有点瑕疵!还有零的情况吧!比如零加零的情况!


 题解:

技术分享图片
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<string>
  5 #include<vector>
  6 #include<algorithm>
  7 #define ll long long
  8 using namespace std;
  9 #define N 100
 10 
 11 char s1[100],s2[100];
 12 int sum[100];
 13 vector<int>order;
 14 
 15 char num[12][10][10]={
 16      {"01110","10001","10011","10101","11001",
 17 "10001","01110"},
 18     {"00100","01100","00100","00100","00100",
 19 "00100","01110"},
 20 {"01110","10001","00001","00110","01000",
 21                     "10000","11111"},
 22 {"11111","00001","00010","00110","00001",
 23 "10001","01110"},
 24 {"00010","00110","01010","10010","11111",
 25 "00010","00010"},
 26  {"11111","10000","10000","11110","00001",
 27 "00001","11110"},
 28  {"01111","10000","10000","11110","10001",
 29 "10001","01110"},
 30 {"11111","00001","00010","00100","00100",
 31 "00100","00100"},
 32  {"01110","10001","10001","01110","10001",
 33 "10001","01110"},
 34  {"01110","10001","10001","01111","00001",
 35 "00010","11100"},
 36 {"000","000","000","000","000",
 37 "000","000"}};
 38 
 39 void paint( )
 40 {
 41     for(int i=0;i<=6;i++){
 42         for(int j=0;j<(int)order.size();j++){
 43             int x=order[j];
 44             printf("%s",num[x][i]);
 45         }
 46         cout<<endl;
 47     }
 48 }
 49 
 50 
 51 int add(char s1[100],char s2[100]){
 52     int len1=strlen(s1);
 53     int len2=strlen(s2);
 54     int i1=len1-1,i2=len2-1,i3=0;
 55     while(i1>=0&&i2>=0){
 56         sum[i3]+=s1[i1]-0+s2[i2]-0;  //记得这里是加等于!
 57         if(sum[i3]>=10){
 58             sum[i3+1]+=sum[i3]/10;
 59             sum[i3]%=10;
 60         }
 61         i3++,i1--,i2--;
 62     }
 63     while(i1>=0){
 64          sum[i3]+=s1[i1]-0;
 65         if(sum[i3]>=10){
 66             sum[i3+1]+=sum[i3]/10;
 67             sum[i3]%=10;
 68         }
 69          i3++,i1--;
 70     }
 71     while(i2>=0){
 72          sum[i3]+=s2[i2]-0;
 73         if(sum[i3]>=10){
 74             sum[i3+1]+=sum[i3]/10;
 75             sum[i3]%=10;
 76         }
 77          i3++,i2--;
 78     }
 79     while(sum[i3]>=10){  //最终的进位情况!!
 80             sum[i3+1]+=sum[i3]/10;
 81             sum[i3]%=10;
 82             i3++;
 83     }
 84 
 85     if(sum[i3]>0)
 86         return i3+1;
 87     else
 88         return i3;
 89 }
 90 
 91 int main()
 92 {
 93     while(scanf("%s%s",s1,s2)!=EOF){
 94         memset(sum,0,sizeof(sum));
 95         int len3=add(s1,s2);   //计算大数相加
 96 
 97        /* for(int i=0;i<len3;i++){
 98             printf("%d",sum[i]);
 99         }
100         cout<<"*****"<<endl;
101 */
102         order.clear();    //生成次序
103         for(int i=len3-1;i>=0;i--){
104             order.push_back(sum[i]);
105             if(i!=0)
106                 order.push_back(10);
107         }
108         paint();
109     }
110 
111 
112     return 0;
113 }
View Code(用到了vector来存贮结果)

 


201/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】

标签:can   lse   close   现在   opened   alt   相加   content   return   

原文地址:https://www.cnblogs.com/zhazhaacmer/p/9399424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!