1030 训练课 错题集 黄梓涵
HZH114_514 · · 个人记录
::::warning[WARNING]{open} PLEAZE PUT THIZ INTO LUOGU. ::::
D46943 \Huge 大 数比较
⌨ 完整代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
string x,y;
cin>>x>>y;
if(x[0]!='-'&&y[0]!='-')
{
if(x.size()>y.size())
cout << "1\n";
else if(x.size()<y.size())
cout << "0\n";
else if(x.size()==y.size())
{
if(x>=y)
cout << "1\n";
else
cout << "0\n";
}
}
else if(x[0]=='-'&&y[0]!='-')
{
cout << "0\n";
}
else if(y[0]=='-'&&x[0]!='-')
{
cout << "1\n";
}
else
{
if(x.size()>y.size())
cout << "0\n";
else if(x.size()<y.size())
cout << "1\n";
else if(x.size()==y.size())
{
if(x<=y)
cout << "1\n";
else
cout << "0\n";
}
}
}
return 0;
}
🧠思路问题
::::info[点击来展开一些东西]
看到题目,我们不难想到用整形变量 int 来存储两个数,但一看数据范围:只能存储这么大的数↓
接下来统计总和
D46944 因子化简
🧠思路问题
看到题目描述,可以想到唯一分解定理,它可以一边把给定数拆的细碎一边求出他的质因数
我们每次分解一个质数,若这个分解次数
最后如果被拆的数不是
最后输出即可。
没优化,95分,气笑了😈
⌨ 完整代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
void test01()
{
int n,k;
cin>>n>>k;
int sus=n;
for(int i=2;i*i<=sus;i++)
{
int ans=0;
while(sus!=1&&sus%i==0)
{
sus/=i;
ans++;
}
if(ans<k)
{
while(ans--)
{
n/=i;
}
}
}
if(sus!=1) n/=sus;
cout << n << "\n";
return;
}
signed main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--)
test01();
return 0;
}
D46945 Cowz on Zkatez G
本题目采用 Zpecial Judge ,即使输出和样例输出不一样也可能100分
⌨ 完整代码
#include<bits/stdc++.h>
using namespace std;
int r,c,vis[115][80],dis[115][80];
char a[115][80];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1},ex,ey;
struct noob
{
int xx,yy;
};
deque<noob>q;
void print(noob n1)
{
cout << n1.xx << " " << n1.yy << "\n";
return;
}
void dfs(int x,int y)
{
vis[x][y]=1;
q.push_back({x,y});
if(x==ex&&y==ey)
{
for_each(q.begin(),q.end(),print);
exit(0);
}
for(int i=0;i<4;i++)
{
int nx=dx[i]+x,ny=dy[i]+y;
if(nx<1||nx>ex||ny<1||ny>ey) continue;
if(vis[nx][ny]==1) continue;
if(a[nx][ny]=='*') continue;
//q.push_back({nx,ny});
dfs(nx,ny);
}
q.pop_back();
return;
}
int main() {
cin>>ex>>ey;
for(int i=1;i<=ex;i++)
{
for(int j=1;j<=ey;j++)
{
cin>>a[i][j];
dis[i][j]=-1;
}
}
dfs(1,1);
return 0;
}
🧠思路问题
DFS输出迷宫路径
先把DFS模板套上,然后在 vis...; 后面补上一行 q.push_back({x,y}); ,注意: q 应为 deque 类型。 在 return; 前一行补上 q.pop_back();
最后补上一个判断终点的条件,若成功则打印整个 q 并且 exit(0); 然后就完成了。
:::::info[for_each]
科普:for_each 函数,包含在 <bits/stdc++.h> 库中,使用方法为 for_each(something.begin(),something.end(),PrintFunction); ,暂时不知道可不可以套进数组。
其中 PrintFunction 函数需要自己写。
基本格式为:
void PrintFunction(/*传进去的参数*/)
{
//打印参数的方式
return;
}
在 完整代码中,你可以看到 for_each(q.begin(),q.end(),print); ,所以 PrintFunction 在扔进括号里的时候不需要额外带括号。
::::info[参考代码]
#include<bits/stdc++.h>
using namespace std;
//打印方法函数
void print01(int val)
{
cout << val << " ";
return;
}
//for_each算法基本用法
void test01()
{
vector<int>v;
for(int i=1;i<=10;i++)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),print01);
cout << "\n";
return;
}
int main() {
test01();
return 0;
}
期望输入:
` `
期望输出:
1 2 3 4 5 6 7 8 9 10 |
|---|
总结:for_each是一个冷门的容器遍历函数,不想写for...的时候就可以用它 |
| ::::: |
推荐阅读:
- 1009 训练课 错题集 黄梓涵 队列
- 1016 训练课 错题集 黄梓涵
- 1023 训练课 错题集 黄梓涵
- 0918 训练课 错题集 黄梓涵 位运算
- 0911 训练课 错题集 黄梓涵