您的位置:首页 > 其它

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 模拟 acm