自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(94)
  • 收藏
  • 关注

原创 LeetCode 977. Squares of a Sorted Array

题目Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.给定一个非递减排序的数组,返回每个数组的平方的非递减排序。思路每个元素取平方,再排序。代码实现int* sortedSquares(int* nums, int numsSize, int* returnSize){

2020-12-29 21:39:59 197

原创 LeetCode 1295. Find Numbers with Even Number of Digits (C语言)

题目Given an array nums of integers, return how many of them contain an even number of digits.给定一个整数数组nums,返回位数是偶数的数组元素的个数。思路关键:根据一位数整除10一次得0,二位数整除10两次得0,...,n位数整除10n次得0。让每一个数组不断整除10直到变为0,每整除一次将其位数计数器digits+1。实现代码int findNumbers(int* nums, int

2020-12-28 20:42:15 184

原创 LeetCode 485. Max Consecutive Ones

题目Given a binary array, find the maximum number of consecutive 1s in this array.给定一个二进制数组(元素只有0和1的数组),找出数组中最多有多少个连续的1?思路遍历整个数组,如果遇到1则将计数器count+1,如果遇到0则将result赋值为Max{count, result},然后count归零。最后一段的可能是连续的1没有0,则此情况下无法触发检查result的代码,result少更新了一次,因此遍历完.

2020-12-28 20:29:51 112

原创 《算法笔记》codeup 100000584 问题 G: 找零钱

思路:本题同问题D及问题F类似,尽可能地按面额从大到小找钱即可解决。解答:/*《算法笔记》codeup 100000584 问题 G: 找零钱*/#include <cstdio>using namespace std;int main() { int amount; // 总额 int denomination[5] = {50, 20, 10, 5, 1};

2020-08-20 23:44:51 162

原创 《算法笔记》codeup 100000584 问题 F: 迷瘴 (详细注释)

思路:这道题和D那道修墙的题目差不多,对药水浓度进行从小到大的排序,依次取浓度最小的药水加入,知道达到或超过目标浓度。解答:/*《算法笔记》codeup 100000584 问题 F: 迷瘴*/#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int main() { int test_num; while(scanf("%d",

2020-08-20 11:35:23 225

原创 《算法笔记》codeup 100000584 问题 D: Repair the Wall

思路:每次用最长的砖,累加的长度超过窟窿的长度就算成功,输出用了的砖数;如果用完所有的砖后,累加的长度仍未达到窟窿的长度则输出“impossible”。解答:/*《算法笔记》codeup 100000584 问题 D: Repair the Wall*/#include <cstdio>#include <algorithm>using namespace std;int cmp(int a, int b) { return a > b;

2020-08-16 12:46:48 149

原创 vscode 添加了头文件仍显示“未定义的标识符”

解决方法:在头文件语句下面添加一行using namespace std;

2020-08-16 11:50:28 5547 1

原创 《算法笔记》codeup_100000584 问题 B: 出租车费

思路:这题主要还是一个数学问题。所有方案中单位路费最低的情况是搭完阶段一和阶段二,平均每公里2.25元。因此将整段路程分为8公里长的小路程。最后剩下的不足8公里的部分有两种方案,要么重新计费,要么继续上一轮的计费,此后每公里为2.4元。易得这两种方案的车费分别为y = 2.4x和。当x<5时前者车费低,当x=5时两种方案的车费相等,当x>5时后者车费低。解答:#include <cstdio>using namespace std;const double eps

2020-08-01 18:44:08 203

原创 《算法笔记》codeup_100000584_A

思路:就是书中的区间贪心,不过我觉得对本题中的区间的排序按时间先后比较符合直观,所以排序方法的方向是与书中相反的。解答:#include <cstdio>#include <algorithm>using namespace std;const int max_num = 100;struct show { int start_time, end_time;} show_arr[max_num];

2020-07-29 21:39:29 127

原创 《算法笔记》codeup 100000583 问题 D: 八皇后 (N皇后代码解析)

思路:从棋盘的第一列开始,尝试在当前列的每一行摆放皇后。如果当前在行列上摆放暂时不会引起冲突,则保存摆放的位置。然后进入下一行重复上述操作,直到每一列都完成操作。解答:#include <cstdio>#include <iostream>#include <cmath> #include <algorithm>using namespace std;/* 从棋盘的第一列开始,尝试在当前列的每一行摆放皇后。 如果当前在

2020-07-05 20:13:25 2172

原创 《算法笔记》codeup_100000583_B

思路:输出的每一行分为:空格、一个0、斐波那契数列这三个部分。想清楚每一行空格的数量和斐波那契数列的项数即可。解答:#include <cstdio>using namespace std;int fib_arr[25];int Fib(int n) { if(n == 1) return 1; else if(n == 2) return 1; else return(Fib(n - 1) + F

2020-07-03 12:14:17 163

原创 《算法笔记》codeup_100000583_A

思路:一块:只有“吃一块”一种方案。两块:“两天各吃一块”和“一天吃两块”两种方案。三块:如果第一天吃一块,则第二天开始回到共有两块的情况;如果第一天吃两块,则第二天回到共有一块的情况。N块:如果第一天吃一块,则第二天开始回到共有N-1块的情况;如果第一天吃两块,则第二天回到共有N-2块的情况。解答:#include <cstdio>using namespace std;int get_plan_num(int choco_num) { if(choc

2020-07-03 12:10:59 127

原创 《算法笔记》codeup_100000582_D

思路:思路1:编写一个删除给定字符串中所有给定字符的方法,遍历str2删除str1中的每一个str2元素。思路2:设置一个大小等于str1的整型数组if_exist,初始化为0。遍历str1,对于str1中的第i个字符遍历str2判断该字符是否存在于str2中,如果存在则置if_exist[i]为1。遍历str1,如果if_exist[i]为0,则输出str1[i]。思路3:设置一个大小为255的整型数组num,初始化为0,数组下标代表ASCII码,数组元素的值表示当前ASCII码的个数。遍历s

2020-06-27 18:06:36 176

原创 《算法笔记》codeup_100000582_C

思路:设置一个数组a,a[i]表示数i出现的次数。解答:#include <cstdio>#include <cstring>using namespace std;int main() { int data_num; while(scanf("%d", &data_num) != EOF) { // 保存输入的数据 int data[data_num]; for(int i = 0; i

2020-06-26 21:12:38 170

原创 《算法笔记》codeup_100000582_B

思路:设置两个一维数组分别保存数和组号。设置一个二维数组,第一维的下标i表示组号,第二维的下标j表示数的大小,数组的元素值表示第i组的j数的个数,这应用散列的思想。遍历每一组数据完成二维数组的赋值。然后分别对两个一维数组进行排序。最后分别遍历每一个组合不同且数不同的数据,访问这些数据的组号和数作为下标的二维数组元素的值,得到每个不同数据的个数并输出。解答:#include <cstdio>#include <cstring>#include <algorith

2020-06-26 10:13:27 204

原创 《算法笔记》codeup_100000582_A

思路:明白输出的含义就容易解出:第一行输出的1是读者1喜欢的2号书另外还有读者3喜欢,第二行输出的BeiJu是读者2喜欢的3号书没有其他人喜欢,第三行输出的1是读者3喜欢的2号书另外还有读者1喜欢,第四行输出的BeiJu是读者4喜欢的1号书另外没有其他人喜欢。解答:#include <cstdio>#include <cstring>using namespace std;int main() { int reader_num, book_num;

2020-06-23 18:05:39 156

原创 《算法笔记》codeup_100000581_I

思路:计算好每个人的分数后按题目要求排序即可。解答:#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>using namespace std;struct stu{ char id[25]; int score = 0;};bool cmp(stu a, stu b) { if(a.score == b.s

2020-06-16 11:47:53 226

原创 《算法笔记》codeup_100000581_H

思路:对输入数组进行默认排序,反向扫描并输出奇数,然后正向扫描并输出偶数。解答:#include <cstdio>#include <algorithm>using namespace std;int main() { int arr[10]; while(scanf("%d %d %d %d %d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &

2020-06-16 08:45:33 124

原创 《算法笔记》codeup_100000581_G

思路:按题目要求模拟即可,注意元素个数为偶数时中间两个数的下标的表达式。解答:#include <cstdio>#include <algorithm>using namespace std;int main() { int num; while (1) { scanf("%d", &num); if(num == 0) break; int arr[num];

2020-06-15 21:31:43 160

原创 《算法笔记》codeup_100000581_F

思路:使用sort()解答:#include <cstdio>#include <algorithm>using namespace std;struct mouse{ int weight; char color[10];};bool cmp(mouse a, mouse b) { return a.weight > b.weight;}int main() { int mouse_num; whil

2020-06-15 18:07:41 155

原创 《算法笔记》codeup_100000581_E

思路:主要是想清楚计算每个方向的和时加数下标的变化。解答:#include <cstdio>#include <algorithm>#include <cstring>using namespace std;bool cmp(int a, int b) { return a > b;}int main() { int m; while(scanf("%d", &m) != EOF) { i

2020-06-15 17:38:48 210

原创 《算法笔记》codeup_100000581_D

思路:直接用sort()排序解答:#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int main() { char str[200]; while(gets(str) != NULL) { int str_len = strlen(str); sort(str, str + str_len);

2020-06-15 11:31:38 132

原创 《算法笔记》codeup_100000581_C

思路:主要是弄清楚如何写出cmp函数解答:#include <cstdio>#include <algorithm>//#include <string>//#include <iostream>#include <cstring>using namespace std;struct Student { char id[10]; char name[10]; int score;}stu_arr

2020-06-15 11:10:40 180

原创 《算法笔记》codeup_100000581_B

思路:找出最大的元素,与最后的元素交换位置,然后只要对前n-1个元素进行排序即可。解答:#include <cstdio>using namespace std;void select_sort(int a[], int size) { for(int i = 0; i <= size - 1; i++) { int min = i; for(int j = i + 1; j <= size - 1; j++) {

2020-06-11 17:31:25 172

原创 《算法笔记》codeup_100000581_A

思路:简简单单的排序解答:#include <cstdio>using namespace std;void select_sort(int a[], int size) { for(int i = 0; i <= size - 1; i++) { int min = i; for(int j = i + 1; j <= size - 1; j++) { if(a[j] < a[min])

2020-06-11 16:27:34 135

原创 《算法笔记》codeup_100000580_I

思路:大致和数组逆置那道题类似,设置一个表示结果的字符串初始值为"YES",依次比较字符串首尾元素,出现不同则置结果字符串为"NO",最后输出结果字符串。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int main() { char str[1000]; char resul

2020-06-10 18:51:32 111

原创 《算法笔记》codeup_100000580_H

思路:用一个二维字符数组保存所有输入的字符串,倒序输出二维字符数组中的四个字符串。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int main() { int sample_num; while(scanf("%d", &sample_num) != EOF) {

2020-06-10 18:36:13 121

原创 《算法笔记》codeup_100000580_G

思路:用strlen()获取长度。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int main() { int sample_num; while(scanf("%d", &sample_num) != EOF) { while(sample_num-

2020-06-10 17:47:02 142

原创 《算法笔记》codeup_100000580_F

思路:算出首尾序号的表达式,依次调换后输出即可。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int main() { char str[1000]; while(gets(str)) { int str_len = strlen(str); f

2020-06-10 17:33:07 97

原创 《算法笔记》codeup_100000580_E

思路:获取字符串和字符,遍历字符串,当遍历到的元素和字符不相当时输出。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int main() { char str[1000]; while(gets(str)) { char c; c = getchar(); //

2020-06-10 17:11:34 265 1

原创 《算法笔记》codeup_100000580_D

思路:先通过字符串匹配算法找到模式在文本中出现的位置,然后遍历文本,在非匹配位置将文本直接赋值到输出字符数组,在匹配位置将整个替换字符赋值到输出字符数组,然后文本字符串跳过模式长度继续遍历。解答:#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int BF3(char txt[], char pat[

2020-06-09 12:32:35 198

原创 《算法笔记》codeup_100000580_C

思路:通过字符串匹配算法找到模式在文本中出现的位置,再删除文本中的模式后输出,或者跳过文本中的模式输出解答:为了把BF算法分离出来,代码写复杂了#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int BF2(char txt[], char pat[], int match[]) { int i

2020-06-06 21:13:18 218

原创 《算法笔记》codeup_100000579_D

思路:使用B题中实现的通用转换算法即可。解答:#include <cstdio>#include <cstring>using namespace std;void oct_to_char(int temp, int b, char output[]) { //将10进制数temp转换成字符表示的b进制数,存储在字符串output中 int i =...

2020-05-06 20:06:18 169

原创 《算法笔记》codeup_100000579_C

思路:仿照竖式除法,首先让输入的待转换的数除以2,除得的余数是结果的最后一位,除得的商作为新的被除数循环进行前面的操作,依次从后向前地获取到转换结果的每一位。0或1作为被除数时结束循环,此时除得的商始终是0,可以以此为标识结束循环。解答:#include <cstdio>#include <cstring>using namespace std;int...

2020-05-06 20:02:53 149

原创 《算法笔记》codeup_100000579_B

思路:使用字符串存储输入的待转换数及转换后的结果,先转换为十进制,再从十进制转换到目标进制。解答:#include <cstdio>#include <cstring>using namespace std;int char_to_oct(int a, char input[]) {//按进制a将input字符串转换为10进制数 int sum = ...

2020-05-05 22:30:25 209

原创 《算法笔记》codeup_100000578_E

解答:#include <cstdio>using namespace std;int month[13][2] = { {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31,...

2020-04-29 18:40:55 132

原创 《算法笔记》codeup_100000578_D

解答:#include <cstdio>using namespace std;int month[13][2] = { {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31,...

2020-04-29 18:36:19 97

翻译 《线性代数及其应用 第四版》习题1.4

18. 行化简的结果显示,矩阵B化简后的简化阶梯型只有三行包含主元位置:根据1.4的定理4,由于B不是每行都有主元位置,因此B的列向量的线性组合不能表示所有R4中的向量。要注意B的列向量也不能张成R3,因为B的列向量位于R4而不是R3中。...

2020-04-29 18:21:31 1948

原创 《算法笔记》codeup_100000578_B

解答:#include <cstdio>#include <iostream>#include <cstring>using namespace std;/*21 December 20125 January 2013*/char month_name[12][10] = {"January", "February", "March"...

2020-04-27 16:40:01 151

原创 《算法笔记》codeup_100000577_D

思路:将每个*看作坐标系上的点,先全部点置为*,然后消去两侧的*,最后消去中间间隔的*。解答:#include <cstdio>using namespace std;int main() { int layer_num; while(scanf("%d", &layer_num) != EOF) { int h = 2*layer_num-1;...

2020-04-24 18:53:56 118

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除