Problem A. Bad Horse
题意:给n个关系,每个关系有两个人,要求把这些人分成两组,每组里的人之间都没有关系。
二分图染色即可。关系=边,人=点。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cmath> #define ll __int64 #define INF 0x3fffffff using namespace std; int G[105][105]; int col[105]; int k; bool bfs(int s) { queue<int> p; p.push(s); col[s] = 1; while(!p.empty()) { int from = p.front(); p.pop(); for(int i = 1; i <= k; i++) { if(G[from][i] && col[i] == -1) { p.push(i); col[i] = !col[from];//染成不同的颜色 } if(G[from][i] && col[from] == col[i])//颜色有相同,则不是二分图 return false; } } return true; } int main() { //freopen("d:\\A-small-2-attempt0.in","r",stdin); //freopen("d:\\output.txt","w",stdout); int n,T,t=1; cin>>T; while(T--){ cin>>n; k=0; map<string,int>m; memset(G,0,sizeof(G)); memset(col, -1, sizeof(col)); for(int i=0;i<n;i++){ char a[105],b[105]; cin>>a>>b; if(!m[a]) m[a]=++k; if(!m[b]) m[b]=++k; G[m[a]][m[b]]=1; G[m[b]][m[a]]=1; } bool flag=false; for(int i=1;i<=k;i++){ if(col[i]==-1&&!bfs(i)){ flag=true; break; } } if(!flag) cout<<"Case #"<<t++<<": "<<"Yes"<<endl; else cout<<"Case #"<<t++<<": "<<"No"<<endl; } return 0; }<strong> </strong>
Problem B. Captain Hammer
题意:给出斜抛运动的速度和水平位移,求斜抛角度。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cmath> #define ll __int64 #define INF 0x3fffffff using namespace std; int main() { //freopen("d:\\Test.txt","r",stdin); //freopen("d:\\output.txt","w",stdout); int T,t=1; cin>>T; while(T--){ double v,d,c; cin>>v>>d; c=asin(9.8*d/v/v) * 180.0/3.1415926/2.0; cout<<"Case #"<<t++<<": "; printf("%.7f\n",c); } return 0; }
Problem C. Moist
题意:给n个字符串,要求按字典序排序,排序方法为从上往下扫,遇到一个比上面里的小的,就交换位置。求交换次数
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cmath> #define ll __int64 #define INF 0x3fffffff using namespace std; char s[105][105]; int n; int main() { //freopen("d:\\C-small-2-attempt0.in","r",stdin); //freopen("d:\\output.txt","w",stdout); int T,t=1; cin>>T; while(T--){ cin>>n; getchar(); for(int i=0;i<n;i++){ gets(s[i]); } int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ if(strcmp(s[j],s[i])>0){ swap(s[j],s[i]); ans++; break; } } } cout<<"Case #"<<t++<<": "<<ans<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Google APAC 2016 University Graduates Test(google校招笔试)
原文地址:http://blog.csdn.net/u012198382/article/details/47703169