STL快速解题
?http://ac.jobdu.com/problem.php?pid=1402??? 特殊的數(shù)
#include<iostream> #include<bitset> #include<cstdio> using namespace std;void read(int &data) //快速讀取數(shù)據(jù) {char ch = getchar();while (ch < '0' || ch > '9')ch = getchar();data = 0;do{data = data*10 + ch-'0';ch = getchar(); }while (ch >= '0' && ch <= '9'); }int main(void) {int n,i,m,max,num,min;bitset<1000001> bits,b;while(scanf("%d",&n)!=EOF){bits.reset(); //清空b.reset();max=-1;min=1000001;for(i=0;i<n;i++){//scanf("%d",&m);read(m);if(m>max)max=m;if(m<min)min=m;if(bits[m]==0) //這個(gè)數(shù)字第一次出現(xiàn)bits[m]=1;else if(bits[m]==1 && b[m]==0) //這個(gè)數(shù)字第二次出現(xiàn)b[m]=1;//超過(guò)兩次以上出現(xiàn)的數(shù)字不用在進(jìn)行賦值操作了}num=0;for(i=min;i<=max;i++){if(bits[i]==1 && !b[i]) //只出現(xiàn)了一次num++;}//num=bits.count()-b.count();printf("%d\n",num);if(num){m=0;for(i=min;i<=max;i++){if(b[i]==0 && bits[i]==1){if(m==0){printf("%d",i);m++;}else{printf(" %d",i);m++;}if(m==num){printf("\n");break;}}}}}return 0; }http://ac.jobdu.com/problem.php?pid=1403??? 神奇的開(kāi)關(guān)??http://poj.org/problem?id=1176
#include<iostream> #include<queue> #include<cstdio> #include<string> #include<set> using namespace std; #include<memory.h>struct node {string name; //定義一個(gè)優(yōu)先隊(duì)列friend bool operator<(node a,node b){return a.name > b.name; //小到大(字典序)} }w;string str; priority_queue<node > q;set<string>myset; int ON[101],OFF[101],n,i,j,k,p,r,t; bool pos[101];inline void solve() {int h;if(i)memset(pos,false,sizeof(pos));else //開(kāi)memset(pos,true,sizeof(pos));if(j) //奇數(shù)的燈改變狀態(tài){for(h=1;h<=n;h+=2)pos[h] ^=1; //跟 pos[h] =1- pos[h]; 是等效的}if(k) //偶數(shù)的燈改變狀態(tài){for(h=1;h<=n;h++){if((h&1)==0) //由于==的優(yōu)先級(jí)比與運(yùn)算的優(yōu)先級(jí)別要高,所以與運(yùn)算需要加括號(hào),導(dǎo)致了多次WApos[h] ^=1;}}if(p) //編號(hào)為(3 * K + 1)(K>=0)的燈改變狀態(tài){for(h=1;h<=n;h+=3){if(h%3==1)pos[h] ^=1;}}for(h=0;h<r;h++){if(!pos[ON[h]])return ;}for(h=0;h<t;h++){if(pos[OFF[h]])return ;}str="";for(h=1;h<=n;h++){if(pos[h])str+='1';elsestr+='0';}myset.insert(str); }int main(void) {int c,m;set<string>::iterator iter;while(scanf("%d",&n)!=EOF){myset.clear();scanf("%d",&c);r=t=0;while(1){scanf("%d",&m);if(m==-1)break;ON[r++]=m;}while(1){scanf("%d",&m);if(m==-1)break;OFF[t++]=m;}for(i=0;i<=1;i++) //枚舉狀態(tài){for(j=0;j<=1;j++){for(k=0;k<=1;k++){for(p=0;p<=1;p++){if(i+j+k+p>c)break;if ((i+j+k+p)%2!=c%2)continue;solve();}}}}if(myset.empty())printf("IMPOSSIBLE\n");else{for(iter=myset.begin();iter!=myset.end();iter++)cout<<*iter<<endl;}}return 0; }
?
總結(jié)
- 上一篇: 2010年北京大学计算机研究生机试真题
- 下一篇: 2006年清华大学计算机研究生机试真题