noip2018 Matchstick equation

The following excerpt from the topic Los Valley p1149

N matchsticks you, you can spell the number shaped as " A = B + C A + B = C" of the equation? Equation A A, B B, C C is an integer with a match stick spell (if the number of non-zero, then the most significant bit is not 0 0). Fight with a match stick figures 0-9 0 - spelling as shown in Figure 9:

note:

  1. Plus their needs and equal sign two matchsticks

  2. If A B ≠ A B, then A = B + C A + B = C and B = C A + B + A = C as different equations ( A, B, C> 0 = A , B , C > = 0)

  3. n (<= 24) matchsticks must spend all.

  At first glance this question a little ignorant, the number of the first match 0-9 with an array of digital need to save up, but entangled in the range of numbers needed to find, seemingly hundreds of numbers can all be composed of encounter this of course is able to find a range of bigger is better, but also in the analysis, the number of matches required for 1000 is 20, n is 24 maximum, minus the four symbols needed matches, just 20, so I opened to 1000.

  There is also a place to count the number of matches is how each number takes a few matches, play table seemingly not realistic, so every time the number of violent enough, continue to take determined bits%, plus direct each of the bits is the total number of matches, if there is this number itself is 0, then directly back 6 on it.

Code

  

#include<iostream>
#include<cstdio>
using namespace std;
int n;
int l[10]={6,2,5,5,4,5,6,3,7,6};
int ans;
int num(int x)
{
    int a=0,b=0;
    if(x==0)return 6;
    while(x)
    {
        b=x%10;a+=l[b],x/=10;
    }
    return a;
}
int main()
{
    cin>>n;
    n-=4;
    for(int i=0;i<=1000;++i)
        for(int j=0;j<=1000;++j)
        {
            int a=num(i),b=num(j),c=num(i+j);
            if(a+b+c==n)ans++;
        } 
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/yuelian/p/11183644.html
Recommended