This is the fifth day of my participation in Gwen Challenge

What a pity, yesterday force majeure was absent for one day, the nuggets badge set box I wanted was gone, it seems that there is no badge 😢😢😢~

Start algorithm brush problem!!

An introduction to try

An array of

An array is a collection of data of the same type stored in contiguous memory space.

dichotomy

Number 35: Search for insertion location

Given a sorted array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, return the position where it will be inserted in order.

class Solution{
public:
    int searchInsert(vector<int>& nums, int target){
        for(int i = 0; i< nums.size(a); i++){ } } };Copy the code

shuffleing Machine

A deck of 54 cards according tp a given random order and repeats for a given number of times.It is assumed that the initial status of a card deck is in the following order:

S1, S2... , / S13, H1,... , C1,... , D1,... ,D13,J1,J2

where S stands for Spade,H for Heart C for Club, D for Diamond, and D for Joker.

A given order is a permutation of distinct integers in [1, 54].

If the number at the i-th position is j, it means to move the card from position i to position j.

For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2.

Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

#include <stdio.h>

int main(a) {
    int cards[55];
    int mids[55];
    int ends[55];
    int times = 0;
    for (int i = 1; i < 55; i++) {
        cards[i] = i;
    }
    scanf("%d", &times);
    for (int i = 1; i < 55; i++)
    {
        scanf("%d", &mids[i]);
    }
    for (int j = 0; j < times; j++) {
        for (int i = 1; i < 55; i++) {
            ends[mids[i]] = cards[i];
        }
        for (int a = 1; a < 55; a++) { cards[a] = ends[a]; }}for (int i = 1; i < 55; i++) {
        int temp = cards[i];
        if (temp <= 13) printf("S%d", ends[i]);
        else if (temp < 26) printf("H%d", temp%13);
        else if (temp == 26) printf("H13");
        else if(temp < 39) printf("C%d",temp%13);
        else if(temp == 39) printf("C13");
        else if (temp < 52) printf("D%d", temp%13);
        else if (temp == 52) printf("D13");
        else printf("J%d", temp%13);
        if(i ! =54) printf("");
    }
    return 0;
}
Copy the code

Thinking and summarizing

Improvement: The final output is too cumbersome and can be simplified as follows.

for(int i = 1; i<=55; i++) {if(i ! =1) printf("");
    cards[i]--;
    printf("%c%d",CHA[cards[i]/13],cards[i]%13+1);// Output letters through character array, judge letters by division; The latter avoids the 13%13==0 situation by using the original number -1
}
Copy the code