这是我参与更文挑战的第5天,活动详情查看:更文挑战
好可惜啊,昨天不可抗力的缺席了一天,想要的掘金徽章套装盒没了,看来无缘徽章了😢😢😢~
开始算法刷题!!!
入门练手
数组
数组是存放在连续内存空间上的相同类型数据的集合。
二分法
编号35:搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
class Solution{
public:
int searchInsert(vector<int>& nums, int target){
for(int i = 0; i< nums.size();i++){
}
}
};
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() {
int cards[55];
int mids[55];
int ends[55];
int times = 0;
for (int i = 1; i < 55; i++) {
cards[i] = i;
}
scanf("%d", ×);
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;
}
思考和总结
改进:最后的输出过于繁琐,可以简化如下。
for(int i = 1;i<=55;i++)
{
if(i != 1) printf(" ");
cards[i]--;
printf("%c%d",CHA[cards[i]/13],cards[i]%13+1);//通过字符数组输出字母,通过除法判断字母;后者通过原数字-1避开13%13==0的情况
}