UVa 725 Division
来源:互联网 发布:java lambda 性能 编辑:程序博客网 时间:2024/06/06 01:54
来源:《算法竞赛入门经典(第二版)》例题7-1、UVa 725
题目分析:
UVa题目惯例小心输出格式。这个题是简单的整数枚举。联系 UVa 10976可以发现枚举也是有技巧的,往往只需部分枚举,其他不定元可以通过部分枚举的值推算出来,这道题就是如此。题意本身非常简单了,给一个整数n,求0~9五五分的两个整数u、v,使得成立u / v = n
事先准备好v的所有可能情况
题外话,这个题一开始用c++11特性和字符串处理胡乱装逼跑了610ms,写这篇博客的时候是在不爽全改成了整数版本,变成跑10ms。我还是跑得快的~
// Created by wander on 16/6/1.// Copyright © 2016年 W4anD0eR96. All rights reserved.// UVa725 Division// 类型:枚举-整数枚举#include "bits/stdc++.h"using namespace std;int n, cnt;int EnumList[100050], s;bool Used[10], flag, blank;void Init(int d) { if (d >= 5) { EnumList[cnt++] = s; return; } for (int i = 0; i < 10; i += 1) { if (Used[i]) continue; Used[i] = true; s = s * 10 + i; Init(d + 1); s = s / 10; Used[i] = false; }}void Check(int n, int i) { memset(Used, false, sizeof(Used)); int u = EnumList[i]; int v = n * u; if (v > 98765) return; if (v < 10000) Used[0] = true; if (u < 10000) { if (Used[0]) return; else Used[0] = true; } for (int ucopy = u; ucopy != 0; ucopy /= 10) if (Used[ucopy % 10]) return; else Used[ucopy % 10] = true; for (int vcopy = v; vcopy != 0; vcopy /= 10) if (Used[vcopy % 10]) return; else Used[vcopy % 10] = true; flag = true; printf("%05d / %05d = %d\n", v, u, n);}int main() {#ifdef DEBUG freopen("in", "r", stdin); freopen("out", "w", stdout);#endif // input and pre-process Init(0); while (~scanf("%d", &n) && n) { // work if (!blank) blank = true; else putchar('\n'); flag = false; for (int i = 0; i < cnt; i += 1) Check(n, i); if (!flag) printf("There are no solutions for %d.\n", n); } return 0;}
// Created by wander on 16/5/27.// Copyright © 2016年 W4anD0eR96. All rights reserved.// UVa725 Division// 类型:枚举-整数枚举#include "bits/stdc++.h"using namespace std;int n, cnt;string EnumList[100050], s;bool Used[10], flag, blank;void Init(int d) { if (d >= 5) { EnumList[cnt++] = s; return; } for (int i = 0; i < 10; i += 1) { if (Used[i]) continue; Used[i] = true; s.append(1, '0' + i); Init(d + 1); s.erase(s.end() - 1); Used[i] = false; }}void Check(int n, int i) { string u = EnumList[i]; string v = to_string(n * stoi(u)); set<char> e; if (v.size() > 5) return; if (v.size() <= 4) v += "0"; if (u.size() <= 4) u += "0"; for (int i = 0, sup = (int)u.size(); i < sup; i += 1) if (e.count(u[i])) return; else e.insert(u[i]); for (int i = 0, sup = (int)v.size(); i < sup; i += 1) if (e.count(v[i])) return; else e.insert(v[i]); flag = true; printf("%s / %s = %d\n", v.c_str(), u.c_str(), n);}int main() {#ifdef DEBUG freopen("in", "r", stdin); freopen("out", "w", stdout);#endif // input and pre-process Init(0); while (~scanf("%d", &n) && n) { // work if (!blank) blank = true; else putchar('\n'); flag = false; for (int i = 0; i < cnt; i += 1) Check(n, i); if (!flag) printf("There are no solutions for %d.\n", n); } return 0;}
0 0
- UVA 725 - Division
- UVA 725 - Division
- UVa 725 - Division
- UVA - 725 Division
- UVa 725 - Division
- UVa 725 Division
- UVa 725 - Division
- Uva 725 Division
- UVA-725除法-Division
- UVA - 725 Division
- uva 725 - Division
- Uva - 725 - Division
- UVA - 725 Division
- uva 725 Division
- 除法(Division, UVa 725)
- UVA 725 Division
- UVA 725 - Division
- UVa 725----Division(枚举)
- SICP 练习1.9
- 嵌入式Linux驱动开发之helloword心得
- Oracle中的数值处理相关函数介绍
- PyQt5:网格布局2(14)
- 一些Android sys_call_table获取的方法
- UVa 725 Division
- 从整数1到99之间选出能被3整除、且有一位上的数是5的那些数,并把它们放在p数组中,输出(p数组中)满足条件的数据。
- Javascript学习1-基础知识
- Mac下MySql卸载方法
- PyQt5:表格布局(15)
- bzoj4103【THUSC2015】异或运算
- 从键盘上输入一个数,将其插入到数列{2,5,6,8,12,13,15,17,19,22}中,并保证该数列的有序性。
- 简单的LINUX字符设备驱动及编译进Linux内核
- 解决hp开机"511-cpu fan not detected "错误