首页 > 试题广场 >

循环数

[编程题]循环数
  • 热度指数:806 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
142857是一个六位数,我们发现:
142857 * 1 = 142857
142857 * 2 = 285714
142857 * 3 = 428571
142857 * 4 = 571428
142857 * 5 = 714285
142857 * 6 = 857142
即用1到6的整数去乘142857,会得到一个将原来的数首尾相接循环移动若干数字再在某处断开而得到的数字。
也就是说,如果把原来的数字和新的数字都首尾相接,他们得到的环是相同的。只是两个数的起始数字不一定相同。
请写一个程序,判断给定的数不是循环数。

输入描述:
输入包括多组数据。

每组数据包含一个正整数n,n是2到60位的正整数,并且允许前缀0。即001也是合法的输入数据。


输出描述:
对应每一组数据,如果是循环数,则输出“Yes”;否则,输出“No”。
示例1

输入

142857
012345

输出

Yes
No
数据使用int和long这样都会越界的
使用大数乘法算出两个的结果
判断循环用两个字符串判断是否包含
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while(sc.hasNext()){
			
			String str = sc.nextLine();
			
			String twostr = str+str;
			boolean flag = true;
			
			for(int i= 1; i<=6 ;i++)
			{
				String result = mutli(str,i);
				if(!isCircle(twostr,result)){
					flag = false;
					break;
				}		
			}
			if(flag)
				System.out.println("Yes");
			else
				System.out.println("No");
		}
	}

	 public static String mutli(String s,int n)
	        {
	            char[] ss = s.toCharArray();
	            String result = null;
	            int jinwei = 0;
	            for(int i = s.length()-1;i >= 0;i--)
	            {
	                char c = s.charAt(i);
	                int nn = c - '0';
	                int o = nn * n + jinwei;
	                ss[i] = (char) ((o)%10 + '0');
	                jinwei = o / 10;
	            }
	            if(jinwei==0){
	                result = String.valueOf(ss);
	            }else {
	                result = String.valueOf(jinwei)+String.valueOf(ss);
	            }
	            return result;
	        }

	        private static boolean isCircle(String strcycle,String str) {
	            if(strcycle.contains(str)){
	                return true;
	            }else{
	                return false;
	            }
	        }
}


编辑于 2017-08-22 15:48:33 回复(0)
更多回答
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
string Mul(const string& s,int index)
{
    string res;
    int tmp=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        int bit=(s[i]-'0')*index+tmp;
        res.push_back(char(bit%10+'0'));
        tmp=bit/10;
    }
    while(tmp)
    {
         res.push_back(tmp%10);
         tmp/=10;
    }
	int index1=0;
	int index2=s.size()-1;
	while(index1<=index2)
	{
		swap(res[index1],res[index2]);
		index1++;
		index2--;
	}
    return res;
}
bool isCarry(const string& s,int i)
{
    string tmp1=Mul(s,i);
    string lei=s+s;
    for(int i=0;i<lei.size();i++)
    {
        string tmp=lei.substr(i,s.size());
        if(tmp==tmp1)
            return true;
    }
    return false;
}
int main()
{
    string s;
    while(cin>>s)
    {
        bool isOk=true;
        for(int i=1;i<=s.size();i++)
        {
            if(!isCarry(s,i))
            {
                isOk=false;
                break;
            }
        }
        if(isOk)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

发表于 2017-09-10 13:56:59 回复(1)
import java.util.Scanner;
public class CycleNum {
	 
	    public static void main(String[] args) {
	        Scanner input = new Scanner(System.in);	  
	         while(input.hasNext())
	         {
	             String str = input.nextLine();
	             String strcycle = str +str;
	             boolean resu = isCircle(strcycle, mutliP(str, 1)) &&isCircle(strcycle, mutliP(str, 2)) 
	            		 &&isCircle(strcycle, mutliP(str, 3)) &&isCircle(strcycle, mutliP(str, 4)) 
	            		 &&isCircle(strcycle, mutliP(str, 5))
	                     &&isCircle(strcycle, mutliP(str, 6));
	              
	             if(resu)
	                 System.out.println("Yes");
	             else
	                 System.out.println("No");
	         }
	    }
	    // 字符串全是数字的乘法 
	    public static String mutliP(String s,int n)
	    {
	        char[] ss = s.toCharArray();
	        String result = null;
	        int jinwei = 0;
	        for(int i = s.length()-1;i >= 0;i--)
	        {
	            char c = s.charAt(i);
	            int nn = c - '0';
	            int o = nn * n + jinwei;
	            ss[i] = (char) ((o)%10 + '0');
	            jinwei = o / 10;
	        }
	        if(jinwei==0){
	        	result = String.valueOf(ss);
	        }else {
	        	result = String.valueOf(jinwei)+String.valueOf(ss);
	        }
	        return result;
	    }
	    //判断是否为循环数字
		private static boolean isCircle(String strcycle,String str) {
			if(strcycle.contains(str)){
				return true;
			}else{
				return false;
			}
		}
}

发表于 2017-08-02 20:34:06 回复(1)
对于每一个输入的数,先找出所有的循环组成;然后依次求2-6倍的结果,判断是是原数的循环。
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>

using namespace std;

string add(string num1, string num2)
{
	int n = num1.size();
	int carry = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		int cur = num1[i] - '0' + num2[i] - '0' + carry;
		if (cur >= 10) { carry = 1; cur -= 10; }
		else carry = 0;
		num1[i] = '0' + cur;
	}
	if (carry == 1) num1 = "1" + num1;
	return num1;
}

bool isCirculation(string num)
{
	int n = num.size();
	if (num[0] > '1') return false;
	unordered_set<string> circle;
	string cur = num;
	for (int i = 0; i <= n; ++i)
	{
		circle.emplace(cur);
		cur = cur.substr(1) + cur.substr(0, 1);
	}

	cur = num;
	for (int i = 2; i <= 6; ++i)
	{
		cur = add(cur, num);
		if (circle.find(cur) == circle.end()) return false;
	}

	return true;
}

int main(int argc, char** argv)
{
	string num;
	while (cin >> num)
	{
		if (isCirculation(num)) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

发表于 2017-07-08 15:33:13 回复(0)
#include<bits/stdc++.h>
using namespace std;

string strAdd(string a, string b) {
    string res; 
    int i, j, tmp, addbit = 0;
    for (i = j = a.length() - 1; 
         i >= 0; --i, --j) {
        tmp = addbit;
        if (i >= 0) {
            tmp += a[i] - '0';
        }
        if (j >= 0) {
            tmp += b[i] - '0';
        }
        res += tmp % 10 + '0';
        addbit = tmp / 10;
    }
    if (addbit) {
        res += addbit + '0';
    }
    reverse(res.begin(), res.end());
    return res;
}

bool isLoopNum(string a, string s) {
    bool flag = true;
    int i, lena= a.length();
    string left, right;
    for (i = 0; i < lena - 1; ++i) {
        left = a.substr(0, i + 1);
        right = a.substr(i + 1);
        right += left;
        if (right == s)
            return true;
    }
    return false;
}

int main() {
    string s, tmp;
    cin >> s;
    tmp = s;
    int len = s.length();
    bool flag = true;
    for (int i = 2; i <= len; ++i) {
        tmp = strAdd(s, tmp);
        if (tmp.length() != s.length() || !isLoopNum(tmp, s)) {
            flag = false;
            break;
        }
    }
    if (flag) {
        cout << "Yes";
    } else {
        cout << "No";
    }
    cout << "\n";
    return 0;
}

编辑于 2018-11-02 18:13:05 回复(0)
#include<iostream>
#include <string>
using namespace std;

string multiplication(string s, int n)
{
	int i = s.size() - 1, num, sum, pre = 0;
	bool sta = false;
	string res;
	for (; i >= 0; i--)
	{
		num = s[i] - '0';
		if (sta == true)
		{
			sum = num * n + pre;
			sta = false;
		}
		else
			sum = num * n;
		if (sum > 9)
		{
			pre = sum / 10;
			sum = sum % 10;
			sta = true;
		}
		char ch = sum + '0';
		res = ch + res;
	}
	if (true == sta)
	{
		char ch = pre + '0';
		res = ch + res;
	}
	return res;
}
bool Stringcompare(string s1, string s2)
{
	if (s1 == s2)
		return true;
	string res;
	char ch = s1[0];
	int len = s2.length();
	bool sta = false;
	for (int i = 1; i < len; i++)
	{
		if (s2[i] == ch)
		{
			res.clear();
			res = s2.substr(i, len - i) + s2.substr(0, i);
			if (res == s1)
			{
				sta = true;
				break;
			}
		}
	}
	return sta;
}

int main()
{
	string s, num;
	while (cin >> s)
	{
		bool sta = true;
		for (int i = 2; i <= 6; i++)
		{
			num = multiplication(s, i);
			if (Stringcompare(s, num) == false)
			{
				sta = false;
				break;
			}
		}
		if (true == sta)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}

发表于 2017-04-28 00:09:27 回复(0)
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int num=sc.nextInt();
String s=String.valueOf(num);
int len=s.length();
String s1=s+s;
boolean f=true;
for(int i=1;i<=len;i++){
if(!s1.contains(String.valueOf(num*i)))
f=false;
}
if(f)
System.out.println("YES");
else
System.out.println("NO");
}
}
}

发表于 2017-04-16 00:11:46 回复(0)
#include<iostream>
#include<string>
using namespace std;
string mutipl(const string &input,int k)
{
string output,foutput;
int num1,c=0;
for (int i=input.size()-1;i>=0;i--)
{
num1=(input[i]-'0')*k+c;
if (num1>=10)
{
c=num1/10;
num1=num1%10;
}
else
c=0;
output.push_back(num1+'0');
}
if (c>0)
output.push_back(c+'0');
for(int j=output.size()-1;j>=0;j--)
foutput.push_back(output[j]);
return foutput;
}
int main()
{
string s1,s2,r("Yes");
int num;
bool fit[10]={false};
while(cin>>s1)
{
for(int i=0;i<s1.size();i++)
fit[s1[i]-'0']=true;
for(int i=2;i<=6;i++)
{
s2=mutipl(s1,i);
//cout<<s2<<endl;
if(s2.size()==s1.size())
{
for(int j=0;j<s1.size();j++)
{
if(fit[s2[j]-'0'])
continue;
else
{
r="No";
break;
}
}    
}
else
{
r="No";
break;
}
if(r=="No")
break;
if (i==6)
r="Yes";       
}
cout<<r<<endl;
}
}
发表于 2016-10-07 20:57:40 回复(0)
//首先,重申题意:用 1-6 的整数乘输入的数字n,不是 1-n的位数 的整数去成n
//然后,关键是两点:1,模拟长数字与个位数相乘;2,判断一个数是否为另一个数的封闭循环。
#include <iostream>
#include <string>
using namespace std;
string multiple(string bignum, int i) {
    string result;
    int carry = 0;
    for (auto b = bignum.rbegin();b != bignum.rend();b++) {
        int temp = ((*b) - '0')*i+carry;
        result.insert(result.begin(),temp % 10+'0');
        carry = temp / 10;
    }
    if (carry != 0) {
        result.push_back(carry);
    }
    return result;
}
bool isCircle(string str1, string str2) {
    string &copy=str1.append(str1);
    for (int i = 0;i < str2.size();i++) {
        if (0==copy.compare(i, str2.size(), str2)) {
            return true;
        }
    }
    return false;
}
int main() {
    string bignum;
    while (getline(cin,bignum)) {
        bool flag = true;
        for (int i = 1;i <= 6;i++) {
            string str=multiple(bignum, i);
            if (!isCircle(bignum, str)) {
                flag = false;
                break;
            }
        }
        if (flag)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}
发表于 2015-10-03 12:03:44 回复(0)
private static boolean isCircle(String numStr){
BigDecimal number = new BigDecimal(numStr);
numStr += numStr;
for(int i=2;i<=6;i++){
String s = number.multiply(new BigDecimal(i)).toString();
if(s.length()*2!=numStr.length()){
s+="0";
}
if(!numStr.contains(s)){
return false;
}
}
return true;
}

编辑于 2015-09-17 16:19:54 回复(0)
// write your code here cpp

#include<stdio.h>
#include<string.h>
#include<stdbool.h>

char n[64], m[64];

void calBig(char *n, char *m, int fac)
{
    m[strlen(n)] = '\0';
    for(int i=strlen(n)-1, tmp=0, car=0; i>=0; --i){
        tmp = (n[i]-'0')*fac + car;
        m[i] = tmp%10+'0';
        car = tmp/10;
    }
}

bool isCircle(char *n, char *m)
{
    for(int i=0, k=0; m[i]; ++i, k=0){
    	for(int j=i; 
            m[i]==n[0]&&n[k]&&n[k]==m[j]; ++j, ++k)
            if(!m[j+1]) 
            	j = -1;
        if(!n[k])
            return true;
    }
    return false;
}

int main()
{
    while(~scanf("%60s", n)){
        bool isC = true;
        for(int i=2; i<=6; ++i){
            calBig(n, m, i);
            if(!(isC=isCircle(n, m)))
                break;
        }
        printf("%s\n", isC?"Yes":"No");
    }
    return 0;
}


发表于 2015-09-11 15:26:24 回复(0)
#include<stdio.h>
#include<string.h>
long pow_10(int n)
{
int i = 0;
long data = 1;
for(i = 0; i < n; i++)
{
data *= 10;
}
return data;
}
int main()
{
long data = 0, data_n[6] = {0};
int i = 0, j = 0, n = 0;
int a[6] = {0};
char ch[6] = {'0'};
printf("put in data(6)\n");
scanf("%s", ch);
//printf("strlen(ch) = %d\n", strlen(ch));

for(i = 6 - strlen(ch), j = 0; i < 6; i++, j++)
{
a[i] = ch[j] - '0';
if(a[i] != 0)
data += a[i] * pow_10(5 - i);
}
printf("data is %ld\n", data);
for(i = 0; i < 6; i++)
{
for(j = 0; j < 6; j++)
{
if(a[j] != 0)
{
data_n[i] += a[j] * pow_10(5 - (i+j) % 6);
}
}
printf("data_n[i] = %ld\n", data_n[i]);
}
  for(n = 2; n < 7; n++)
{
for(i = 0; i < 6; i++)
{

if(data * n == data_n[i])
{
break;
}
}
if(i == 6)
{
printf("No\n");
return 0;
}
}
printf("Yes\n");
return 0;
}
发表于 2015-03-05 15:03:40 回复(0)