USACO chapter 1 section 1.3 Prime Cryptarithm

USER: tianbing tianbing [tbbd4261]
TASK: crypt1
LANG: C++
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.011 secs, 2928 KB]
Test 2: TEST OK [0.011 secs, 2928 KB]
Test 3: TEST OK [0.022 secs, 2928 KB]
Test 4: TEST OK [0.000 secs, 2928 KB]
Test 5: TEST OK [0.000 secs, 2928 KB]
Test 6: TEST OK [0.011 secs, 2928 KB]
Test 7: TEST OK [0.011 secs, 2928 KB]
All tests OK.

YOUR PROGRAM ('crypt1') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 -------
5
2 3 4 6 8
------- test 2 -------
4
2 3 5 7
------- test 3 -------
1
1
------- test 4 -------
7
4 1 2 5 6 7 3
------- test 5 -------
8
9 1 7 3 5 4 6 8
------- test 6 -------
6
1 2 3 5 7 9
------- test 7 -------
9
1 2 3 4 5 6 7 8 9
Keep up the good work!

Thanks for your submission!

简单就好,没考虑效率;
题目限制:三位数乘以两位数,三位数乘以两位数的个位是个三位数(part1),同样三位数乘以两位数的十位也是一个三位数(part2)。
结果是个四位数,并且所有的数字都在题目给的范围中;
笨的方法:
three数组存所有可能的三位数,two存所有可能的两位数,判断part1 part2 以及最后的结果result即可

 1/*
 2ID:tbbd4261
 3PROG:crypt1
 4LANG:C++
 5*/

 6
 7#include<fstream>
 8#include<vector>
 9using namespace std;
10int a[10]={0};
11
12bool valid(int x,int n)   //判断数字是否合法 
13{
14     for(int i=1; i<=n; i++)
15      if(a[i]==x)return true;
16     return false;
17}

18
19bool ispri(int x,int n)//判断数是否由给定数字组成 
20{
21     while(x)
22     {
23        int i=x%10;
24        if(!valid(i,n))return false;
25        x=x/10;
26     }

27     return true;
28}

29
30int main()
31{
32    ifstream fin("crypt1.in");
33    ofstream fout("crypt1.out");
34    int n,k,i,j,cnt=0;
35    int result;
36    vector<int> three;
37    vector<int> two;
38    fin>>n;
39    for(i=1; i<=n; i++)
40        fin>>a[i];
41    for(i=1; i<=n; i++)
42    for(j=1; j<=n; j++)
43    {
44         two.push_back(a[i]*10+a[j]);
45         for(k=1; k<=n; k++)
46          three.push_back(a[i]*100+a[j]*10+a[k]);
47    }

48    
49    for(i=0; i<three.size(); i++)
50    for(j=0; j<two.size(); j++)
51    {
52     int part1=three[i]*(two[j]%10);
53     int part2=three[i]*(two[j]/10);
54     result=three[i]*two[j];
55     if(result>=1000&&result<10000&&ispri(result,n)
56     &&part1>=100&&part1<=999&&part2>=100&&part2<=999&&ispri(part1,n)&&ispri(part2,n))
57        cnt++;
58    }

59    fout<<cnt<<endl;
60    
61    return 0;
62}

posted on 2010-05-30 15:07 田兵 阅读(146) 评论(0)  编辑 收藏 引用 所属分类: USACO


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


<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(2)

随笔分类(65)

随笔档案(65)

文章档案(2)

ACM

搜索

积分与排名

最新随笔

最新评论

阅读排行榜