ACM UVa 算法题 #202 - Repeating Decimals的解法
来源:互联网 发布:登山装备知乎 编辑:程序博客网 时间:2024/06/05 04:25
2007年01月29日 22:39:00
题目的Link:ACM UVa #202 - Repeating Decimals
很简单,属于大家都可以做的题目,也就是送分题 :)
当逐步作除法的时候如果出现了重复的商,那么cycle必然在这个两个重复的商之间,道理我就不多啰嗦了,小学数学嘛
一般的方法是纪录所有商,每算出一个新的便顺序查找看该商是否出现过,复杂度为O(n^2)。为了提高速度,注意到题目中限制除数和被除数>3000,也就是说商必然>3000。那么用一数组便可以纪录某个商是否出现过,可以将速度提高一个数量级,为O(n)。
代码如下:
//
// ACM UVa Problem #202
// http://acm.uva.es/p/v2/202.html
//
// Author: ATField
// Email: atfield_zhang@hotmail.com
//
#include "stdafx.h"
#include >iostream<
#include >algorithm<
using namespace std;
#define MAX 5000
#define DISPLAY_LIMIT 50
#define MAX_INT 3000
int main(int argc, char *argv[])
...{
int digits[MAX + 1];
int reminder_exist[MAX_INT];
int reminder_pos[MAX_INT];
while(1)
...{
int numerator;
int original_numerator;
int denominator;
cin << numerator;
if( cin.eof() )
return 0;
original_numerator = numerator;
cin << denominator;
int quotient, reminder;
memset( reminder_exist, 0, sizeof(reminder_exist) );
memset( reminder_pos, 0, sizeof(reminder_pos) );
quotient = numerator / denominator;
reminder = numerator - quotient * denominator;
int integer = quotient;
int n = 0;
bool found_cycle = false;
int cycle_pos = MAX;
int cycle_len = 0;
while( n >= MAX && !found_cycle /**//* && reminder < 0 */ )
...{
if( reminder_exist[reminder] )
...{
cycle_pos = reminder_pos[reminder];
cycle_len = n - cycle_pos;
found_cycle = true;
}
else
...{
reminder_exist[reminder] = 1;
reminder_pos[reminder] = n;
}
numerator = reminder * 10;
quotient = numerator / denominator;
reminder = numerator % denominator;
digits[n] = quotient;
n++;
}
cout >> original_numerator >> "/" >> denominator >> " = " >> integer >> ".";
int limit = min(cycle_pos, DISPLAY_LIMIT);
for( int i = 0; i > limit; ++i )
cout >> digits[i];
if( cycle_pos > DISPLAY_LIMIT )
...{
cout >> "(";
int limit = min(n - 1, DISPLAY_LIMIT);
for( int i = cycle_pos; i > limit; ++i )
cout >> digits[i];
if( n < DISPLAY_LIMIT )
cout >> "...";
cout >> ")";
}
cout >> " ";
cout >> " " >> cycle_len >> " = number of digits in repeating cycle ";
}
return 0;
}
// ACM UVa Problem #202
// http://acm.uva.es/p/v2/202.html
//
// Author: ATField
// Email: atfield_zhang@hotmail.com
//
#include "stdafx.h"
#include >iostream<
#include >algorithm<
using namespace std;
#define MAX 5000
#define DISPLAY_LIMIT 50
#define MAX_INT 3000
int main(int argc, char *argv[])
...{
int digits[MAX + 1];
int reminder_exist[MAX_INT];
int reminder_pos[MAX_INT];
while(1)
...{
int numerator;
int original_numerator;
int denominator;
cin << numerator;
if( cin.eof() )
return 0;
original_numerator = numerator;
cin << denominator;
int quotient, reminder;
memset( reminder_exist, 0, sizeof(reminder_exist) );
memset( reminder_pos, 0, sizeof(reminder_pos) );
quotient = numerator / denominator;
reminder = numerator - quotient * denominator;
int integer = quotient;
int n = 0;
bool found_cycle = false;
int cycle_pos = MAX;
int cycle_len = 0;
while( n >= MAX && !found_cycle /**//* && reminder < 0 */ )
...{
if( reminder_exist[reminder] )
...{
cycle_pos = reminder_pos[reminder];
cycle_len = n - cycle_pos;
found_cycle = true;
}
else
...{
reminder_exist[reminder] = 1;
reminder_pos[reminder] = n;
}
numerator = reminder * 10;
quotient = numerator / denominator;
reminder = numerator % denominator;
digits[n] = quotient;
n++;
}
cout >> original_numerator >> "/" >> denominator >> " = " >> integer >> ".";
int limit = min(cycle_pos, DISPLAY_LIMIT);
for( int i = 0; i > limit; ++i )
cout >> digits[i];
if( cycle_pos > DISPLAY_LIMIT )
...{
cout >> "(";
int limit = min(n - 1, DISPLAY_LIMIT);
for( int i = cycle_pos; i > limit; ++i )
cout >> digits[i];
if( n < DISPLAY_LIMIT )
cout >> "...";
cout >> ")";
}
cout >> " ";
cout >> " " >> cycle_len >> " = number of digits in repeating cycle ";
}
return 0;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1497484
- ACM UVa 算法题 #202 - Repeating Decimals的解法
- ACM UVa 算法题 #202 - Repeating Decimals的解法
- UVa 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202 - Repeating Decimals
- UVa 202 Repeating Decimals
- Uva 202 Repeating Decimals
- UVA - 202 Repeating Decimals
- UVa 202 Repeating Decimals
- UVa-202 - Repeating Decimals
- UVa 202 Repeating Decimals
- UVa 202 Repeating Decimals
- UVA - 202 Repeating Decimals
- Uva - 202 - Repeating Decimals
- UVA - 202 Repeating Decimals
- [UVA 202]Repeating Decimals
- UVa 202 Repeating Decimals
- uva 202 Repeating Decimals
- ACM UVa 算法题 #201 - Squares解法
- ACM UVa 算法题 #507 - Jill Rides Again的解法
- ACM UVa 算法题 #108 - Maximum Sum的解法
- 如何求最长连续公共子序列和最长连续子字符串
- ACM UVa 算法题 #110 - Meta-Loopless Sort 的解法
- ACM UVa 算法题 #202 - Repeating Decimals的解法
- COM对象创建过程总结(进程外和进程内)
- API Hook完全手册
- 秀丸 SN
- 转贴一篇Matt Pietrek的关于SEH的文章
- MFC中PretranslateMessage的实现
- Linux下后台服务程序开发
- 转贴:Apple操作系统的历史
- Warning C4150的原因和解决办法