Hdu 2522 hash

题目链接

题意:输入整数n (1<= abs(n) <= 10^5) , 输出 1/n.

这题不是自己做出来的...看了网上的思路.这种题目都能想到用hash..反正我是没往那里想,看到循环节我就知道不会了2333

可以手动模拟一下除法的步骤, 对于正整数 n > 1,  被除数初始left = 1, 做除法时, 如果left < n,那么输出0,否则输出left/n, 余数为:left%n, 

left = left * 10. 如果余数已经出现过, 那么说明是第二个循环节的开始,终止循环.

现在想想,出题人真是碉堡了..羡慕嫉妒恨,出个题目太不容易了

附上代码:

 1 /*************************************************************************
 2     > File Name: 2522.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年05月13日 星期二 22时27分32秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 bool hash[100002];
19 
20 void
21 solve(int n) {
22       memset(hash, false, sizeof(hash));
23       int left = 1;
24     hash[1] = true;
25     while (left) {
26           left *= 10;
27         printf("%d", left/n);
28         left %= n;
29         if (hash[left]) {
30               break;
31         } else {
32               hash[left] = true;
33         }
34     }
35 
36     return ;
37 }
38 
39 int
40 main(void) {
41       int T;
42     scanf("%d", &T);
43     while (T--) {
44           int n;
45         scanf("%d", &n);
46         if (n < 0) {
47               printf("-");
48             n *= -1;
49         } 
50       if (n == 1) {
51               printf("1\n");
52             continue;
53         }
54         printf("0.");
55         solve(n);
56         puts("");
57     }
58 
59     return 0;
60 }
61     

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3726706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值