UVA - 10494 - If We Were a Child Again

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值