http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=1435
题意:
求大数除法和求模。第一个数任意长,也就是可以比longlong长。其实就是高精度数除以long long。
解题:
第一个数用string输入,第二个数用long long。模拟小学数学,把结果算好并保存。求除法的过程最后模也求出来了。
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;
// #define LOCAL_TEST
const int MAXN = 10000;
int ChToNum(char ch)
{
return ch - '0';
}
char NumToCh(int i)
{
return i + '0';
}
int main()
{
#ifdef LOCAL_TEST
freopen("f:\\in.txt", "r", stdin);
freopen("f:\\out.txt", "w+", stdout);
#endif
string strNumA;
string sign;
long long numB;
while ( cin >>strNumA >>sign >>numB )
{
long long t;
long long tmp;
int div[MAXN];
int iCount;
t = 0;
tmp = 0;
memset(div, 0, sizeof(div));
iCount = 0;
for ( int i=0; i<strNumA.length(); i++ )
{
t = ( t*10 + ChToNum(strNumA[i]) );
tmp = t / numB;
if ( iCount==0)
{
if ( tmp > 0 )
{
div[iCount] = tmp;
iCount++;
} // end if
} // end if
else
{
div[iCount] = tmp;
iCount ++;
} // end else
t = t % numB;
} // end for
if ( sign[0] == '/' )
{
if ( iCount == 0 )
{
cout <<"0" <<'\n';
} // end if
else
{
for ( int i=0; i<iCount; i++ )
{
cout <<div[i];
} // end for
cout <<'\n';
} // end else
} // end if
else
{
if ( sign[0] == '%' )
{
cout <<t <<'\n';
} // end if
} // end else
} // end while
return 0;
}