20160321 UVa RUJIA 算法竞赛入门经典:镜像回文,生成元,环状序列

401 - Palindromes

#include<cstdio>
#include<cstring>
char s[105],s1[105],s2[105],t;
char alp[]="A000300HIL0JM0O0002TUVWXY5",num[]="1SE0Z0080";
int main()
{
    while(scanf("%s",s)!=-1){
        int a=0,b=0;
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        int i=0,j=strlen(s)-1;
        strcpy(s1,s);
        while(i<=j){
            t=s1[i];s1[i]=s1[j];s1[j]=t;
            ++i;--j;
        }
        if(strcmp(s,s1)==0)a=1;//printf("s1=%s\n",s1);
        for(int i=0;s1[i];i++){
            if(s1[i]>'0'&&s1[i]<='9')s2[i]=num[s1[i]-'1'];
            else s2[i]=alp[s1[i]-'A'];
        }
        if(strcmp(s,s2)==0)b=1;//printf("s2=%s\n",s2);
        if(a+b==0)printf("%s -- is not a palindrome.\n\n",s);
        else if(a&&b==0)printf("%s -- is a regular palindrome.\n\n",s);
        else if(a==0&&b)printf("%s -- is a mirrored string.\n\n",s);
        else printf("%s -- is a mirrored palindrome.\n\n",s);
    }
	return 0;
}


1583 - Digit Generator

#include<cstdio>
#include<cstring>
int ans[100001];
int main()
{
    memset(ans,0x7f,sizeof(ans));
    int t,n;
    for(int i=1;i<=100000;i++){
        int a=i,s=0;
        while(a){s+=a%10;a/=10;}
        ans[s+i]=(ans[s+i]<i?ans[s+i]:i);
    }
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        if(ans[n]==ans[0])printf("0\n");
        else printf("%d\n",ans[n]);
    }
	return 0;
}


1584 - Circular Sequence

#include<cstdio>
#include<cstring>
char s[210],ans[105],t[105];
int main()
{
    int len,T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        len=strlen(s);
        for(int i=0;i<len;i++)s[i+len]=s[i];
        strncpy(ans,s,len);ans[len]=0;
        for(int i=1;i<len;i++){
            strncpy(t,s+i,len);
            t[len]=0;
            if(strcmp(t,ans)<0)strcpy(ans,t);
        }
        puts(ans);
    }
	return 0;
}

刷些水题完成指标


你可能感兴趣的:(算法竞赛入门经典)