当前位置:网站首页>One hundred - day plan -- -- DAY2 brush
One hundred - day plan -- -- DAY2 brush
2022-08-05 02:46:00 【Ceylon_CC】
第一题【深基7.例2】质数筛
题目描述
输入 n n n a positive integer no greater than .要求全部储存在数组中,去除掉不是质数的数字,依次输出剩余的质数.
输入格式
第一行输入一个正整数 n n n,表示整数个数.
第二行输入 n n n 个正整数 a i a_i ai,以空格隔开.
输出格式
输出一行,依次输出 a i a_i ai the remaining prime numbers in ,以空格隔开.
样例 #1
样例输入 #1
5
3 4 5 6 7
样例输出 #1
3 5 7
提示
数据保证, 1 ≤ n ≤ 100 1\le n\le100 1≤n≤100, 1 ≤ a i ≤ 1 0 5 1 \leq a_i \leq 10^5 1≤ai≤105.
解题思路
- 1)This problem is essentially a prime number problem,Here is an efficient method for picking prime numbers,欧拉筛法.
- 2)我们创建两个数组,用
vis
数组标记每个对应下标的数是否为质数,为0则为质数,为1则为合数.用prime
数组记录遍历过程中找到的质数.- 3)When traversing, if the current number is a prime number, mark it,Then put the current number×The prime numbers from the previous sieve,This number is marked as composite.
参考代码
#include <bits/stdc++.h>
using namespace std;
long long n,m;
bool vis[10000001]={
1,1};
int Prime[10000001],k;
void prime(long long n)
{
for(int i=2;i<=n;i++)
{
if(!vis[i])
{
Prime[++k]=i;
}
for(int j=1;j<=k;j++)
{
if(Prime[j]*i>n)
{
break;
}
vis[Prime[j]*i]=true;
if(i%Prime[j]==0)
{
break;
}
}
}
}
int main()
{
cin>>n;
prime(100001);
for(int i=1;i<=n;i++)
{
int t;
cin>>t;
if(!vis[t])
{
cout<<t<<" ";
}
}
return 0;
}
第二题 马的遍历
题目描述
有一个 n × m n \times m n×m 的棋盘,在某个点 ( x , y ) (x, y) (x,y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步.
输入格式
输入只有一行四个整数,分别为 n , m , x , y n, m, x, y n,m,x,y.
输出格式
一个 n × m n \times m n×m 的矩阵,代表马到达某个点最少要走几步(左对齐,宽 5 5 5 格,不能到达则输出 − 1 -1 −1).
样例 #1
样例输入 #1
3 3 1 1
样例输出 #1
0 3 2
3 -1 1
2 1 4
提示
数据规模与约定
对于全部的测试点,保证 1 ≤ x ≤ n ≤ 400 1 \leq x \leq n \leq 400 1≤x≤n≤400, 1 ≤ y ≤ m ≤ 400 1 \leq y \leq m \leq 400 1≤y≤m≤400.
解题思路
- 1)This problem is essentially a breadth-first search problem,这里可以使用STL模板库 中的
<queue>
.- 2)The points that can be reached on the chessboard are entered in the order in which they are found,The first time to reach the target point is the final answer.
参考代码
#include<bits/stdc++.h>
using namespace std;
struct xy{
int x,y;
}node,top;
const int dx[8]={
-1,-2,-2,-1,1,2,2,1};
const int dy[8]={
2,1,-1,-2,2,1,-1,-2};
int a[401][401];
int vis[401][401];
int n,m;
void bfs(int x,int y,int step)
{
a[x][y]=step;
vis[x][y]=1;
queue<xy> Q;
node.x=x;
node.y=y;
Q.push(node);
while(!Q.empty()){
top=Q.front();
Q.pop();
for(int i=0;i<8;i++)
{
int nx=top.x+dx[i];
int ny=top.y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(vis[nx][ny]==0)
{
node.x=nx;
node.y=ny;
Q.push(node);
vis[nx][ny]=1;
a[nx][ny]=a[top.x][top.y]+1;
}
}
}
}
int main()
{
memset(a,-1,sizeof(a));
int x,y;
cin>>n>>m>>x>>y;
bfs(x,y,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
printf("%-5d",a[i][j]);
}
cout<<endl;
}
return 0;
}
第三题 [NOIP2001 普及组] 装箱问题
题目描述
有一个箱子容量为 V V V,同时有 n n n 个物品,每个物品有一个体积.
现在从 n n n 个物品中,任取若干个装入箱内(也可以不取),使箱子的剩余空间最小.输出这个最小值.
输入格式
第一行共一个整数 V V V,表示箱子容量.
第二行共一个整数 n n n,表示物品总数.
接下来 n n n 行,每行有一个正整数,表示第 i i i 个物品的体积.
输出格式
- 共一行一个整数,表示箱子最小剩余空间.
样例 #1
样例输入 #1
24
6
8
3
12
7
9
7
样例输出 #1
0
提示
对于 100 % 100\% 100% 数据,满足 0 < n ≤ 30 0<n \le 30 0<n≤30, 1 ≤ V ≤ 20000 1 \le V \le 20000 1≤V≤20000.
解题思路
- 1)This question asks to find the maximum amount that can be loaded,You can think of an object's weight as its value,Then turn the question into a simple one01背包问题.
- 2)Directly set the backpack board.
参考代码
#include<bits/stdc++.h>
using namespace std;
int dp[105000];
int main()
{
int v,n;
cin>>v>>n;
for(int i=1;i<=n;i++)
{
int V;
cin>>V;
for(int j=v;j>=V;j--)
{
dp[j]=max(dp[j],dp[j-V]+V);
}
}
cout<<v-dp[v];
return 0;
}
第四题 NASA的食物计划
题目背景
NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,Therefore, the history of the space shuttle was terminated under pressure from all parties,但是此类事情会不会在以后发生,谁也无法保证,在遇到这类航天问题时,The solution may only be to get astronauts out of the warehouse for repairs,But multiple repairs consume a lot of energy for astronauts,因此NASAI want to design a food plan,Load up on high-calorie foods with limited bulk and weight.
题目描述
Space shuttles are limited in size,Of course if the load is too heavy,Fuel wastes a lot of money,Each food item has its own volume、quality and calories,In the case of telling you the maximum value of volume and mass,Please output the maximum calorie value of the food plan you can achieve,Of course each food can only be used once.
输入格式
第一行 两个数 体积最大值(<400)and mass maximum(<400)
第二行 一个数 食品总数N(<50).
第三行-第3+N行
每行三个数 体积(<400) 质量(<400) Calories(<500)
输出格式
一个数 The maximum calorie that can be reached(int范围内)
样例 #1
样例输入 #1
320 350
4
160 40 120
80 110 240
220 70 310
40 400 220
样例输出 #1
550
解题思路
- 1)这道题目在01There is one more dimension on the basis of the backpack,Can be applied directly01背包中
f[j]=max{f[j],f[j-a[i]]+c[i]
的结论,Because the dimension has changed from one dimension to two dimensions,So set up a two-dimensional dynamic array- 2)套用01背包板子,Change the form a bit.
参考代码
#include<bits/stdc++.h>
using namespace std;
int dp[550][550];
int main()
{
int V,M,N;
cin>>V>>M>>N;
for(int i=1;i<=N;i++)
{
int v,m,ka;
cin>>v>>m>>ka;
for(int j=V;j>=v;j--)
{
for(int k=M;k>=m;k--)
{
dp[j][k]=max(dp[j][k],dp[j-v][k-m]+ka);
}
}
}
cout<<dp[V][M];
return 0;
}
边栏推荐
- Note that Weifang generally needs to pay attention to issuing invoices
- Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
- 优炫数据库的单节点如何转集群
- SDC简介
- Lexicon - the maximum depth of a binary tree
- UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
- [机缘参悟-60]:《兵者,诡道也》-2-孙子兵法解读
- How Jin Cang database correctness verification platform installation file
- 02 【开发服务器 资源模块】
- 2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
猜你喜欢
J9 Digital Currency: What is the creator economy of web3?
虚拟内存原理与技术
VSCode Change Default Terminal 如何修改vscode的默认terminal
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
【OpenCV 图像处理2】:OpenCV 基础知识
How OpenGL works
QT语言文件制作
What should I do if the self-incrementing id of online MySQL is exhausted?
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
Gantt chart is here, project management artifact, template is used directly
随机推荐
剑指offer专项突击版第20天
Syntax basics (variables, input and output, expressions and sequential statement completion)
1484. 按日期分组销售产品
QT:神奇QVarient
Programmer's Tanabata Romantic Moment
The 20th day of the special assault version of the sword offer
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
人人都在说的数据中台,你需要关注的核心特点是什么?
云原生(三十二) | Kubernetes篇之平台存储系统介绍
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
ARM Mailbox
Solve connect: The requested address is not valid in its context
QStyle平台风格
627. 变更性别
shell statement to modify txt file or sh file
从零到一快速学会三子棋
Multithreading (2)
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)