maybe_madio 发表于 2010-10-26 23:30

POJ-1006问题

POJ-1006

在本地VC6.0下测试,得出正确结果, 为什么提交后总是Wrong Answer

现将代码贴出,请帮忙看看:
#include <iostream>
using namespace std;
#define PH      0
#define EM      1
#define IN      2
int get_Top(int days, int kind);
int triple(int ptop, int etop, int itop, int cur);
bool is_Integer(float n);
int main()
{
int ptop,  etop,  itop;
int pdays, edays, idays, curdays;
int count =0;

while(cin >> pdays >> edays >> idays >> curdays)
{
  if(pdays==edays&&pdays==idays&&pdays==curdays&&pdays==-1)
   break;
  count++;
  ptop = get_Top(pdays,PH);
     etop = get_Top(edays,EM);
     itop = get_Top(idays,IN);
  cout<<"Case "<<count<<": "<<"the next triple peak occurs in "<<triple(ptop, etop, itop, curdays)<<" days.";
}

return 0;
}
int get_Top(int days, int kind)
{
switch(kind)
{
case 0:
  return days%23;
case 1:
  return days%28;
case 2:
  return days%33;
}
return 0;
}

int triple(int ptop, int etop, int itop, int cur)
{
float x, y, z;
int m1   = etop-ptop-5,
  m2   = itop-etop-5;
int temp = 0;
for(z=2;  (itop+(z-1)*33) <= 21252;  z++)
{
  temp = m2 + 33*z;
  y = (float)temp/28;
  x = (float)(m1+temp)/23;
  if(x>0 && y>0 && is_Integer(x) && is_Integer(y))
   return itop + (z-1)*33 -cur;
}
return 0;
}

bool is_Integer(float n)
{
if( (int)n - n ==0)
  return true;
else
  return false;
}


ultra1989 发表于 2010-10-27 08:45

本帖最后由 ultra1989 于 2010-10-27 08:54 编辑

用float可能会有精度问题用中国剩余定理求解
参考程序:

#include<stdio.h>
int main(){
   int a,b,c,m,n,i,d=1;
   while(scanf("%d%d%d%d",&a,&b,&c,&m)){

if(a==-1&&b==-1&&c==-1&&m==-1) break;

n=0;

i=(5544*a+14421*b+1288*c-m+21252)%21252;

if(!i)

i=21252;

printf("Case %d: the next triple peak occurs in %d days.\n",d++,i);

   }

return 0;
}

qbist 发表于 2010-10-27 13:20

{:3_44:}{:3_51:}{:2_28:}

小草远在天涯 发表于 2010-10-27 17:27

{:3_61:}好复杂啊,我是初级的。。。路过。。。

maybe_madio 发表于 2010-10-27 22:23

回复 ultra1989 的帖子


    确实这道题使用剩余定理解效率太高了! 哈哈! 学习了!  我还没有看过数论方面的书! 以后肯定要看看

两个程序一对比,一下就体现出数学的强大力量! {:2_31:}

39133120 发表于 2010-10-28 23:11

好复杂啊,得仔细

鸿光满面 发表于 2011-2-4 13:40

好复杂啊         
页: [1]
查看完整版本: POJ-1006问题