高精度问题中去掉末尾无效0的C++算法
仅供参考
#include
<
iostream
>
#include < string >
using namespace std;
int main()
{
string s;
while (cin >> s)
{
int posdot;
posdot = ( int )s.find( '. ' );
int count = ( int )s.size() - 1 ;
while (s[count] == ' 0 ' && count >= posdot)
{
count -- ;
}
s = (s[count] == ' . ' ) ? s.substr( 0 ,count):s.substr( 0 ,count + 1 );
cout << s << endl;
}
system( " PAUSE " );
}
#include < string >
using namespace std;
int main()
{
string s;
while (cin >> s)
{
int posdot;
posdot = ( int )s.find( '. ' );
int count = ( int )s.size() - 1 ;
while (s[count] == ' 0 ' && count >= posdot)
{
count -- ;
}
s = (s[count] == ' . ' ) ? s.substr( 0 ,count):s.substr( 0 ,count + 1 );
cout << s << endl;
}
system( " PAUSE " );
}
测试数据:
200000.0000001110000525554112000
200000.0000001110000525554112
225544444448885.000000
225544444448885
45969464546546546545
45969464546546546545
41654651654984664646.000222219465165
41654651654984664646.000222219465165
1654651654654654654654654.0
1654651654654654654654654