zoukankan      html  css  js  c++  java
  • 【USACO 1.3.4】牛式

    【題目描述 】

    下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。

            * * *
        x     * *
       ----------
            * * *
          * * *
       ----------
          * * * *
    

    数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。

    注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.

    写一个程序找出所有的牛式。

    【格式】

    INPUT FORMAT:

    (file crypt1.in)

     Line 1:数字的个数n。
    
     Line 2:N个用空格分开的数字(每个数字都属于{1,2,3,4,5,6,7,8,9})。
    

    OUTPUT FORMAT:

    (file crypt1.out)

     共一行,一个数字。表示牛式的总数。

    【分析】

    直接模擬就可以了,其實如果它加上0的話會麻煩一點的。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <cstring>
     5 #include <algorithm>
     6 const int maxl=20000+10;
     7 using namespace std;
     8 bool allow[11];
     9 void solve();
    10 bool check(int t);
    11 int main()
    12 {
    13     int i,n;
    14     //文件操作 
    15     freopen("crypt1.in","r",stdin);
    16     freopen("crypt1.out","w",stdout);
    17     memset(allow,0,sizeof(allow));
    18     scanf("%d",&n);
    19     for (i=1;i<=n;i++)
    20     {
    21         int temp;
    22         scanf("%d",&temp);
    23         allow[temp]=1;
    24     }
    25     solve();
    26     return 0;
    27 }
    28 void solve()
    29 {
    30      int a,b,cnt=0;
    31      for (a=111;a<=999;a++)
    32      for (b=11;b<=99;b++)
    33      {
    34          if (a*b>9999) continue;
    35          if (a*(b%10)>999) continue;
    36          if (a*(b/10)>999) continue;
    37          if (!check(a) || !check(b) || !check(a*b) || !check(a*(b%10)) || !check(a*(b/10))) continue;
    38          cnt++;
    39      }
    40      printf("%d",cnt);
    41 }
    42 bool check(int t)
    43 {
    44      while (t!=0)
    45      {
    46            int c=t%10;
    47            if (allow[c]==0) return 0;
    48            t=t/10;
    49      }
    50      return 1;
    51 }


  • 相关阅读:
    09 shell脚本程序练习
    springboot整合vue03-创建springboot工程
    springboot整合vue02-创建前端页面
    springboot整合vue01-创建vue工程
    pe工具04-获取数据目录
    pe工具03-获取节表信息
    pe工具02-解析pe头信息
    pe工具01-获取进程和模块
    以挂起方式创建进程
    进程
  • 原文地址:https://www.cnblogs.com/hoskey/p/3782751.html
Copyright © 2011-2022 走看看