hdu2919

来源:互联网 发布:ubuntu server配置网络 编辑:程序博客网 时间:2024/06/17 17:45

Adding Sevens

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 663    Accepted Submission(s): 260


Problem Description
A seven segment display, similar to the one shown on the right, is composed of seven light-emitting elements. Individually on or off, they can be combined to produce 127 different combinations, including the ten Arabic numerals. The figure below illustrates how the ten numerals are displayed. 

7-seg displays (as they're often abbreviated) are widely used in digital clocks, electronic meters, and calculators.


A 7-seg has seven connectors, one for each element, (plus few more connectors for other electrical purposes.) Each element can be turned on by sending an electric current through its pin. Each of the seven pins is viewed by programmers as a single bit in a 7-bit number, as they are more comfortable dealing with bits rather than electrical signals. The figure below shows the bit assignment for a typical 7-seg, bit 0 being the right-most bit. 

For example, in order to display the digit 1, the programmer knows that only bits 1 and 3 need to be on, i.e. the 7-bit binary number to display digit 1 is ``0001010", or 10 in decimal. Let's call the decimal number for displaying a digit, its display code, or just code for short. Since a 7-seg displays 127 different configurations, display codes are normally written using 3 decimal places with leading zeros if necessary, i.e. the display code for digit 1 is written as 010.

In a 9-digit calculator, 9 7-seg displays are stacked next to each other, and are all controlled by a single controller. The controller is sent a sequence of 3n digits, representing n display codes, where 0 < n < 10 . If n < 9 , the number is right justified and leading zeros are automatically displayed. For example, the display code for 13 is 010079 while for 144 it is 010106106

Write a program that reads the display codes of two numbers, and prints the display code of their sum. 
 

Input
Your program will be tested on one or more test cases. Each test case is specified on a single line in the form of A +B = where both A and B are display codes for decimal numbers a and b respectively where 0 < a , b < a + b < 1, 000, 000, 000 . The last line of the input file is the word ``BYE'' (without the double quotes.)
 

Output
For each test case, print A +B =C where C is the display code for a + b .
 

Sample Input
010079010+010079=106010+010=BYE
 

Sample Output
010079010+010079=010106106106010+010=106093
给定a,b求a+b,给定的a,b和要求的a+b每一位都是用题目描述的编码来表示

例如:当需要显示1的时候,需要第1,3根灯亮,则七位二进制表示为0001010 = 2^1+2^3=10,所以用010表示1;//亮用1表示,不亮用0表示,得到的数不足3位前导补0

当需要显示3的时候,需要第0,1,2,3,6根灯亮,则七位二进制表示1001111=2^0+2^1+2^2+2^3+2^6=79,所以用079表示3

......

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=30+10;int hash1[200],hash2[10];char a[MAX],b[MAX];void Map(){hash2[0]=63,hash1[63]=0;hash2[1]=10,hash1[10]=1;hash2[2]=93,hash1[93]=2;hash2[3]=79,hash1[79]=3;hash2[4]=106,hash1[106]=4;hash2[5]=103,hash1[103]=5;hash2[6]=119,hash1[119]=6;hash2[7]=11,hash1[11]=7;hash2[8]=127,hash1[127]=8;hash2[9]=107,hash1[107]=9;}int main(){Map();int lena,lenb,sum=0,i,j,p,temp=1;while(~scanf("%s",a),strcmp(a,"BYE")){i=j=sum=0,temp=1;while(a[i++] != '+');lena=i-1;while(a[i] != '=')b[j++]=a[i++];lenb=j;for(i=lena-3,j=lenb-3;i>=0 && j>=0;i-=3,j-=3){p=(a[i]-'0')*100+(a[i+1]-'0')*10+a[i+2]-'0';sum+=hash1[p]*temp;p=(b[j]-'0')*100+(b[j+1]-'0')*10+b[j+2]-'0';sum+=hash1[p]*temp;temp*=10;}while(i>=0){p=(a[i]-'0')*100+(a[i+1]-'0')*10+a[i+2]-'0';sum+=hash1[p]*temp;temp*=10;i-=3;}while(j>=0){p=(b[j]-'0')*100+(b[j+1]-'0')*10+b[j+2]-'0';sum+=hash1[p]*temp;temp*=10;j-=3;}printf("%s",a);while(temp>sum)temp/=10;while(temp){printf("%03d",hash2[sum/temp]);sum=sum%temp;temp=temp/10;}cout<<endl;}return 0;}