C++代码找问题大赛 I
C++代码找问题大赛 I:
注意逝项: 本考试禁止复制(虽然我管不了)。
卷一:
找出以下代码的编译问题(P1001)
1.
#include<bits/stdc++.h>
int a,b;
int main(){
cin>>a>>b;
cout<<a+b;
return 0;
}
2.
#include<bits/stdc++.h>>
using namespace std;
int a,b;
int main(){
cin>>a>>b;
cout<<a+b;
return 0;
}
3.
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
cin<<a<<b;
cout>>a+b;
return 0;
}
4.
#include<bit/stdc++.h>
using namespace std;
int a,b;
int main(){
cin>>a>>b;
cout<<a+b;
return 0;
}
5.
#include<bits/stdc++.h>
using namesapce std;
int a,b;
int main(){
cin>>a>>b;
cout<<a+b;
return 0;
}
找出以下代码的编译问题(P3379)
1.
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char c=getchar();
while(c>57||c<48){
if(c==45) f=-1;
c=getchar();
}
while(c<58&&c>47){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void write(int x){
if(x<0) putchar(45),x=-x;
if(x>9) write(x/10);
putchar(x%10|48);
}
int n=read(),m=read();
int r=read();
int v[500005];
int h[500005],to[1000005],nxt[1000005],idx;
void add(int x,int y){to[++idx]=y;nxt[idx]=h[x];h[x]=idx;}
int fa[500005],sz[500005],son[500005],dep[500005];
void dfs1(int x,int fat){
fa[x]=fat;
dep[x]=dep[fat]+1;
sz[x]=1;
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]==fat) continue;
dfs1(to[ep],x);
if(sz[to[ep]]>sz[son[x]]) son[x]=to[ep];
sz[x]+=sz[to[ep]];
}
}
int top[500005],dfn[500005],dcnt;
void dfs2(int x,int tp){
dfn[x]=++dcnt;
top[x]=tp;
if(son[x]==0) return;
dfs2(son[x],tp);
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]!=fa[x]&&to[ep]!=son[x]) dfs2(to[ep],to[ep]);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(r);
dfs2(r,r);
for(int i=1;i<=m;i++){
int x=read(),y=read();
write(LCA(x,y)),putchar(10);
}
}
2.
#include<bits/stdc++.h>
using namespace std;
void read(){
int x=0,f=1;
char c=getchar();
while(c>57||c<48){
if(c==45) f=-1;
c=getchar();
}
while(c<58&&c>47){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void write(int x){
if(x<0) putchar(45),x=-x;
if(x>9) write(x/10);
putchar(x%10|48);
}
int n=read(),m=read();
int r=read();
int v[500005];
int h[500005],to[1000005],nxt[1000005],idx;
void add(int x,int y){to[++idx]=y;nxt[idx]=h[x];h[x]=idx;}
int fa[500005],sz[500005],son[500005],dep[500005];
void dfs1(int x,int fat=0){
fa[x]=fat;
dep[x]=dep[fat]+1;
sz[x]=1;
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]==fat) continue;
dfs1(to[ep],x);
if(sz[to[ep]]>sz[son[x]]) son[x]=to[ep];
sz[x]+=sz[to[ep]];
}
}
int top[500005],dfn[500005],dcnt;
void dfs2(int x,int tp){
dfn[x]=++dcnt;
top[x]=tp;
if(son[x]==0) return;
dfs2(son[x],tp);
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]!=fa[x]&&to[ep]!=son[x]) dfs2(to[ep],to[ep]);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(r);
dfs2(r,r);
for(int i=1;i<=m;i++){
int x=read(),y=read();
write(LCA(x,y)),putchar(10);
}
}
3.
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char c=getchar();
while(c>57||c<48){
if(c==45) f=-1;
c=getchar();
}
while(c<58&&c>47){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void write(int x){
if(x<0) putchar(45),x=-x;
if(x>9) write(x/10);
putchar(x%10|48);
}
int n=read(),m=read();
int r=read();
int v[500005];
int h[500005],to[1000005],nxt[1000005],idx;
void add(int x,int y){to[++idx]=y;nxt[idx]=h[x];h[x]=idx;}
int fa[500005],sz[500005],son[500005],dep[500005];
void dfs1(int x,int fat=0){
fa[x]=fat;
dep[x]=dep[fat]+1;
sz[x]=1;
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]==fat) continue;
dfs1(to[ep],x);
if(sz[to[ep]]>sz[son[x]]) son[x]=to[ep];
sz[x]+=sz[to[ep]];
}
}
int top[500005],dfn[500005],dcnt;
void dfs2(int x,int tp){
dfn[x]=++dcnt;
top[x]=tp;
if(son[x]==0) return;
dfs2(son[x],tp);
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]!=fa[x]&&to[ep]!=son[x]) dfs2(to[ep],to[ep]);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(r);
dfs2(r,r);
for(int i=1;i<=m;i++){
int x=read(),y=read();
write(LCA(x,y)),putcahr(10);
}
}
4.
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char c=getchar();
while(c>57||c<48){
if(c==45) f=-1;
c=getchar();
}
while(c<58&&c>47){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void write(int x){
if(x<0) putchar(45),x=-x;
if(x>9) write(x/10);
putchar(x%10|48);
}
int n=read(),m=read();
int r=read();
int v[500005];
int h[500005],to[1000005],nxt[1000005],idx;
void add(int x,int y){to[++idx]=y;nxt[idx]=h[x];h[x]=idx}
int fa[500005],sz[500005],son[500005],dep[500005];
void dfs1(int x,int fat=0){
fa[x]=fat;
dep[x]=dep[fat]+1;
sz[x]=1;
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]==fat) continue;
dfs1(to[ep],x);
if(sz[to[ep]]>sz[son[x]]) son[x]=to[ep];
sz[x]+=sz[to[ep]];
}
}
int top[500005],dfn[500005],dcnt;
void dfs2(int x,int tp){
dfn[x]=++dcnt;
top[x]=tp;
if(son[x]==0) return;
dfs2(son[x],tp);
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]!=fa[x]&&to[ep]!=son[x]) dfs2(to[ep],to[ep]);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(r);
dfs2(r,r);
for(int i=1;i<=m;i++){
int x=read(),y=read();
write(LCA(x,y)),putchar(10);
}
}
5.
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char c=getchar();
while(c>57||c<48){
if(c==45) f=-1;
c=getchar();
}
while(c<58&&c>47){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void write(int x){
if(x<0) putchar(45),x=-x;
if(x>9) write(x/10);
putchar(x%10|48);
}
int n=read(),m=read();
int r=read();
int v[500005];
int h[500005],to[1000005],nxt[1000005],idx;
void add(int x,int y){to[++idx]=y;nxt[idx]=h[x];h[x]=idx;}
int fa[500005],sz[500005],son[500005],dep[500005];
void dfs1(int x,int fat=0){
fa[x]=fat;
dep[x]=dep[fat]+1;
sz[x]=1;
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]==fat) comtinue;
dfs1(to[ep],x);
if(sz[to[ep]]>sz[son[x]]) son[x]=to[ep];
sz[x]+=sz[to[ep]];
}
}
int top[500005],dfn[500005],dcnt;
void dfs2(int x,int tp){
dfn[x]=++dcnt;
top[x]=tp;
if(son[x]==0) return;
dfs2(son[x],tp);
for(int ep=h[x];ep;ep=nxt[ep]){
if(to[ep]!=fa[x]&&to[ep]!=son[x]) dfs2(to[ep],to[ep]);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs1(r);
dfs2(r,r);
for(int i=1;i<=m;i++){
int x=read(),y=read();
write(LCA(x,y)),putchar(10);
}
}
答案
下一卷