【问题标题】:what is value of auto t1=std::make_tuple(case1==case2,integer)auto t1=std::make_tuple(case 1==case 2,integer) 的值是多少
【发布时间】:2020-06-25 09:36:18
【问题描述】:

**说明

first, each player draws a single card from the deck
if one of the players drew a card of the winning suit and the other did not, then the player who drew a card of the winning suit wins the round
otherwise, the numbers written on the cards decide: if one player drew a card with a greater number on it than the other player, the player with the greater number wins, otherwise, if both players drew cards with the same numbers, the round ends in a draw
after the round is ended, the players return the cards they drew into the deck

** *在第一行,有一个字符

表示获胜的花色。

在第二行,有一个整数

表示要玩的回合数。

接下来的每一个 线表示在单轮游戏中抽出的牌,包含四个以空格分隔的值: ,并表示玩家 1 牌的花色、玩家 1 牌上的数字、玩家 2 牌的花色和玩家的2张牌。 *

input:-
B
5
A 2 B 1
A 7 D 2
B 5 D 13
B 3 B 1
A 12 C 12
int main() {
    char winning_suit;
    std::cin >> winning_suit;
    unsigned tests;
    std::cin >> tests;
    while (tests--) {
        char suit1, suit2;
        int number1, number2;
        std::cin >> suit1 >> number1 >> suit2 >> number2;

        auto t1 = std::make_tuple(suit1 == winning_suit, number1);
        auto t2 = std::make_tuple(suit2 == winning_suit, number2);

        if (t1 > t2) {
            std::cout << "Player 1 wins\n";
        } else if (t1 < t2) {
            std::cout << "Player 2 wins\n";
        } else {
            std::cout << "Draw\n";
        }
    }
}
output:-
Player 2 wins
Player 1 wins
Player 1 wins
Player 1 wins
Draw

【问题讨论】:

  • "auto t1=std::make_tuple(case1==case2,integer)"。所以 t1 将是一个具有两个值的元组,第一个是布尔值,具有“case1==case2”比较的结果,第二个是代码中 number1 和 number2 提供的整数。我不明白你的问题是什么

标签: c++ c++11 stl tuples


【解决方案1】:

声明

auto t1 = std::make_tuple(suit1 == winning_suit, number1);
auto t2 = std::make_tuple(suit2 == winning_suit, number2);

声明和初始化std::tuple&lt;bool, int&gt;

第一次循环,它们有值

{ false, 2 }
{ true, 1 }

然后使用std::tuple::operator&gt; and std::tuple::operator&lt; 对它们进行比较,它根据第一个元素对它们进行排序,然后是第二个元素。这是lexicographic order,具体来说

偏序集的笛卡尔积的顺序

这意味着获胜花色中的所有牌都大于其他花色中的所有牌,并且当花色相同时,编号较高的牌大于编号较小的牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2014-08-25
    • 2017-04-16
    • 1970-01-01
    • 2016-03-14
    • 2019-11-26
    • 2021-11-27
    相关资源
    最近更新 更多