鱼C论坛

 找回密码
 立即注册
查看: 1898|回复: 1

一个蛮力搜索的问题,缠了我好久

[复制链接]
发表于 2014-2-1 20:31:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-1 20:32 编辑

我有个不太好的习惯,就是喜欢在全局一下儿把所有的变量声明完,其实有些变量在局部何以发挥作用。
这是一个找出假金币的算法。
输入示例
5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=
输出3
#include <iostream>

bool jd(int j, int *s, char c)
{
  //Assuming that the j-th coin is fake
  //and check the correctness of this hypothesis
  //s is the weighing record, the first element is number of counterweight
  //c is the result
  int i, f;
  int m = s[0] << 1;
  for(i=f=1; i<=m && f; )
  {
    if(s[i] == j)
      f = 0;
    else
      ++i;
  }
  //Judging whether coin j is included
  if((!f && c == '=') || (f && c != '='))
    return 0;
  return 1;
}

int main()
{
  int num[100][1001];
  char s[1000];                       //Input
  int i, t, n, k, no, j;
  std::cin >> n >> k;
  for(i=0; i<k; ++i)
  {
    std::cin >> num[i][0];
    for(j=1; j<=2*num[i][0]; ++j)
      std::cin >> num[i][j];
    std::cin.get();
    std::cin >> s[i];
  }

  for(i=0, t=0; i<=n; ++i)            //Search all the cases
  {
    for(j=0; j<k && jd(i, num[j], s[j]); ++j);
                                      //Check
    if(j < k) continue;
    ++t;                              //t stores the possible cases
    if(t > 1) break;
    no = i;                           //not only one fake coin
  }

  if(t != 1)
    std::cout << 0;
  else
    std::cout << no;
  return 0;
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-15 20:31:31 | 显示全部楼层
我只是路过打酱油的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-10 19:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表