[Daily Exercises] Calculating the Size of Poker Cards with Perfect Numbers - Niu Ke Exercises

        Hello, everyone, this is bang___bang_, and today I still record 2 Niuke exercises, 1 simple question (perfect number calculation); 1 medium question (playing card size).

Table of contents

1️⃣ Complete number calculation

2️⃣Poker size


1️⃣ Complete number calculation

Calculation of perfect numbers

describe:

        Perfect numbers, also known as perfect numbers or complete numbers, are some special natural numbers.

        The sum (i.e., factor function) of all its true factors (i.e. divisors other than itself) is exactly equal to itself .

For example: 28, it has divisors 1, 2, 4, 7, 14, 28, except itself 28, the remaining 5 numbers are added together, 1+2+4+7+14=28.

        Input n, please output the number of perfect numbers within n (including n).

Data range: 1≤n≤5×10^5 

Enter a description:

enter a number n

Output description:

Output the number of perfect numbers not exceeding n

Example:

Input: 1000

Output: 3

Problem-solving ideas:  The sum of the divisors of the number is equal to the number itself.

        Dividers are summed and compared with themselves, equal count +1.

Implementation code:

#include <iostream>
#include<cmath>
using namespace std;

bool get_div_num(int n)
{
    int sum=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            sum+=i;
            //去掉重复的约数
            if(n/i!=i)
            {
                sum+=n/i;
            }
        }
    }
    return sum==n;
}
int main() {
    int n=0;
    while(cin>>n)
    {
        int count=0;
        for(int i=2;i<=n;i++)
        {
            if(get_div_num(i))
            {
                count++;
            }
        }
        cout<<count<<endl;
    }
    return 0;
}

2️⃣Poker size

Poker Card Size 

describe:

        Everyone should be familiar with the poker game. A deck of cards consists of 54 cards, including 3~A, 4 cards for each 2, 1 card for the king, and 1 card for the king. Cards from small to large are represented by the following characters and strings (among them, lowercase joker means little king, uppercase JOKER means big king):)
3 4 5 6 7 8 9 10 JQKA 2 joker JOKER
        Input two hands of cards, use " -" connection, each card in each hand is separated by a space, there is no space on both sides of "-", such as: 4 4 4 4-joker JOKER Please
        compare the size of the two hands and output the larger card, if there is no comparison relationship, then output ERROR

basic rules:
(1) The input of each hand may be one of a pair, a straight (5 consecutive cards), three, a bomb (four) and a pair of kings. There is no other situation, which is guaranteed by the input Both hands are legal, and the straights have been arranged from small to large;
(2) Except for bombs and pairs of kings that can be compared with all cards, other types of cards can only be compared with the same type of existence (for example, a pair with Pair comparison, compare three with three), regardless of the dismantling of cards (such as: splitting a pair into pieces)
(3) The size rules are the same as the common rules that everyone usually understands, size, pair, and three comparisons Card size; Shun compares the smallest card size ; bombs are larger than all the previous cards, and bombs compare card sizes; Pair King is the largest card ;
(4) The two input hands will not be equal.

Answer hints:
(1) Except for bombs and pairs of kings, the others must be compared with the same type.
(2) The legality of the input has been guaranteed, so there is no need to check whether the input is a legal card.

(3) The input sequence has been sorted from small to large, so there is no need to sort it again.

Data range: ensure that the input is legal

Enter a description:

        Enter two hands of cards, connect the two hands with "-", each card in each hand is separated by a space, and there is no space on both sides of "-", such as 4 4 4 4-joker JOKER.

Output description:

        Output the larger hand of the two hands, without connectors, the order of the playing cards remains unchanged, and they are still separated by spaces; if there is no comparison relationship, output ERROR.

Example: 

Input: 4 4 4 4-joker JOKER

Output: joker JOKER

substr interception string function: start from pos position to intercept the substring whose length is len. The npos in the string function parameter means "until the end of the string".

basic_string substr (size_type pos = 0, size_type len = npos) const;

Problem-solving ideas:

        1. Judging whether there is a joker card, if there is a joker card, it will be returned.

        2. Split the string into two hands.

        3. Get the first card of the two hands. (for size comparison next)

        4. Judging the number of cards in the two hands, if they are the same, it means that the two hands are of the same type, enter the rules of Fighting the Landlord, and return the one with the highest card.

        5. Under the legal premise, if the number of cards in the hand is different, there can only be a pair of cards in the hand that is a joker or a bomb. The joker has been resolved in step 1, and only the bomb is left. If the number of cards in the hand is 4, return to that hand. (i.e. bombs).

        6. If the rest do not conform to the comparison relationship, return ERROR.

Implementation code:

#include <iostream>
#include <string>
#include<algorithm>
using namespace std;

string CmpCard(const string &line)
{
    //判断是否有鬼牌
    if(line.find("joker JOKER")!=string::npos)
    {
        return "joker JOKER";
    }
    //分割手牌
    int pos=line.find('-');
    string card1=line.substr(0,pos);
    string card2=line.substr(pos+1);
    //统计手牌数(空格划分,统计空格数再+1)
    int cnt1=count(card1.begin(),card1.end(),' ')+1;
    int cnt2=count(card2.begin(),card2.end(),' ')+1;
    //获取第一张牌
    string card1_first=card1.substr(0,card1.find(' '));
    string card2_first=card2.substr(0,card2.find(' '));
    //牌数相同,常规比较,斗地主规则    
    if(cnt1==cnt2)
    {
        //比较串,直接比较在标准串的下标大小
        string str="3 4 5 6 7 8 9 10 J Q K A 2";
        if(str.find(card1_first)>str.find(card2_first))
            return card1;
        return card2;
    }
    //如果牌数不同,鬼牌情况已排除,不合法的牌不会输入,只剩单副手牌炸弹情况
    if(cnt1==4)
        return card1;
    if(cnt2==4)
        return card2;
    //剩余情况均不存在比较关系
    return "ERROR";

}


int main() {
    string line,res;
    while(getline(cin,line))
    {
        res=CmpCard(line);
        cout<<res<<endl;
    }
}

At the end of the article, this article records 2 Niuke exercises (calculation of complete numbers, the size of playing cards) for the purpose of recording. If necessary, I hope it can be helpful!

Guess you like

Origin blog.csdn.net/bang___bang_/article/details/132010091