poj 1047-Round and Round We Go

题目没什么难度,主要是两个要点,一是长整数的乘法,二是循环数组比对,都是比较简单的。

唯一想骂娘的地方在于,,,输入没有终止标志,这让我纠结了好一阵子,最后百度才知道可以下面这么搞,涨姿势了又

while (cin>>temp)
{
<span style="white-space:pre">	</span>.....
}

下面是代码,写得比较粗犷没有优化。。。

#include <iostream>
using namespace std;

bool comp (unsigned char a[], unsigned char b[], unsigned char len)
{
	unsigned char aii = 0, bii = 0;
	while (aii < len)
	{
		if (a[aii] == b[bii])
		{
			aii++;
			bii++;
		}
		else if (aii == 0)
		{
			bii++;
		}
		else if (aii<=bii)
		{
			aii = 0;
		}
		else
		{
			return false;
		}
		if (bii == len && aii != 0)
		{
			bii = 0;
		}
		else if (bii == len && aii == 0)
		{
			return false;
		}
	}
	return true;
}

bool multip (unsigned char a[], unsigned char len, unsigned char b)
{
	unsigned char a0[60] = {0};
	unsigned char temp = 0;
	unsigned char addi = 0;
	unsigned char b1 = b/10, b0 = b%10;

	for (int i=len; i>0; i--)
	{
		temp = a[i-1]*b0+addi;
		a0[i-1] = temp%10;
		addi = temp/10;
	}

	if (b1>0)
	{
		addi = 0;
		for (int i=len; i>1; i--)
		{
			temp = a[i-1]*b1+addi;
			a0[i-2] += temp%10;
			addi = temp/10+a0[i-2]/10;
			a0[i-2] = a0[i-2]%10;
		}
	}
	return comp(a,a0,len);
}
int main ()
{
	unsigned char *a = new unsigned char[62];
	unsigned char *a_raw = new unsigned char[62];
	unsigned char len = 0;
	while (cin>>a_raw)
	{
		len = 0;
		while (a_raw[len]!=0)
		{
			a[len] = a_raw[len]-48;
			len ++;
		}
		if (len==0)
		{
			break;
		}
		int i;
		for (i=1; i<=len; i++)
		{
			if (multip(a, len, i)==false)
			{
				cout<<a_raw<<" is not cyclic"<<endl;
				break;
			}
		}
		if (i==len+1)
		{
			cout<<a_raw<<" is cyclic"<<endl;
		}
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值