Topic describes

LL: I’m in a really good mood today, because I bought a deck of cards and found that there were 2 Kings and 2 small Kings in it. He randomly picked out 5 cards, want to test their luck, see if they can draw a straight, if so, he decided to buy a sports lottery ticket, hey hey!! “Ace of hearts, 3 of spades, xiao Wang, King, 5 of diamonds”, “Oh My God!” Not straight….. LL was not happy. He thought about it for A while and decided that big, small and small could be any number, and A could be 1,J was 11,Q was 12 and K was 13. The top five cards can become “1,2,3,4,5”, “So Lucky!” . LL: I’m going to buy a sports lottery ticket. Now, you are asked to use this card to simulate the above process, and then tell us LL’s luck, printing true if the card forms a straight, false otherwise. For convenience, you can think of big wang as 0.

Answer key

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool IsContinuous(vector<int> numbers) {
    sort(numbers.begin(), numbers.end());
    int len = numbers.size(), zero = 0, internal = 0;
    if (len == 0)return false;
    for (int i = 0; i < len - 1; i++) {
        if (numbers[i] == 0) {
            zero++;
            continue;
        }
        if (numbers[i] == numbers[i + 1])return false;
        internal += (numbers[i + 1] - numbers[i] - 1);
    }
    if (zero >= internal)return true;
    return false;
}

int main(a) {
    ios::sync_with_stdio(false);
    vector<int> v;
    int n, temp;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        v.push_back(temp);
    }
    cout << IsContinuous(v);
    return 0;
}
Copy the code