A.
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; char s[65]; int main() { int n;scanf("%d",&n); scanf("%s",s); int x=0; for(int i=0;i<n;i+=x){ printf("%c",s[i]); x++; } puts(""); return 0; }
B.
#include <iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; int a[100005]; int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); int ans=min(a[n]-a[2],a[n-1]-a[1]); printf("%d ",ans); return 0; }
C.
将n写成k个2的幂次之和,可以先写n个1,如果k>n就输出NO,否则将多出来的(n-k)个1分配到前k个1中
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int a[200005]; int qpow(int a,int b){ int ret=1; while(b){ if(b&1) ret=ret*a; a=a*a; b>>=1; } return ret; } int main() { int n,k; scanf("%d%d",&n,&k); if(k>n){ printf("NO "); return 0; } int r=n-k,cur=1; while(r>0&&cur<=k){ for(int i=1;;i++){ int num=qpow(2,i); if(num-1>r){ a[cur]+=num/2-1,r-=num/2-1; cur++; break; } } } if(r>0) puts("NO"); else{ puts("YES"); for(int i=k;i>=1;i--){ if(i!=k) printf(" "); printf("%d",a[i]+1); } puts(""); } return 0; }
D.
给定每个人的下个人和下下个人是谁,解这n个人的排列顺序;先把n个人排列的圈构建出来,再确定正方向
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; vector<int> edge[200005]; int vis[200005]; int ans[200005]; void dfs(int n,int u){ int siz=edge[u].size(); for(int i=0;i<siz;i++){ int v=edge[u][i]; if(!vis[v]){ ans[n+1]=v; vis[v]=1;dfs(n+1,v); } } } int main() { int n;scanf("%d",&n); int a,b; for(int i=1;i<=n;i++){ int u,v; scanf("%d%d",&u,&v); if(i==1) {a=u,b=v; if(a>b) swap(a,b);} edge[u].push_back(v); edge[v].push_back(u); } ans[1]=1; vis[1]=1; dfs(1,1); if(((ans[2]<ans[3])&&(a==ans[2]&&b==ans[3]))||((ans[2]>ans[3])&&(a==ans[3]&&b==ans[2]))){ for(int i=1;i<=n;i++){ if(i!=1) printf(" "); printf("%d",ans[i]); } puts(""); } else{ for(int i=n;i>=1;i--){ if(i!=n) printf(" "); printf("%d",ans[i]); } puts(""); } return 0; }