POJ 1047 Round and Round We Go 循环数新解

furney · · 1488 次点击 · · 开始浏览

1/7=0.142857142857...

=>10^6/7=142857.142857142857...

=>(10^6-1)/7=142857

=>999999/7=142857

```#include<iostream>
#include<string>
using namespace std;

int main()
{
string num;
bool flag = true;
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;
}```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

1/7=0.142857142857...

=>10^6/7=142857.142857142857...

=>(10^6-1)/7=142857

=>999999/7=142857

```#include<iostream>
#include<string>
using namespace std;

int main()
{
string num;
bool flag = true;
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;
}```