UVA - 10494 - If We Were a Child Again

来源:互联网 发布:阿里云服务器报价 编辑:程序博客网 时间:2024/06/05 05:30

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_TESTconst int MAXN = 10000;int ChToNum(char ch){return ch - '0';}char NumToCh(int i){return i + '0';}int main(){#ifdef LOCAL_TESTfreopen("f:\\in.txt", "r", stdin);freopen("f:\\out.txt", "w+", stdout);#endifstring 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 ifelse{div[iCount] = tmp;iCount ++;} // end elset = t % numB;} // end forif ( sign[0] == '/' ){if ( iCount == 0 ){cout <<"0" <<'\n';} // end ifelse{for ( int i=0; i<iCount; i++ ){cout <<div[i];} // end forcout <<'\n';} // end else} // end ifelse{if ( sign[0] == '%' ){cout <<t <<'\n';} // end if} // end else} // end whilereturn 0;}


0 0
原创粉丝点击