欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Hdu 2522 hash

发布时间:2025/6/15 编程问答 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

总结

以上是生活随笔为你收集整理的Hdu 2522 hash的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。