閱讀944 返回首頁    go 阿裏雲 go 技術社區[雲棲]


蠻力法找假金幣

#include <iostream>
using namespace std;

bool notFalseGold(int i, int *num, char op)
{
    bool found = false;
    for (int j = 1; j <= num[0] * 2; j++)
    {
        if (num[j] == i)
        {
            found = true;
            break;
        }
    }
    if (found && op == '=' || !found && op != '=' )
        return false;
    else
        return true;
}
int main()
{
    int number[101][1001];//行表示編號,列表示數據
    char op[101];         //對比的結果:< > =這三種
    int n, k;

    cin >> n >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> number[i][0];//0號存兩邊的砝碼個數
        for (int j = 1; j <= number[i][0] * 2; j++)
        {
            cin >> number[i][j];
        }
        cin.get();//吸收回車符
        cin >> op[i];//讀取比較的結果
    }

    int t, i, no;
    for (t = 0, i = 1; i <= n; i++)
    {
        int j;
        for (j = 0; j < k && notFalseGold(i, number[i], op[j]); j++)
        {
            ;//此處隻是分號
        }
        if (j < k)
            continue;
        t ++; //可能的假金幣加1
        if (t > 1)
            break;//如果存在多個假的,那就不符合條件
        else
            no = i;//記下假金幣的編號
    }
    if (t == 1)
    {
        cout << no << endl;
    }
    else
    {
        cout << 0 << endl;
    }
    return 0;
}

最後更新:2017-04-02 15:15:28

  上一篇:go 篩法求素數
  下一篇:go 微軟 Build 2012 大會首日實錄