大意:将有理数表示为小数循环数,如:
1/3 = 0.(3)
22/5 = 4.4
1/7 = 0.(142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803(571428)
本身很简单,注意小数循环不是靠字符串的比较,而是除到某位时的商和余数比较,如果商和余数相同,则开始重复。不过在做的时候犯了不少错,立此存照:
1.一开始无法在OJ上通过编译,报错itoa找不到,include了stdlib.h之后依然报错,改成_itoa还是报错,无奈上网查了查,原来GNU GCC不支持此函数,必须用sprintf。
2.同样是编译器之间的差别,以前常遇到的是VS2008会自动include如memory.h,而在GCC中不会。这里,strlen函数同样在VS中不用特别引用头文件,而在GCC中需要string.h。
3.2/2=2.0,一开始没注意到这个,输出2,WA。
4.输出每行76个字符,必须把换行也算进去,WA一次。
5.此题和POJ3720类似,3720是要计算1/1,....1/n的循环小数表示中某个数字的出现次数。之前写过code,于是这里就基本重写了一回。结果超时。原因:原来的实现中逐个比较商和余数,为n^2复杂度。改成位置标识数组,就过了。
6.声明标识数组时,在GNU中可以放在函数内部作为局部变量,而在VS2008下不行,一运行就crash。
最后,之前用vector,没去估计最长表示,其实是商*余数,也就是10*quot。所以,用普通数组完全可行。
/* ID: blackco3 TASK: fracdec LANG: C++ */ #include <iostream> #include <string.h> #include <memory.h> #include <vector> using namespace std; #define _max_ 100000 struct t_dpos { int quot, remain ; t_dpos(int a,int b):quot(a),remain(b){}; }; vector<t_dpos> record ; void out_char( char a, int &cp ) { cout << a ; if( (cp++)==75 ) cout << endl , cp=0; } int pos[10][_max_]; /* if I use it in local function , VS2008 just not run into error */ int main() { freopen("fracdec.in", "r", stdin); freopen("fracdec.out", "w", stdout); int nume, deno ; cin >> nume >> deno ; if( !(nume % deno ) ){ cout << nume/deno << ".0" << endl ; return 0 ; } int repeat_pos, is_repeat=false ; memset( pos, 0xff, sizeof(pos) ); for(register int remain=(nume%deno)*10, quot=0 ; remain ; remain*=10){ quot = remain / deno , remain =remain % deno; if( pos[quot][remain]>=0 ){ is_repeat=true, repeat_pos=pos[quot][remain] ; break ; } else pos[quot][remain]=record.size(), record.push_back( t_dpos(quot,remain) ); } char int_str[77]; sprintf( int_str, "%d.", nume/deno ); cout << int_str ; int cp=strlen(int_str); for(int j=0; j<record.size(); j++){ if(is_repeat && j==repeat_pos) out_char( '(', cp ); out_char( '0'+record[j].quot, cp ) ; } if( is_repeat ) cout << ')' << endl ; else if( cp!=75 ) cout << endl ; return 0; }
发表评论
-
USACO Training Section 4.2 Cowcycles
2010-01-31 21:11 882英文原题 中文题译 ... -
USACO Training Section 4.2 Job Processing
2010-01-30 17:31 1130英文原题 中文题译 大意: 有N个工件,每个工件要经 ... -
USACO Training Section 4.2 Drainage Ditches ISAP非递归和多路增广递归
2010-01-29 19:39 1843郁闷。不小心覆盖了重 ... -
USACO Training Section 4.2 The Perfect Stall 匈牙利算法的递归和非递归实现
2010-01-28 21:21 1640英文原题 中文题译 ... -
USACO Training Section 4.1 Cryptcowgraphy 奶牛密码
2010-01-27 20:58 1192英文原题 中文题译 大意: 奶牛们要从农场逃跑 ... -
USACO Training Section 4.1 Beef McNuggets
2010-01-26 21:37 964英文原题 中文题译 大意: 给定N个正整数, ... -
USACO Training Section 4.1 Fence Loops
2010-01-24 20:14 1060英文原题 大意: 农夫布朗的牧场上的篱笆已经失 ... -
USACO Training Section 3.4 Closed Fences
2010-01-23 17:50 1400英文原题 题意 一个 ... -
USACO Training Section 3.4 American Heritage
2010-01-21 23:19 771英文原题 大意:有一个由最多26个大写字母构成的二叉树 ... -
USACO Training Section 3.4 Raucous Rockers
2010-01-21 23:09 794英文原题 大意:有S首歌,要放到D个CD里。每首歌有一个 ... -
USACO Training Section 3.4 Electric Fence
2010-01-21 12:57 966英文原题 大意:给定一个三角形(0,0),(m,n),( ... -
USACO Training Section 3.3 Riding the Fences
2010-01-20 23:38 1203英文原题 中文题译 经典的求欧拉路径:给定最多两个奇 ... -
USACO Training Section 3.3 Shopping Offers
2010-01-19 22:18 911英文原题 中文题译 这是个与日常生活相关的题。商场促销 ... -
USACO Training Section 3.3 A Game
2010-01-19 20:54 1090英文原题 有如下一个双人游戏:N(2 <= N & ... -
USACO Training Section 3.3 Home on the Range
2010-01-19 19:36 770英文原题 中文题译 大意:给定一个01矩阵,计算其中全为 ... -
USACO Training Section 3.3 Camelot
2010-01-19 03:39 1226英文原题 中文题译 ... -
USACO Training Section 3.2 Sweet Butter
2010-01-19 00:10 1043英文原题 中文题译 大意:农场之间有路构成稀疏无向图,若 ... -
USACO Training Section 3.2 Magic Squares
2010-01-18 23:11 927英文原题 中文题译 大意:有人发明了一种有8个块三种变换 ... -
USACO Training Section 3.2 Feed Ratios
2010-01-18 20:52 1304英文原题 中文题译 大意:给出整数a[i][j]和 ... -
USACO Training Section 3.2 Spinning Wheels
2010-01-18 19:37 864英文原题 中文题译 大意:有五个纺车飞轮,每个都有最多五 ...
相关推荐
ACM----USACO Training(解题博客网),提供了USACO Training解题的代码,可以参考一下
自己写的usaco_training带代码。供参考,有一道题是cheat的。自己看吧。
usaco2.4解题报告1
usaco测试数据+标程 usaco的section1到section5的所有测试数据 以及标准程序
usaco解题报告,就是usaco.training.gateway上面的题目全解
USACO题解+代码+翻译,好东西,超级齐全,对大家帮助不小,特别是现在nocow挂了
usaco section2.3--section5.5源程序。。。。。。。。。。。。。。。。
[USACO 1.1.4]破碎的项链.cpp
2.4 Section 2.4 3 Chapter3 3.1 Section 3.1 3.2 Section 3.2 3.3 Section 3.3 3.4 Section 3.4 4 Chapter4 4.1 Section 4.1 4.2 Section 4.2 4.3 Section 4.3 4.4 Section 4.4 5 Chapter5 5.1 Section 5.1 5.2 ...
USACO全部译题 USACO Training Program Gateway
USACO_培训USACO_培训Ride.java-> Gift1.java->
USACO training 教程翻译合集(清晰明确)
USACO培训页面美国计算机奥林匹克训练页2015年6月17日开始
One of the answer of the USACO training exercises.
usaco 合集,包括英文原题和中文译题,测试数据以及答案,很全啊!usaco 合集usaco 合集usaco 合集usaco 合集
usaco历年测试数据
usaco 2010-2011 nov news,喜欢usaco的朋友可以看看
某些USACO题目的答案,很详细,代码清晰结构良好,算法高效易于调试
USACO题集及答案
usaco的总结和心得 包括了对题目的分了和总结 以及对题目的解法概括