学习心得(code)

superlong@CoreCoder

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  74 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

公告

文字可能放在http://blog.csdn.net/superlong100,此处存放代码

常用链接

留言簿(4)

我参与的团队

搜索

  •  

最新随笔

最新评论

  • 1. re: Poj 1279
  • 对于一个凹多边形用叉积计算面积 后能根据结果的正负来判断给的点集的时针方向?
  • --bsshanghai
  • 2. re: Poj 3691
  • 你写的这个get_fail() 好像并是真正的get_fail,也是说fail指向的串并不是当前结点的子串。为什么要这样弄呢?
  • --acmer1183
  • 3. re: HDU2295[未登录]
  • 这个是IDA* 也就是迭代加深@ylfdrib
  • --superlong
  • 4. re: HDU2295
  • 评论内容较长,点击标题查看
  • --ylfdrib
  • 5. re: HOJ 11482
  • 呵呵..把代码发在这里很不错..以后我也试试...百度的编辑器太烂了....
  • --csuft1

阅读排行榜

评论排行榜

Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.

      * * *
x * *
-------
* * * <-- partial product 1
* * * <-- partial product 2
-------
* * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.

Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.

Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1: N, the number of digits that will be used
Line 2: N space separated digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

5
2 3 4 6 8

OUTPUT FORMAT

A single line with the total number of unique solutions. Here is the single solution for the sample input:

      2 2 2
x 2 2
------
4 4 4
4 4 4
---------
4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1

方法是暴利枚举然后判断 - -!
/*
ID: superlo1
LANG: C++
TASK: crypt1
*/

#include 
<iostream>
using namespace std;

int n, set[10];
bool hash[11];

bool check(int x, int flag)
{
    
int a[4], len = 0;
    
while(x)
    {
        a[
++len] = x % 10;
        x 
= x / 10;
        
if(hash[a[len]] == 0return false;
    }
    
if(len > 3 && !flag) return false;
    
return true;
}

int main()
{
    freopen(
"crypt1.in","r",stdin);
    freopen(
"crypt1.out","w",stdout);
    scanf(
"%d"&n);
    
for(int i = 0; i < n; i ++)    
    {
        scanf(
"%d"&set[i]);
        hash[
set[i]] = 1;
    }
    
int i, j, cnt = 0;
    
for(i = 100; i <= 999; i ++)
    
if(check(i, 0))
    {
        
for(j = 10; j <= 99; j ++)
        
if(check(j, 0&& check( (j % 10* i, 0
        
&& check( (j / 10* i, 0&& check(i * j, 1))
        {
            
//printf("%d %d\n",i,j);
            cnt ++;
        }
    }
    printf(
"%d\n",cnt);
    
//while(1);
}

posted on 2009-08-21 00:42 superlong 阅读(195) 评论(0)  编辑 收藏 引用 所属分类: USACO

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理