Hdu 2522 hash
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net url重写相关技术问题整
- 下一篇: zabbix服务无法启动