51Nod - 1035 模拟
2017-01-18 10:36
148 查看
题意:
正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数。1/6= 0.1(6) 循环节长度为1
1/7= 0.(142857) 循环节长度为6
1/9= 0.(1) 循环节长度为1
Input
输入n(10 <= n <= 1000)
Output
输出<=n的数中倒数循环节长度最长的那个数
Input示例
10
Output示例
7
思路:
模拟题,模拟除法运算,因为除数范围在[0,1000],所以可以开一个数组Hash来保存,每个数之前一次遇到的位置,如果重复遇到就说明这里就是循环节。代码:
#include <bits/stdc++.h> using namespace std; const int MAXN = 10005; int Hash[MAXN], ans[MAXN]; int cal(int x) { int y = 1, res = 0; while (true) { while (y < x) {y *= 10; res++;} if (Hash[y] != -1) return res - Hash[y]; else Hash[y] = res; y %= x; if (!y) break; } return 0; } void solve() { int Max = 0; for (int i = 1; i <= 1000; i++) { ans[i] = ans[i - 1]; memset(Hash, -1, sizeof(Hash)); int tmp = cal(i); //if (i <= 100) printf("%d : %d\n", i, tmp); if (tmp >= Max) { Max = tmp; ans[i] = i; } } } int main() { solve(); int n; scanf("%d", &n); printf("%d\n", ans ); return 0; }
相关文章推荐
- 51nod 1035 最长的循环节【模拟除法】
- 字符串 模拟 Spell checker (poj 1035)
- 51Nod 1035 最长的循环节
- HDU1035 - Robot Motion (模拟)
- POJ 1573 && hdu 1035 Robot Motion(简单模拟)
- HDU 1035 Robot Motion(水题,模拟)
- 51nod-1562:玻璃切割(O(n)模拟)
- HDOJ1035 Robot Motion 【模拟】
- 51nod--1185 威佐夫游戏 V2 (博弈, 乘法模拟)
- 51nod 1035 最长的循环节
- hdu 1035很简单的一个模拟
- POJ 1035 Spell checker (模拟)
- POJ 1573 && hdu 1035 Robot Motion【简单模拟】
- 51nod-1391-01串(O(n)模拟)
- hdoj 1035 Robot Motion (DFS+模拟)
- 51nod 1572宝岛地图(动态规划预处理+模拟)
- 51Nod - 1432 模拟 + 贪心
- 51nod - 1035 最长的循环节 - 数论
- 51nod 1035 最长的循环节 数学
- 51nod 1035:最长的循环节