poj 上的1006题runtime error,高手帮忙看下
用的是余数定理做的,在自己机子上感觉速度蛮快,为什么会runtime error?
代码风格不好,看着可能有点累,麻烦大家帮忙看下
- C/C++ code
#include<stdio.h>#include<iostream>#define F 23#define S 28#define T 33#define MAX 50int main(){ int f,s,t,d,a[MAX][4],min,i=0,j=0,k1,k2,k3,number; for(scanf("%d %d %d %d",&f,&s,&t,&d);(f!=-1 && s!=-1 && t!=-1 && d!=-1);scanf("%d %d %d %d",&f,&s,&t,&d)) { //将四个输入的值存入数组中,如果输入为-1,-1,-1,-1结束 , a[i][j]=f; a[i][j+1]=s; a[i][j+2]=t; a[i][j+3]=d; i++; } min=F*S*T;//最小公倍数 number=i;//输入数的数目 for(i=0;i<number;i++)//用剩余定理计算 { int sum=0,temp1,temp2,temp3; k1=temp1=min/F; k2=temp2=min/S; k3=temp3=min/T; int m1=1,m2=1,m3=1; while((k1%F)!=1){ ++m1; k1=temp1*m1; //k1、k2、k3存储用于计算的关键值键值 } while((k2%S)!=1){ ++m2; k2=temp2*m2; } while((k3%T)!=1){ ++m3; k3=temp3*m3; } sum=k1*a[i][j]+k2*a[i][j+1]+k3*a[i][j+2]; sum=sum%min-a[i][j+3]; if(sum<=0) sum=sum+min; printf("Case %d: the next triple peak occurs in %d days.\n",i+1,sum); } system("PAUSE"); return 0;}[解决办法]
- C/C++ code
#include<iostream>//#include<vector>using std::cin;using std::cout;using std::endl;//using std::vector;int main(int argc, char *argv[]){ int p = 0, e = 0, i = 0, d = 0; int cases = 0; while(cin >> p >> e >> i >> d) { if(-1 == p) break; ++cases; int j = 1; while(j <= 21252) { if((j + d - p) % 23 ==0 && (j + d - e) % 28 ==0 && (j + d - i) % 33 ==0) { cout << "Case " << cases << ": the next triple peak occurs in " << j << " days." << endl; break; } ++j; } } return 0;}
[解决办法]
[解决办法]
做了一下,解决了:代码如下:[code=C/C++][/code]#include<iostream>
using namespace std;
int main()
{int p,e,i,d,a,t=0;
while(1)
{
scanf("%d%d%d%d",&p,&e,&i,&d);
if(p==-1 && e==-1 && i==-1 && d==-1)
break;
a=(5544*p+14421*e+1288*i-d+21252)%21252;
if(!a)
a=21252;
printf("Case %d: the next triple peak occurs in %d days.\n",++t,a);
}
return 0;
}