Problem Title: Octal Fractions
Algorithm: Other
Problem Source: ACM/ICPC South Africa 2001
The State On OJ: Time limit: 1 Seconds Memory limit: 32768K Total Submit: 1249 Accepted Submit: 653
The Submit Result: 2737139 2008-01-29 01:02:09 Accepted 1086 C++ 00:00.00 840K princetonboy
Process:
题目分析:
初看此题感觉非常麻烦,还要用到高精度运算. 后来仔细一想恍然大悟,只需要积累做除法就可以了. 例子: 0.75 = (5/8 + 7) / 8. 从最后一位向前做除运算,累加以后除以8,一直做最后就能得到结果. 程序实现起来也很简单.
My Code:
#include<iostream>
#include<string>
#include<vector>
using namespace std ;
int main()
{
string str ;
vector <int> v ;
while(cin >> str)
{
if(cin.fail()) break ;
str = str.substr(2) ;
v.clear() ;
for(int i = (int) str.size()-1 ; i >= 0 ; i --)
{
int m , n = str[i] - 48 ;
int len = (int) v.size() ;
for(int j = 0 ; j < len ; j ++)
{
m = n*10 + v[j] ;
v[j] = m / 8 ;
n = m % 8 ;
}
while(n != 0)
{
n *= 10 ;
v.push_back(n/8) ;
n %= 8 ;
}
}
cout << "0." << str << " [8] = 0." ;
for(int i = 0 ; i < (int) v.size() ; i ++)
cout << v[i] ;
cout << " [10]" << endl ;
}
return 0 ;
}