1.平方数检测
二分法,手写sqrt函数。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll my_sqrt(ll x)
{
ll l=0,r=x,ans=0;
while(l<=r)
{
ll mid=(l+r)/2;
if(mid*mid>=x)ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
ll n,x;
cin>>n;
for(ll i=1;i<=n;i++)
{
cin>>x;
ll t=my_sqrt(x);
if(t*t==x)printf("1");
else printf("0");
}
return 0;
}
2.卷积
直接模拟。
#include <bits/stdc++.h>
using namespace std;
const int N=510,inf=0x3f3f3f3f;
typedef long long ll;
int a[N][N],b[N][N];
int main()
{
ios::sync_with_stdio(false);
int n1,m1,n2,m2;
cin>>n1>>m1>>n2>>m2;
for(int i=1;i<=n1;i++)
for(int j=1;j<=m1;j++)
cin>>a[i][j];
for(int i=1;i<=n2;i++)
for(int j=1;j<=m2;j++)
cin>>b[i][j];
int n3=n2-n1+1;
int m3=m2-m1+1;
for(int i=1;i<=n3;i++)
for(int j=1;j<=m3;j++)
{
int sum=0;
for(int k1=1;k1<=n1;k1++)
for(int k2=1;k2<=m1;k2++)
sum+=a[k1][k2]*b[k1+i-1][k2+j-1];
j==m3?printf("%d\n",sum):printf("%d ",sum);
}
return 0;
}
3.优惠券
不会
4.幸运二叉树
数据结构
没满分
5.阵营判断
判断二分图。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,inf=0x3f3f3f3f;
typedef long long ll;
int head[N],cnt,color[N];
struct edge
{
int to,next;
}e[N<<1];
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
}
bool dfs(int u,int c)
{
color[u]=c;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].to;
if(color[v]==u)return 0;
if(color[v]==-1&&!dfs(v,c^1))return 0;
}
return 1;
}
bool judge(int n)
{
for(int i=0;i<n;i++)
if(color[i]==-1&&!dfs(i,0))return 0;
return 1;
}
int main()
{
ios::sync_with_stdio(false);
memset(head,-1,sizeof(head));
memset(color,-1,sizeof(color));
int n,m,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>m;
for(int j=0;j<m;j++)
{
cin>>x;
add(i,x);
}
}
if(judge(n))printf("true\n");
else printf("false\n");
return 0;
}
6.子集和
尺取法。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,inf=0x3f3f3f3f;
typedef long long ll;
ll n,k,a[N],sum;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(ll i=0;i<n;i++)
cin>>a[i];
cin>>k;
ll ans=-1;
for(ll l=0,r=0;r<n;r++)
{
sum+=a[r];
if(sum==k){ans=r;continue;}
while(sum>k&&l<=r)
{
sum-=a[l];
if(sum==k)ans=r;
l++;
}
}
printf("%lld\n",ans);
return 0;
}
/*
5
1 2 3 4 5
5
ans:4
*/
Comments | NOTHING