USACO 1.3.3 Prime Cryptarithm
我用的枚举法,即每产生一组数据就判断是否是所给数字里的.
AC还沾沾自喜,但一看题解,发现自己的代码真low...
在平时练习时,应该追求高效,精炼的代码,这样比赛时才能省出大量时间去做其他题!
/*
ID:wang9621
PROG:crypt1
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int cnt[];
int main()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n;
scanf("%d",&n);
for(int i = ;i<=n; i++) scanf("%d",&cnt[i]);
int count = ;
int flag = ;
int flag1 = ;
int flag2 = ;
int cnt1,cnt2;
for(int i = ; i<=n; i++)
{
if(cnt[i]==&&i==) continue;
for(int j = ; j<=n; j++)
{
for(int k = ; k<=n; k++)
{
flag = cnt[i]*+cnt[j]*+cnt[k];
for(int p = ; p<=n; p++)
{
if(cnt[p]==&&p==) continue;
for(int q = ; q<=n; q++)
{
cnt1 = flag1 = flag*cnt[p];
cnt2 = flag2 = flag*cnt[q];
int flag11 = ;
int wei1 = ;
int flag22 = ;
int wei2 = ;
while(flag1)
{
int sou = flag1%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
flag11++;
break;
}
}
wei1++;
flag1 /= ;
}
while(flag2)
{
int sou = flag2%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
flag22++;
break;
}
}
wei2++;
flag2 /= ;
}
if(flag11==&&flag22==&&wei1==&&wei2==)
{
int sum = cnt1+cnt2*;
int cntt = ;
int cnttt = ;
while(sum)
{
int sou = sum%;
for(int s = ; s<=n; s++)
{
if(cnt[s] == sou)
{
cntt++;
break;
}
}
cnttt++;
sum /= ;
}
if(cntt==&&cnttt==)
{
count++;
}
}
}
}
}
}
}
printf("%d\n",count);
return ;
}
我的代码
测试结果
Executing...
Test 1: TEST OK [0.000 secs, 4184 KB]
Test 2: TEST OK [0.000 secs, 4184 KB]
Test 3: TEST OK [0.000 secs, 4184 KB]
Test 4: TEST OK [0.000 secs, 4184 KB]
Test 5: TEST OK [0.011 secs, 4184 KB]
Test 6: TEST OK [0.000 secs, 4184 KB]
Test 7: TEST OK [0.011 secs, 4184 KB] All tests OK.
/*
ID:wang9621
PROG:crypt1
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int cnt[];
bool hash1(int x)
{
while(x)
{
if(!cnt[x%]) return false;
x /= ;
}
return true;
}
int main()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n,x;
int count = ;
scanf("%d",&n);
for(int i = ;i<=n; i++)
{
scanf("%d",&x);
cnt[x] = ;
}
for(int i = ; i<; i++)
{
if(hash1(i))
{
for(int j = ; j<; j++)
{
if(hash1(j))
{
if(i*j<&&i*(j/)<&&i*(j%)<&&hash1(i*(j%))&&hash1(i*(j/))&&hash1(i*j))
{
count++;
}
}
}
}
}
printf("%d\n",count);
return ;
}
高效代码
测试结果 Executing...
Test 1: TEST OK [0.000 secs, 4180 KB]
Test 2: TEST OK [0.000 secs, 4180 KB]
Test 3: TEST OK [0.000 secs, 4180 KB]
Test 4: TEST OK [0.000 secs, 4180 KB]
Test 5: TEST OK [0.000 secs, 4180 KB]
Test 6: TEST OK [0.000 secs, 4180 KB]
Test 7: TEST OK [0.000 secs, 4180 KB]
All tests OK.
USACO 1.3.3 Prime Cryptarithm的更多相关文章
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
- USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- 洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 187通过 234提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 题面错误 题目描述 ...
- l洛谷——P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ...
- 洛谷 P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ...
- USACO Section1.3 Prime Cryptarithm 解题报告
crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- 【USACO题库】1.3.4 Prime Cryptarithm牛式
好久没有发题解了,今天发一个很久很久之前写过得题吧 题目其实莫名的难 但是理解后,原来就是一只纸老虎 题目加工中~~~~(缩短题目) 加工完成:已知数字1-9组成集合的一个子集,求满足题意乘法步骤的情 ...
- 【USACO 1.5】Prime Palindromes
/* TASK: pprime LANG: C++ SOLVE: 枚举数的长度,dfs出对称的数,判断是否在范围内,是否是素数 原来想着枚举每个范围里的数,但是显然超时,范围最大是10^9. 对称的数 ...
- p1211 Prime Cryptarithm
直接深搜+检验. #include <iostream> #include <cstdio> #include <cmath> #include <algor ...
随机推荐
- Java中判断字符串中相同字符的个数
public static int countStr(String str1, String str2) { int counter=0; if (str1.indexOf(str2) == -1) ...
- 获得正在编辑行的数据 esayui datagrid
function getEditRow(datagridId) {//datagridId为table容器的id var input = $('#' + datagridId).parent().fi ...
- JSP基本语法--包含指令<%@include file="路径"%> <jsp:include page>
包含指令,真正改变的地方只有具体内容处: 方法1: 在每个jsp页面(HTML)都包含工具栏,头部信息,尾部信息,具体内容 方法2: 将工具栏,头部信息,尾部信息都分成各个独立的文件,使用的时候直接导 ...
- bfs UESTC 381 Knight and Rook
http://acm.uestc.edu.cn/#/problem/show/381 题目大意:给你两个棋子:车.马,再给你一个n*m的网格,从s出发到t,你可以选择车或者选择马开始走,图中有一些障碍 ...
- IOS传值之代理传值(一)
1.使用代理delegate的方法 2.使用通知Notification的方法 3.KVO等方法 4.block传值 ~~~~~~~~~~~~~~~~ 1.使用代理delegate的方法 #impor ...
- ASP.NET网站限制访问频率
最近做了一个免费发短信的小网站(http://freesms.cloudapp.net/),但发现最近有人破解了我的验证码,以每3秒/条的速度用我的短信服务来发他的广告.更换验证码程序和过滤关键字只是 ...
- 两种方法将oracle数据库中的一张表的数据导入到另外一个oracle数据库中
oracle数据库实现一张表的数据导入到另外一个数据库的表中的方法有很多,在这介绍两个. 第一种,把oracle查询的数据导出为sql文件,执行sql文件里的insert语句,如下: 第一步,导出sq ...
- 封装sdk API 应用
1 #include "QWinApp.h" 2 #include "QGlobal.h" 3 int WINAPI _tWinMain(HINSTANCE h ...
- 第一个前台页面----xflow的页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...
- json对象的简单介绍
1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...