B. Adding Sevens
链接: http://www.bnuoj.com/v3/contest_show.php?cid=5512#problem/B
%lld Java class name: Main
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
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
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 bothA 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 =Cwhere C is the display code for a + b
Sample Input
010079010+010079=106010+010=BYE
Sample Output
010079010+010079=010106106106010+010=106093
大数相加
ps:英文一直是做区域赛题的瓶颈,不过个人认为要抓住细节和关键点。
题意:按照题目给出的信息计算灯亮的编码序列号,注意是7位编码,如题,灯显示1的时候,必须触及1和3,ans=2^1+2^3=0001010(二进制)=10(十进制)。
灯显示3的时候,必须触及0,3,6,1,2,ans=2^0+2^3+2^6+2^1+2^2=1001111(二进制)=79(十进制)。
原理搞清楚,接下来就是锻炼细节和思维方面了
此题扣了很久,
#include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char ss[110], aa[50], bb[50], AA[15], BB[15];
int str1[128];
char *str2[11] = {"063","010","093", "079", "106", "103","119","011", "127", "107"};
void fun()
{
str1[63] = 0;
str1[10] = 1;
str1[93] = 2;
str1[79] = 3;
str1[106] = 4;
str1[103] = 5;
str1[119] = 6;
str1[11] = 7;
str1[127] = 8;
str1[107] = 9;
}
int main()
{
fun();
while(scanf("%s", ss) && strcmp(ss, "BYE") != 0)
{
// memset(aa, 0, sizeof(aa));
// memset(bb, 0, sizeof(bb));
mem(aa,0);
mem(bb,0);
getchar();
int ans = 0;
int k;
for(k = 0; ss[k] != '+'; ++k)
aa[ans++] = ss[k];
ans= 0;
k++;
for(; ss[k] != '='; ++k)
bb[ans++] = ss[k];
// memset(AA, 0, sizeof(AA));
// memset(BB, 0, sizeof(BB));
mem(AA,0);
mem(BB,0);
int cnt1 = 0, j = 0,temp = 0,i;
for(i = 0; i <= strlen(aa); ++i)//十进制表示
{
if(j == 3)
{
//printf("temp=%d\n", temp);
AA[cnt1++] = str1[temp]+'0';
temp = 0;
j = 0;
}
temp *= 10;
temp += aa[i]-'0';
j++;
}
int cnt2 = 0;
j = 0;
temp = 0;
for(i = 0; i <= strlen(bb); ++i)
{
if(j == 3)
{
// printf("temp=%d\n", temp);
BB[cnt2++] = str1[temp]+'0';
temp = 0;
j = 0;
}
temp *= 10;
temp += bb[i]-'0';
j++;
}
int a1[15], b1[15];
// memset(a1, 0, sizeof(a1));
// memset(b1, 0, sizeof(b1));
mem(a1,0);
mem(b1,0);
int va1 = 0,va2=0;
for(i = strlen(AA)-1; i >= 0; --i)
a1[va1++] = AA[i]-'0';
for(i = strlen(BB)-1; i >= 0; --i)
b1[va2++] = BB[i]-'0';
int vv= va1>va2? va1:va2;
for(i = 0; i < vv; ++i)
a1[i] += b1[i];
int len = vv;
for(i = 0; i < vv; ++i)
{
if(i == vv-1 && a1[i]>=10)
len++;
if(a1[i] >= 10)
{
a1[i+1] += a1[i]/10;
a1[i] %= 10;
}
}
printf("%s+%s=", aa, bb);
for(int i = len-1; i >=0 ; --i)
printf("%s", str2[a1[i]]);
printf("\n");
}
return 0;
}