Luo Gu: P1149 Match Stick Equation (Popularity -, Violent Enumeration)

topic:

Insert picture description here

Analysis: The largest number: 1. . . 1+0=1.. . . 1

20-6=14。14/2=7 7/2=3

At first, I was frightened. Think about it later, violently enumerate, count the number required for each number, and then enumerate it!

Code:

#include<bits/stdc++.h>
using namespace std;
//单个数字对应 
int cnt[10]={
    
    6,2,5,5,4,5,6,3,7,6};
int A[10000000];
int f_sum(int x)
{
    
    
 if(A[x]!=-1) return A[x];
 string s=to_string(x);
 int sum=0;
 for(int i=0;i<s.size();i++)
 {
    
    
  sum+=cnt[s[i]-'0'];
 }
 A[x]=sum;
 return sum;
}
int main()
{
    
    
 memset(A,-1,sizeof(A));
 int m;
 cin>>m;
 m=m-4;
 //m根棍子,最大拼成的数m个1 18
 int ans=0;
 for(int i=0;i<=1000;i++)
 {
    
    
  int a=i+i;
  if(f_sum(i)+f_sum(i)+f_sum(a)==m) ans++;
  for(int j=i+1;j<=1000;j++)
  {
    
    
   a=i+j;
   if(f_sum(i)+f_sum(j)+f_sum(a)==m) 
   {
    
    
    ans+=2;
    //cout<<i<<' '<<j<<' '<<a<<endl;
   }
  }
  //cout<<"-------"<<endl; 
 }
 cout<<ans;
}

Guess you like

Origin blog.csdn.net/weixin_42721412/article/details/108474773