证明存在:http://blog.csdn.net/jcwKyl/article/details/3859155
自己的代码 M取余数能算的数比较大:
int _tmain(int argc, _TCHAR* argv[])
{
int N;
// 用栈存储似乎是个错误,费老劲了
cout<<" input N "<<endl;
cin>>N;
stack<int> *bigInt = new stack<int>[N];
for(int M=1,i=1;;M=M*10%N,i++)
{
int reminder = M%N;
if(reminder==0)
{
bigInt[0].push(i);
cout<<" in if "<<endl;
break;//return 0;
}
else
{
cout<<" in else 1 "<<endl;
if(bigInt[reminder].empty()==1)
bigInt[reminder].push(i);
for(int j=1;j<N;j++)// update reminder information
{
cout<<" in for 1 "<<endl;
if(bigInt[j].empty()==0&&bigInt[(j+reminder)%N].empty()==1&&i>bigInt[j].top())
{
cout<<" in if in if 1 "<<endl;
int jlength=bigInt[j].size();
int *pt=new int[jlength];// pt用于存储从栈里拿出来的元素
for(int k=0;k<jlength;k++)
{
int temp = bigInt[j].top();
pt[jlength-1-k]=temp;
bigInt[j].pop();//元素不能出栈
//bigInt[(j+reminder)%N].push(temp);
}
for(int k=0;k<jlength;k++)//保持原来的元素不变
{
bigInt[(j+reminder)%N].push(pt[k]);
bigInt[j].push(pt[k]);
}
bigInt[(j+reminder)%N].push(i);
}
}
if(bigInt[0].empty()==0) break;//return 0;
}
}
bool b[100] ={0};
//for(int m=0;m<N;m++)
//{
int m=0;
cout<<" in bool "<<endl;
int len = bigInt[m].size();
for(int i=0;i<len;i++)
{
int k = bigInt[m].top();
cout<<k<<" ";
b[k]=1;
bigInt[m].pop();
}
cout<<" last "<<endl;
//}
for(int i=20;i>=0;i--)
cout<<b[i]<<" ";
cout<<endl;
system("pause");
//list<int> *li= new list<int>[N];
}
别人的算法: 用一个整数来存储,
#include <stdio.h>
int iRes[1000];
int OneAndZeroNum(int N)
{
int i, X; // X记录目前遍历的十进制位数
int num; // num = 10^X
for(i = 0; i < N; i++)
iRes[i] = 0;
for(X = 0, num = 1; ; X++, num *= 10)
{
int tempRes = num % N;
if(tempRes != 0) // 如果10^X不能整除N
{
if(iRes[tempRes] == 0)
{
//cout<<"a X is "<<X<<" iRes[temp] is "<<iRes[tempRes]<<endl;
iRes[tempRes] = 1 << X; // 记录为tempRes时,十进制数的位数,先赋值运算 再移位运算
//cout<<"b X is "<<X<<" iRes[temp] is "<<iRes[tempRes]<<endl;
}
for(i = 1; i < N; i++) // 更新余数对应的十进制数
if(iRes[i] != 0 && iRes[(i+tempRes)%N] == 0 &&
((iRes[i] & (1 << X)) == 0)) // 避免错误重复更新
iRes[(i+tempRes)%N] |= (iRes[i] | (1 << X));
if(iRes[0] != 0)
return iRes[0];
}
else
{
iRes[0] = 0;
iRes[0] |= 1 << X;
return iRes[0];
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int N;
int iBitMapNum, iFactor;
int Num;
while(1)
{
scanf("%d", &N);
iBitMapNum = OneAndZeroNum(N);
iFactor = 1;
Num = 0;
while(iBitMapNum)
{
Num += (iBitMapNum & 0x01) * iFactor;
iFactor *= 10;
iBitMapNum >>= 1;
}
printf("%d\n", Num);
}
system("pause");
return 0;
}