09年浙大研究生复试机试

    技术2022-05-20  50

     

    1.xxx定律:

    http://acm.hdu.edu.cn/showproblem.php?pid=3782

    //http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=328 //xxx定律 #include <iostream> using namespace std; int main() { int n; while(cin >> n) { if(n==0) break; int cnt=0; while(n!=1) { if(n&1==1)//odd num n=(3*n+1)>>1; else n = n >> 1; cnt++; } cout << cnt << endl; } return 0; }  

     

    2.ZOJ:

    http://acm.hdu.edu.cn/showproblem.php?pid=3783

    //http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=328 //ZOJ #include <iostream> #include <cstring> using namespace std; int main() { char str[100+5]; while(cin >> str && (strcmp(str,"E")!=0) ) { int len = strlen(str); int z_cnt,o_cnt,j_cnt; z_cnt = o_cnt = j_cnt = 0; for(int i=0;i<len;i++) if(str[i]=='Z')z_cnt++; else if(str[i]=='O') o_cnt++; else if(str[i]=='J') j_cnt++; else continue; while(z_cnt||o_cnt||j_cnt) { if(z_cnt) {cout << "Z";z_cnt--;} if(o_cnt) {cout << "O";o_cnt--;} if(j_cnt) {cout << "J";j_cnt--;} } cout <<endl; } return 0; }  

     

    3.继续xxx定律:(使用map来做)

    http://acm.hdu.edu.cn/showproblem.php?pid=3784

    //http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1003&cid=328 //继续xxx定律 //Accepted 1003 78MS 404K G++ CS_BOY #include <iostream> #include <map> using namespace std; struct Node { int num; bool tag;//为0表示覆盖数,1表示关键数 }; int main() { int N;int key;int arr[1000]; while(scanf("%d",&N)) { map<int,bool> M; if(N==0) break; for(int i=0;i<N;i++) { cin >> key; arr[i] = key; M[key] = true;//初始化时置为真。等到寻找过程中不断地修改 } for(int i=0;i<N;i++) { key = arr[i]; if(M[key])//为真 { while(key!=1) { if(key&1==1)//odd number key=(3*key+1)>>1; else key = key >> 1; if(M.count(key)==0) continue;//M中没有这个元素 else if(M[key]) M[key]= false; else continue; //已经被置为false了 } } } int cnt=0; for(int i=N-1;i>=0;i--) { key = arr[i]; if(M[key]) cnt++; else continue; } for(int i=N-1;i>=0;i--) { key = arr[i]; if(M[key]) { printf("%d",key);cnt--; if(cnt!=0) printf(" "); } else continue; } printf("/n"); } return 0; }  

     

    4.寻找大富翁

     

    http://acm.hdu.edu.cn/showproblem.php?pid=3785

    //第四题:寻找大富翁;输入n(0<n<=100000),m(0<m<=10),n为小镇上的人数,m为需要找出的大富翁数。输入: //n, m 接下来一行输入小镇n个人的财富值,输出:前m个大富翁的财产数,n为0时结束 #include <iostream> #include <queue> using namespace std; int main() { int population; int tobefind; while(scanf("%d%d",&population,&tobefind)) { //printf("%d %d/n",population,tobefind); if(population==0 && tobefind ==0) break; priority_queue<int> Q; int wealth; for(int i=0;i<population;i++) { scanf("%d",&wealth); //cin >> wealth; Q.push(wealth); } for(int i=0;i<tobefind;i++) { wealth = Q.top(); Q.pop(); //cout << wealth; printf("%d",wealth); if(i<tobefind-1) //cout << " "; printf(" "); else //cout << endl; printf("/n"); } } return 0; }  

     

     

    5.找出直系亲属

    http://acm.hdu.edu.cn/showproblem.php?pid=3786

    采用并查集来做。

      //找出直系亲属 #include <iostream> using namespace std; int p[3000];//p[i] represents the child of i node int count(unsigned char a,unsigned char b) { if(a==b) return 0; if(p[b]!=-1) return count(a,p[b])+1; else return -65535; } void print1(int s) { if(s==1) cout<<"parent"<<endl; else if(s==2) cout<<"grandparent"<<endl; else if(s==3) cout<<"great-grandparent"<<endl; else { cout<<"great-"; print1(s-1); } } void print2(int s) { if(s==1) cout<<"child"<<endl; else if(s==2) cout<<"grandchild"<<endl; else if(s==3) cout<<"great-grandchild"<<endl; else { cout<<"great-"; print2(s-1); } } int main() { int n,m; while(cin>>n>>m&&(n||m)) { getchar(); memset(p,-1,sizeof(p)); if(n==0) while(m--) cout<<'-'<<endl; else { while(n--) { unsigned char c,p1,p2; c=getchar(); p1=getchar(); p2=getchar(); getchar(); if(p2!='-')p[p2]=c; if(p1!='-')p[p1]=c; } while(m--) { unsigned char c,pt; c=getchar(); pt=getchar(); getchar(); int l1=count(c,pt); int l2=count(pt,c); if(l1>0) print2(l1); else if(l2>0) print1(l2); else cout<<'-'<<endl; } } } return 0; }  

     

     


    最新回复(0)