n/2 和 n-(n/2) 工作正常,但 C++ 中的 Ceil 和 Floor 函数给出错误答案

问题描述

我在 Codechef 上解决“硬币翻转”问题(问题代码CONFLIP)并遇到了这个问题。

我的一个程序提交成功,而在第二个程序中,我在提交时收到错误答案”状态。谁能告诉为什么第二个程序是错误的?

注意:没有没有编译错误,第二个程序也在可见测试用例和我自己测试过的许多其他测试用例上给出正确的输出,但在提交时,我收到“错误答案”状态.两个程序的逻辑是一样的。所有的头文件都被正确包含,只是为了干净的外观这里没有提到。 [使用 C++14(GCC 6.3) 编译]

一个程序:-

int main()
{
  int t,g,n,i,q;
  cin>>t;
  while(t--)
  {
    cin>>g;
    while(g--)
    {
      cin>>i>>n>>q;
      if(i!=q)
      {
        cout<<n-(n/2)<<endl; //This line is different in both the programs.
      }
      else
      {
        cout<<n/2<<endl;  //This line is different in both the programs.
      }
    }
  }
  return 0;
}

第二个程序:-

int main()
{
  int t,q;
  cin>>t;
  while(t--)
  {
    cin>>g;
    while(g--)
    {
      cin>>i>>n>>q;
      if(i!=q)
      {
        cout<<ceil(float(n)/float(2))<<endl;  //This line is different in both the programs.
      }
      else
      {
        cout<<floor(float(n)/float(2))<<endl;  //This line is different in both the programs.
      }
    }
  }
  return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)