zoukankan      html  css  js  c++  java
  • NOIP2008 火柴棒等式

    1.      火柴棒等式

    (matches.pas/c/cpp)

     

    给你 n 根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的 A、B、C 是用火柴棍拼出的整数(若该数非零,则最高位不能是 0)。用火柴棍拼数字 0-9 的拼法如图所示:

     

    注意:

    1. 加号与等号各自需要两根火柴棍
    2. 如果 AB,则 A+B=C 与 B+A=C 视为不同的等式(A、B、C>=0)
    3. n 根火柴棍必须全部用上

     

    【输入】

    输入文件 matches.in 共一行,又一个整数 n(n<=24)。

     

    【输出】

    输出文件 matches.out 共一行,表示能拼成的不同等式的数目。

     

    【输入输出样例1】

    matches.in

    matches.out

    14

    2

     

    【输入输出样例1解释】

    2 个等式为 0+1=1 和 1+0=1。

     

    【输入输出样例2】

     

    matches.in

    matches.out

    18

    9

     

    【输入输出样例2解释】

    9 个等式为:

    0+4=4

    0+11=11

    1+10=11

    2+2=4

    2+7=9

    4+0=4

    7+2=9

    10+1=11

    11+0=11

    【思路】

      枚举+判断。

      题目数据范围很小,可以知道等式中的一个数最大不超过1111,枚举ij判断k=i+j与ij是否满足火柴棍数为n即可。

    【代码】

     1 #include<iostream>
     2 using namespace std;
     3 
     4 const int maxn = 1111;
     5 const int matches[]={6,2,5,5,4,5,6,3,7,6};
     6 int ans=0,n;
     7 int _sum[maxn+5];
     8 
     9 inline int get_sum(int x) {
    10     if(x==0) return matches[0];
    11     int sum=0;
    12     while(x) {
    13         sum+=matches[x%10];
    14         x/=10;
    15     }
    16     return sum;
    17 }
    18 
    19 int main () {
    20     ios::sync_with_stdio(false);
    21     cin>>n;
    22     n-=4;
    23     for(int i=0;i<=maxn;i++) for(int j=0;j<=maxn;j++) {
    24         int k=i+j;
    25         if(get_sum(i)+get_sum(j)+get_sum(k)== n) {
    26             ans++;
    27         }
    28     }
    29     cout<<ans;    
    30     return 0;
    31 }
  • 相关阅读:
    poj 1237 The Postal Worker Rings Once // hoj 1164 The Postal Worker Rings Once
    poj3096Surprising Strings
    Telnet服务的配置2(转)
    浅谈以太网帧格式(转)
    QT for linux 的错误 undefined reference to 'FcFreeTypeQueryFace' 的解决方法(转)
    CString,int,string,char*之间的转换(转)
    sprintf(转)
    CString类(转)
    Linux下telnet服务的配置(转)
    grub删除后的windows恢复(转)
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4859429.html
Copyright © 2011-2022 走看看