![](https://img-blog.csdnimg.cn/20200803231225799.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法练习
文章平均质量分 64
算法
lzh~
Keep on going never give up.
展开
-
背包问题代码汇总(python)
中,找出满足条件(目标和)的目标值。最近做的几道题目总结。原创 2023-05-16 19:26:32 · 532 阅读 · 0 评论 -
202012-2 期末预测之最佳阈值
样例1输入60 01 01 13 15 17 1样例1输出3样例2输入85 15 05 02 13 04 0100000000 11 0样例2输出100000000满分代码用map<int,vector<int>>进行自动排序,去重,和vector的不定长存储分开两次计算一次计算小于自己的复合条件的一次计算大于等于自己的符合条件的#include<bits/stdc++.h>using names原创 2021-04-07 21:15:29 · 158 阅读 · 0 评论 -
202012-1期末预测之安全指数
代码#include<bits/stdc++.h>using namespace std;int main(){ int n,sum=0; cin >> n; for(int i=0; i<n; i++) { int a,b; cin >> a >> b; sum += (a*b); } cout << max(0,sum); return 0;}样例输入162 6010 1000 7原创 2021-04-05 19:18:22 · 91 阅读 · 0 评论 -
1060 Are They Equal (25 分)
1060 Are They EqualIf a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10^5 with simple chopping. Now given the number of significant digits on a machine and two flo原创 2021-03-10 18:52:21 · 92 阅读 · 0 评论 -
1059 Prime Factors (25 分)
1059 Prime FactorsGiven any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 × p2^k2 × ⋯ × pm^km.Input Specification:Each input file contains one test case which giv原创 2021-03-09 18:56:38 · 122 阅读 · 0 评论 -
1015 Reversible Primes (20 分)
1015 Reversible PrimesA reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.Now given any two positive i原创 2021-03-09 16:31:29 · 123 阅读 · 0 评论 -
求2-n的所有素数c++
最原始暴力解决#include<bits/stdc++.h>using namespace std;const int maxn = 101;// 统计100以内的素数 // 判断是否是素数 bool isPrime(int num) { if (num<=1) return false; for(int i=2; i<num; i++) { if(num%i == 0) { return false; } } return true;}原创 2021-03-06 16:29:07 · 2157 阅读 · 0 评论 -
1037 Magic Coupon (25 分)
1037 Magic CouponThe magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offer原创 2021-03-06 15:06:45 · 143 阅读 · 0 评论 -
求最小公倍数和最大公约数
#include<bits/stdc++.h>using namespace std;// 辗转相除法求最大公约数 int gcd(int a,int b) { int maxx = max(a,b); int minn = min(a,b); if(maxx%minn == 0) { return minn; } return gcd(minn,maxx%minn);}// 利用最大公约数求最小公倍数 int lcm(int a,int b) { retu原创 2021-03-05 19:57:26 · 93 阅读 · 0 评论 -
1049 Counting Ones (22/30 分)
1049 Counting OnesThe task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.Input Specificat原创 2021-03-05 17:57:13 · 73 阅读 · 0 评论 -
1101 Quick Sort (25 分)
1101 Quick SortThere is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to it原创 2021-03-05 16:21:49 · 103 阅读 · 0 评论 -
1104 Sum of Number Segments (20 分)
1104 Sum of Number SegmentsGiven a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2)原创 2021-03-05 15:48:35 · 134 阅读 · 0 评论 -
1089 Insert or Merge (25 分)
1089 Insert or MergeAccording to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs withi原创 2021-03-04 21:42:28 · 141 阅读 · 1 评论 -
c++ 判断字符是否是数字或字母(以及大小写转换)
transform 字符串str的大小写转换transform(str.begin(), str.end(), str.begin(), 转换操作);#include<bits/stdc++.h>using namespace std;int main(){ string str = "abcdAA123"; // 将字符串str全部转化为大写 transform(str.begin(), str.end(), str.begin(), ::toupper); cout原创 2021-03-04 19:46:48 · 2900 阅读 · 1 评论 -
c++中二分查找中的binary_search,lower_bound,upper_bound
注意:如果数组无序,请先用sort将数组排序binary_search(起始地址,结束地址,要查找的数值)返回值是 是否存在这么一个数,是一个bool值。#include<bits/stdc++.h>using namespace std;int a[100005];int main(){ int n; cin >> n; for(int i=0; i<n; i++) { cin >> a[i]; } sort(a,a+n);原创 2021-03-04 18:49:07 · 413 阅读 · 1 评论 -
1085 Perfect Sequence (25 分)
1085 Perfect SequenceGiven a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.Now given a sequence and a p原创 2021-03-04 15:49:05 · 109 阅读 · 1 评论 -
1048 Find Coins (25 分)
1048 Find CoinsEva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the原创 2021-03-04 09:38:25 · 188 阅读 · 1 评论 -
1011 World Cup Betting (20 分)
1011 World Cup Betting代码#include<bits/stdc++.h>using namespace std;struct node { char flag; double v;}wtl[3];bool cmp(node a,node b) { return a.v > b.v;}int main(){ double value = 1.0; for(int i=0; i<3; i++) { do原创 2021-03-04 09:23:09 · 101 阅读 · 1 评论 -
1010 Radix (25 分)
1010 RadixGiven a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.Now for any pair of positive integers N1 and N2, your task is to find the原创 2021-03-03 11:07:28 · 114 阅读 · 0 评论 -
1067 Sort with Swap(0, i) (25 分)
1067 Sort with Swap(0, i)Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap op原创 2021-03-03 10:40:10 · 87 阅读 · 1 评论 -
1009 Product of Polynomials (25 分)
1009 Product of PolynomialsThis time, you are supposed to find A×B where A and B are two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:K N1原创 2021-03-02 18:05:36 · 131 阅读 · 0 评论 -
1092 To Buy or Not to Buy (20 分)
1092 To Buy or Not to BuyEva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces.原创 2021-03-02 12:08:49 · 137 阅读 · 0 评论 -
1008 Elevator (20 分)
1008 ElevatorThe highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, an原创 2021-03-02 10:51:06 · 76 阅读 · 0 评论 -
1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence SumGiven a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of原创 2021-03-02 10:21:16 · 89 阅读 · 0 评论 -
1084 Broken Keyboard (20 分)
1084 Broken KeyboardOn a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a string that you are supposed to type, and the string that you actua原创 2021-03-01 21:34:52 · 146 阅读 · 0 评论 -
1006 Sign In and Sign Out (25 分)
1006 Sign In and Sign OutAt the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones原创 2021-03-01 20:48:46 · 185 阅读 · 1 评论 -
1005 Spell It Right (20 分)
1005 Spell It RightGiven a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.Input Specification:Each input file contains one test case. Each case occupies one line which contain原创 2021-03-01 17:41:48 · 64 阅读 · 0 评论 -
1004 Counting Leaves (30 分)
1004 Counting LeavesA family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts with a line containing 0<N<100原创 2021-03-01 17:23:46 · 74 阅读 · 0 评论 -
1003 Emergency (25 分)
1003 EmergencyAs an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of ci原创 2021-02-26 15:35:19 · 131 阅读 · 0 评论 -
fill和memset函数(C++)
memset函数头文件 #include<string.h>因为memset函数按照字节填充,所以一般memset只能用来填充char型数组,(因为只有char型占一个字节)如果填充int型数组,除了0和-1,其他的不能。因为只有00000000 = 0,-1同理,如果我们把每一位都填充“1”,会导致变成填充入“11111111”。理论上只能初始化为0和-1,而且必须为数组,但是memset()函数还能将int型数组初始化为INF(0x3f3f3f3f)——最大值具体介绍https://原创 2021-02-25 16:00:18 · 691 阅读 · 0 评论 -
为什么无穷大总是0x3f3f3f3f而不是0x7fffffff?
如果问题中各数据的范围明确,那么无穷大的设定不是问题,在不明确的情况下,很多程序员都取0x7fffffff作为无穷大,因为这是32-bit int的最大值。如果这个无穷大只用于一般的比较(比如求最小值时min变量的初值),那么0x7fffffff确实是一个完美的选择,但是在更多的情况下,0x7fffffff并不是一个好的选择。 且听我慢慢道来,为什摸0x7fffffff为什摸不是个好的选择 1....转载 2021-02-25 15:14:30 · 872 阅读 · 0 评论 -
1002 A+B for Polynomials (25 分)
A+B for PolynomialsThis time, you are supposed to find A+B where A and B are two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:Output Specific原创 2021-02-25 00:32:46 · 115 阅读 · 0 评论 -
PAT——1001 A+B Format (20分)
题目: 1001 A+B FormatCalculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).Input Specification:Each input file contains one test case. Each case原创 2021-02-24 18:12:59 · 126 阅读 · 0 评论 -
百钱买百鸡(逐步优化)
题目描述:鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。时间复杂度从100x100x100优化到10第一种,直接根据题目条件进行编程:#include<bits/stdc++.h>using namespace std;int main() { for(int cock = 1; cock <= 100; cock++) { for(int hen = 1; hen原创 2020-09-15 19:12:31 · 738 阅读 · 0 评论 -
CSP-202006-2-稀疏向量
稀疏向量(传送门) 不知道为啥,用的四个数组就会报一个运行错误,只有60分,然后看了一个大佬写的用的vector<pair<int, int> >提交了之后就满分了。样例输入10 3 44 57 -310 11 104 205 307 40样例输出-20满分代码#include <bits/stdc++.h>using namespace std;int n,a,b;long long sum;vector<pair&l原创 2020-09-05 17:18:33 · 331 阅读 · 0 评论 -
CSP-202006-1-线性分类器
线性分类器(传送门) 长时间不写,有点手生,写了老半天满分代码#include <bits/stdc++.h>using namespace std;long n;int m;struct pos{ int x;//横坐标 int y;//纵坐标 int v;//每个点所在每条直线的上下,1表示上,-1表示下 int type;//分类点,0表示A,1表示B }a[1001];int main(){ cin >> n >>原创 2020-09-05 10:51:37 · 232 阅读 · 0 评论 -
CSP-201912-2-回收站选址
回收站选址(传送门) 一道水题,读懂题慢慢写就好了样例1输入71 22 10 01 11 02 00 1样例1输出00100样例2输入20 0- 100000 10样例2输出00000样例3输入119 1010 1011 1012 1013 1011 911 812 910 910 1112 11样例3输出...原创 2020-03-07 16:53:41 · 299 阅读 · 0 评论 -
CSP-201912-1-报数
报数(传送门) 一道水题,没什么可说的样例1输入20样例1输出2110样例2输入66样例2输出75115满分代码#include <bits/stdc++.h>using namespace std;int n,m;int a[4];int main(){ cin >> n; memset(a,0,si...原创 2020-03-07 16:48:11 · 551 阅读 · 0 评论 -
CSP-201909-2-小明种苹果(续)
小明种苹果(续)(传送门) 一道水题,但是这是自己当时第一次写csp的题,纯C语言呦(* ̄︶ ̄)样例1输入44 74 -7 -12 -55 73 -8 -6 59 -45 76 -5 -10 60 -25 80 -6 -15 59 0样例1输出222 1 0样例2输入54 10 0 9 04 10 -2 7 02 ...原创 2020-03-07 16:43:42 · 163 阅读 · 0 评论 -
CSP-201909-1-小明种苹果
二十四点(传送门) 一道水题,这是自己当时写的第一道csp的题,还是用纯C语言写的,☆ㄟ( ・ө・ )ㄏ☆样例1输入3373-8-6-476 -5 -10 -880-6-150样例1输出167 2 23样例2输入2210 -3 -115-40样例2输出17 1 4满分代码#include <stdio.h>int a[1000][1000];...原创 2020-03-07 16:35:37 · 247 阅读 · 0 评论