1047:Round and Round We Go

字符串:
AC代码1:
参考:https://blog.csdn.net/dumeichen/article/details/47746581

//字符串,模拟
#include <iostream>
#include <cstring>
using namespace std;

const int N = 65;
char s[N];
int d[100], tmp[100], len;

int test(int x) {
	int i, j;
	//从新数的第i位开始,循环判断
	for (i = x, j = 0; j < len; i = (i + 1) % len, j++) {
		if (tmp[i] != d[j])
			return 0;
	}
	return 1;
}

int check(int x) {
	int i, j;
	memset(tmp, 0, sizeof(tmp));
	for (i = j = 0; i < len; i++) {//字符串乘法,辅助数组存储结果
		tmp[i] += d[i] * x;
		tmp[i + 1] += tmp[i] / 10;//进位
		tmp[i] %= 10;
	}
	if (tmp[i])//位数增加
		return 0;
	for (i = 0; i < len; i++) {//判断是否是,原数的循环
		if (test(i))
			return 1;
	}
	return 0;
}

int main() {
	while (scanf("%s", s) != EOF) {
		int i, j;
		len = strlen(s);
		for (i = 0; i < len; i++)//反转数组,存成int型
			d[len - 1 - i] = s[i] - '0';
		for (i = 2; i <= len; i++) {
			if (!check(i))
				break;
		}
		if(i<=len)
			printf("%s is not cyclic\n", s);
		else
			printf("%s is cyclic\n", s);
	}
	return 0;
}

数学:
对于数num,如果其位数是n,num*(n+1)得到的结果是n个9,则这个数可循环
参考:https://blog.csdn.net/furney/article/details/7168724
AC代码2:

//数学
#include <iostream>
#include <cstring>
using namespace std;

int main() {
	string num;
	bool flag;
	int i, n, c, t;
	while (cin >> num) {
		flag = true;
		n = num.size() + 1;//可能有进位
		c = 0, t = 0;
		for (i = n - 2; i >= 0; i--) {
			t = num[i] - '0';
			if ((t * n + c) % 10 != 9) {//判断结果是否全为9
				flag = false;
				break;
			}
			c = (t * n + c) / 10;//进位
		}
		if (flag)
			cout << num << " is cyclic" << endl;
		else
			cout << num << " is not cyclic" << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值