# ZOJ1489几乎一样的算法，一个AC，一个TLE，为什么？

victorwhz 2014-04-24 10:40:12

``````
#include <stdio.h>

int main()
{
int feed=0,result;
int n,i;
while (scanf("%d",&n))
{
result=2;
if (!feed)
{
feed=1;
}
else
{
printf("\n");
}
if (n<2||!(n%2))
{
printf("2^? mod 2 = 1");
}
else
{
for (i=1;result!=1;i++)
{
result*=2;
result%=n;
}
printf("2^%d mod 2 = 1",i);
}
}
return 0;
}

``````

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

int main(int argc, char* argv[])
{
int n, temp, result;

while(cin >> n)
{
if((n & 0x1) == 0 || n < 2)
cout << "2^? mod " << n << " = 1" << endl;
else
{
temp = 1;
result = 1;
while(1)
{
temp *= 2;
temp %= n;
if(temp == 1)
break;
++result;
}
cout << "2^" << result << " mod " << n << " = 1" << endl;
}
}
return 0;
}
``````
...全文
245 5 打赏 收藏 举报

5 条回复

victorwhz 2014-04-26
• 打赏
• 举报

........输出错了，人家要“2^i mod n = 1", 我给的"2^i mod 2 = 1"
victorwhz 2014-04-26
• 打赏
• 举报

[quote=引用 2 楼 victorwhz 的回复:] [quote=引用 1 楼 FancyMouse 的回复:] while (scanf("%d",&n)) => while (scanf("%d",&n) == 1)

FancyMouse 2014-04-24
• 打赏
• 举报

[quote=引用 1 楼 FancyMouse 的回复:] while (scanf("%d",&n)) => while (scanf("%d",&n) == 1)

victorwhz 2014-04-24
• 打赏
• 举报

while (scanf("%d",&n)) => while (scanf("%d",&n) == 1)

FancyMouse 2014-04-24
• 打赏
• 举报

while (scanf("%d",&n)) => while (scanf("%d",&n) == 1)

3880

C/C++ 其它技术问题

2014-04-24 10:40