当前位置:网站首页>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;
}
边栏推荐
- matlab绘制用颜色表示模值大小的箭头图
- (11) Metaclass
- Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
- 【 2 】 OpenCV image processing: basic knowledge of OpenCV
- 数据增强Mixup原理与代码解读
- 01 [Foreword Basic Use Core Concepts]
- 使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
- 程序员的七夕浪漫时刻
- 金仓数据库如何验证安装文件平台正确性
- VSCode Change Default Terminal how to modify the Default Terminal VSCode
猜你喜欢
![Tencent Cloud [Hiflow] New Era Automation Tool](/img/ac/5c61424f22cd9fed74dcd529fdb6a4.png)
Tencent Cloud [Hiflow] New Era Automation Tool
![[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)](/img/ee/6b52072c841af99488dc0c1141c74c.png)
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)

Regular expression to match a certain string in the middle

常见的硬件延迟

DAY23:命令执行&代码执行漏洞

J9 Digital Currency: What is the creator economy of web3?
![01 [Foreword Basic Use Core Concepts]](/img/90/67537d5fad28d68766ca85b887839e.png)
01 [Foreword Basic Use Core Concepts]

DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)

Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail

shell语句修改txt文件或者sh文件
随机推荐
Programmer's Tanabata Romantic Moment
基于左序遍历的数据存储实践
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
你要的七夕文案,已为您整理好!
Access Characteristics of Constructor under Inheritance Relationship
mysql can't Execute, please solve it
RAID磁盘阵列
sql server 安装提示用户名不存在
C language diary 9 3 kinds of statements of if
Common hardware delays
DAY23:命令执行&代码执行漏洞
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
ARM Mailbox
private package
Introduction to SDC
Access Characteristics of Constructor under Inheritance Relationship
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
627. Change of gender
How OpenGL works
Solve connect: The requested address is not valid in its context