XXX定律
- #include "iostream"
- using namespace std;
- int main()
- {
- int n;
- while (cin>>n&&n!=0)
- {
- int cnt=0;
- while(n!=1)
- {
- n=n%2?(n*3+1)>>1:n>>1;
- cnt++;
- }
- printf("%d\n",cnt);
- }
- }
ZOJ
- #include "iostream"
- #include "string"
- using namespace std;
- int main()
- {
- string str;
- while(cin>>str&&str!="E")
- {
- int i;
- int count[26];
- memset(count,0,sizeof(int)*26);
- for (i=0;i<str.length();i++)
- count[str[i]-'A']++;
- int z='Z'-'A',o='O'-'A',j='J'-'A';
- while (count[z]>0||count[o]>0||count[j]>0)
- {
- if(count[z]-->0)
- printf("Z");
- if(count[o]-->0)
- printf("O");
- if(count[j]-->0)
- printf("J");
- }
- printf("\n");
- }
- }
继续xxx定律
- #include "iostream"
- using namespace std;
- int a[550];
- bool num[250000];//(x*3+1)/2======num means cover
- int main()
- {
- int n,i,j;
- while (scanf("%d",&n)!=EOF&&n!=0)
- {
- memset(num,false,sizeof(bool)*1010);
- for (i=0;i<n;i++)
- {
- scanf("%d",&a[i]);
- int temp=a[i];
- while(!num[a[i]]&&temp!=1)
- {
- temp=temp%2?(temp*3+1)>>1:temp>>1;
- num[temp]=true;
- }
- }
- i=n-1;
- while(num[a[i]])
- i--;
- printf("%d",a[i]);
- //num[a[i]]=true; %>_<%我还以为出现的关键数不该再出现了,没想到不用特判
- for (i=i-1;i>=0;i--)
- {
- if (!num[a[i]])
- printf(" %d",a[i]);
- //num[a[i]]=true;
- }
- printf("\n");
- }
- }
找大富翁
- /************************************************************************/
- /* bubble-437ms */
- /************************************************************************//
- #include "iostream"
- using namespace std;
- int a[100100];
- void swap(int i,int j)
- {
- a[i]=a[i]^a[j];
- a[j]=a[i]^a[j];
- a[i]=a[i]^a[j];
- }
- int main()
- {
- int n,m,i,j;
- while (scanf("%d%d",&n,&m)!=EOF&&!(n==0&&m==0))
- {
- if(n<m)
- m=n;
- for (i=0;i<n;i++)
- scanf("%d",&a[i]);
- for (i=1;i<=m;i++)
- {
- for(j=0;j<n-1;j++)
- if (a[j]>a[j+1])
- swap(j,j+1);
- }
- for(i=1;i<m;i++)
- printf("%d ",a[n-i]);
- printf("%d\n",a[n-m]);
- }
- }
- /************************************************************************/
- /* sort-468ms
- #include "iostream"
- #include "algorithm"
- using namespace std;
- int a[100100];
- void swap(int i,int j)
- {
- a[i]=a[i]^a[j];
- a[j]=a[i]^a[j];
- a[i]=a[i]^a[j];
- }
- int main()
- {
- int n,m,i,j;
- while (scanf("%d%d",&n,&m)!=EOF&&!(n==0&&m==0))
- {
- if(n<m)
- m=n;
- for (i=0;i<n;i++)
- scanf("%d",&a[i]);
- sort(a,a+n);
- for(i=1;i<m;i++)
- printf("%d ",a[n-i]);
- printf("%d\n",a[n-m]);
- }
- } */
- /************************************************************************//
- #include "iostream"
- #include "string"
- #include "algorithm"
- using namespace std;
- #define INF 1000000
- #define min(a,b) a<b?a:b
- char tree['Z'+10][2];
- int Dfs(char a,char b,int level)
- {
- //分别从a和b往下找
- if(a=='-'||tree[a][0]=='-'&&tree[a][1]=='-')
- return INF;
- if(tree[a][0]==b||tree[a][1]==b)
- return level+1;
- int left=Dfs(tree[a][0],b,level+1);
- int right=Dfs(tree[a][1],b,level+1);
- return min(left,right);
- }
- int main()
- {
- string str;
- int n,m,i,j;
- while (scanf("%d%d",&n,&m)!=EOF&&!(n==0&&m==0))
- {
- memset(tree,'-',sizeof(char)*2*('Z'+10));
- for(i=0;i<n;i++)
- {
- cin>>str;
- tree[str[0]][0]=str[1];
- tree[str[0]][1]=str[2];
- }
- for(i=0;i<m;i++)
- {
- cin>>str;
- int level1=Dfs(str[0],str[1],0);
- int level2=Dfs(str[1],str[0],0);
- int level=level1>=INF?-level2:level1;
- if(abs(level)>=INF)
- {
- printf("-\n");
- continue;
- }
- while (level++<-2)
- printf("great-");
- level--;
- if(level==-1)
- printf("parent\n");
- else if(level==-2)
- printf("grandparent\n");
- while (level-->2)
- printf("great-");
- level++;
- if (level==2)
- printf("grandchild\n");
- else if(level==1)
- printf("child\n");
- }
- }
- }