uva 188 Perfect Hash(进制转换 + 遍历)
来源:互联网 发布:手机淘宝店头像怎么换 编辑:程序博客网 时间:2024/05/17 23:10
题目大意:给出一个字符串, 包含n个单词,每个单词对应为一个32进制的数, 要求找出一个数使得任意 1 <= i, j <= n, 都满足 (c / w[i] )% n != (c / w[j]) % n(i != j), 如果不满足, c = min((c / w[i] + 1) * w[i], (c / w[j] + 1) *w[j]).
解题思路:先将单词转换为十进制的数, 然后按照公式逐一遍历。
#include <stdio.h>#include <string.h>const int N = 10005;int n, flag, order, w[N];int min(int a, int b) { return a < b ? a : b;}int judge(int cur) { for (int i = 0; i <= n; i++)for (int j = i + 1; j <= n; j++) { if (w[i] == 0 || w[j] == 0)continue; if ((cur / w[i]) % (n + 1) == (cur / w[j]) % (n + 1))return min((cur / w[i] + 1) * w[i], (cur / w[j] + 1) * w[j]); } flag = 1; return cur;}void handle(char str[]) { n = 0; memset(w, 0, sizeof(w)); int len = strlen(str); for (int i = 0; i < len; i ++) {if (str[i] == ' ') n++; else w[n] = w[n] * 32 + (str[i] - 'a' + 1); }}int main() { char str[N]; while (gets(str)) {handle(str);flag = 0;order = 999999999;for (int i = 0; i <= n; i ++) { if (order > w[i])order = w[i];}while (!flag) { order = judge(order);}puts(str);printf("%d\n\n", order); } return 0;}
- uva 188 Perfect Hash(进制转换 + 遍历)
- UVA - 188 Perfect Hash(hash)
- Uva 188 Perfect Hash
- UVa 188 - Perfect Hash
- uva 188 - Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 - Perfect Hash
- UVA - 188 Perfect Hash
- uva 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA - 188 Perfect Hash
- Uva--188 Perfect Hash
- uva 188 - Perfect Hash 模拟
- 188 - Perfect Hash
- JSP生成验证码
- 开发技巧汇总
- Java学习之路:不走弯路,就是捷径
- 整数划分问题
- hdu_2544 最短路 (floyd算法)
- uva 188 Perfect Hash(进制转换 + 遍历)
- 关于多个div中table的td对齐问题
- 今天碰到Android2.3.3能运行的法度到android4.0中无法运行的景象
- poj-3045 Cow Acrobats
- 基于IPagedList 的 Asp.Net MVC3 分页
- tomcat数据源配置
- ImageButton设置背景透明后,点击显示button按下效果
- 线程中的小问题
- android程序自动适应屏幕大小