2016.07.10 完成 6 道题

来源:互联网 发布:苏联解体对中国知乎 编辑:程序博客网 时间:2024/05/14 04:42

UVA-755

题意:题目给定 n 串字符串,包括0-9,A-Z 和 ‘-’ ,求其对应的号码,并按字典序从小到大排序,输出出现次数大于等于2的号码,格式为xxx-xxxx。如果没有结果 No duplicates.
解题思路:7位数不是很大,可以把字符串对应的号码求出来转换成数字,进行从小到大排序。通过 /10000和%10000来获得前三位和后四位。记得空的位置记得补 0 。

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int ans[100100],n;int f(char c) {    if ('0' <= c  &&  c <= '9') return c-'0';    if ('A' <= c  &&  c <= 'C') return 2;    if ('D' <= c  &&  c <= 'F') return 3;    if ('G' <= c  &&  c <= 'I') return 4;    if ('J' <= c  &&  c <= 'L') return 5;    if ('M' <= c  &&  c <= 'O') return 6;    if ('P' <= c  &&  c <= 'S') return 7;    if ('T' <= c  &&  c <= 'V') return 8;    if ('W' <= c  &&  c <= 'Y') return 9;    //if ('A' <= c  &&  c <= 'C') return 2;}bool cmp (int x,int y){    return x < y ;}int main () {    freopen("xx.in","r",stdin);    int t;    scanf("%d",&t);    bool flag;    while (t--){        scanf("%d",&n);        string s;        flag = false;        memset(ans,0,sizeof(ans));        for (int i = 1; i <= n; i++){            cin>>s;            for (int j = 0; j < s.length(); j++)                if (s[j] != '-') ans[i] = ans[i]*10+f(s[j]);        }        sort(ans+1,ans+1+n,cmp);        int tag = 1;        for (int i = 1; i <= n; i++)            if (ans[i] == ans[i+1]) tag++;                else {                  if (tag>1) {                      printf("%03d-%04d %d\n",ans[i]/10000,ans[i]% 10000,tag);                    flag =true;                  }                  tag=1;                }        if ( !flag ) printf("No duplicates.\n");        if (t) printf("\n");    }}

UVA-10785

题意:元音放奇数位,辅音放偶数位。每一个元音最多用21次,每个辅音最多用5次。每个字母有一个值,给定一个长度n,求用n个字母构造一个满足要求且值最小的字符串,字典序尽量小。
解题思路:元音和辅音取法相同,要值最小,那么就把值小的先用了。然后分别进行排序,奇数位上放元音的序列上的,偶数位放辅音的序列上的。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;char a[5]={'A','U','E','O','I'};char b[21]={'J','S','B','K','T','C','L','D','M','V','N','W','F','X','G','P','Y','H','Q','Z','R'};int n,t;char s1[300],s2[300];bool cmp(const char x,const char y){    return x<y;}int main () {    freopen("xx.in","r",stdin);    scanf("%d",&t);    for (int l = 1; l <= t; l++) {        printf("Case %d: ",l);        scanf("%d",&n);        for (int i = 0; i < (n-1)/2 +1; i++ )            s1[i] = a[i/21];        for (int i = 0; i <n- (n-1)/2 -1; i++)            s2[i] = b[i/5];        sort(s1,s1+(n-1)/2+1,cmp);        sort(s2,s2+n-(n-1)/2 -1, cmp);        int x=0,y=0;        for (int i = 1; i <= n; i++)            if (i % 2 == 1 ){                printf("%c",s1[x]);                x++;            }else {                printf("%c",s2[y]);                y++;            }        printf("\n");    }}

UVA-1586

题意:告诉我们 CHON 的质量,然后求给的化学式的质量。
解体思路:读到一个非数字字符就在总质量上加上它的质量,读到数字字符就把后面的数字字符都取出来转换成数字 k ,并拿它前面那个字符的质量 × (k -1) 。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;double a[150],sum;int n;string s;int main () {    freopen("xx.in","r",stdin);    scanf("%d",&n);    a['C']=12.01;    a['H']=1.008;    a['O']=16.00;    a['N']=14.01;    while (n--) {        cin>>s;        double cur=0;        sum=0;        for (int i = 0; i < s.length(); i++)            if ( '0' <= s[i]  &&  s[i] <= '9'){                double k = s[i]-'0';                while ('0' <= s[i+1] && s[i+1] <= '9'){                    i++;                    k=k*10+s[i]-'0';                }                k--;                sum += cur * k;                cur = 0.0;            }else {                cur = a[s[i]];                sum += cur;            }        printf("%.3f\n",sum);    }}

UVA-401

题意:判断给定字符串是回文字符串、镜面字符串、回文镜面字符串、非回文字符串中的哪一种。
解题思路:镜面字符串是指每个字符串都进行对称之后,和原串是对称的。只要两个标记a1,a2。a1判断是不是回文,a2判断是不是镜面。各扫一遍得到a1,a2。就能得到结果了。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;string s;char Reverse(char x) {    if (x == 'A'  || x == 'M' || x == 'Y' || x == 'O' || x == 'T'  || x == 'H' || x == 'V' || x == 'W' || x == 'X'  || x == 'U' || x == 'I' || x == '1' || x == '8') return x;    if (x == 'E') return '3';    if (x == 'J') return 'L';    if (x == 'L') return 'J';    if (x == 'S') return '2';    if (x == 'Z') return '5';    if (x == '2') return 'S';    if (x == '3') return 'E';    if (x == '5') return 'Z';    return '#';}int main () {    freopen ("xx.in","r",stdin);    while (cin >> s) {        bool tag1=true,tag2=true;        for (int i = 0; i < s.length(); i++)             if (s[i] != s[s.length()-i-1]) {                tag1=false;                break;            }        for (int i = 0; i < s.length(); i++)            if (Reverse(s[i]) != s[s.length()-i-1]){                tag2=false;                break;            }        if (tag1  &&  tag2) cout<<s<<" -- is a mirrored palindrome."<<endl;          else if (tag2) cout<<s<<" -- is a mirrored string."<<endl;            else if (tag1) cout<<s<<" -- is a regular palindrome."<<endl;              else cout<<s<<" -- is not a palindrome."<<endl;        cout<<endl;    }}

UVA-10010

题意:给一个 n*m 的字符矩阵。在给k个字符串,求字符串以矩阵的那个位置开始可以在矩阵中查到整个字符串。
解题思路:大小写问题可以统一置换成大写或小写方便比较。题目的查询是沿着一个方向一直查下去,不是dfs。这题的方向是 8 个不是常遇的 4 个。

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;char map[100][100];const int c[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};int n,m,t,l;string s;bool check(int x, int y){    for (int i = 0; i < 8; i++) {        int k;        for (k = 0; k < s.length(); k++)            if (s[k] != map[x + c[i][0]*k][y + c[i][1]*k]) break;        if (k == s.length()) return true;    }    return false;}int main () {    freopen("xx.in","r",stdin);    scanf("%d",&t);    while (t--) {        memset(map,0,sizeof(map));        scanf("%d%d\n",&n,&m);        for (int i = 1; i <= n; i++)            for (int j = 1; j <= m+1; j++) {                scanf("%c",&map[i][j]);                if ('A' <= map[i][j]  &&  map[i][j] <= 'Z')                    map[i][j]= map[i][j] -'A'+'a';            }        scanf("%d\n",&l);        while (l--) {            cin>>s;            for (int i = 0; i < s.length(); i++)                if ('A' <= s[i]  &&  s[i] <= 'Z')                    s[i]=s[i]-'A'+'a';            for (int i = 1; i <= n; i++)                for (int j = 1; j <= m; j++)                    if ( map[i][j] == s[0]  &&  check(i,j) ){                        printf("%d %d\n",i,j);                        i= n+1;                        break;                    }        }        if (t) printf("\n");    }}

UVA-10361

题意:每次给2行字符,第一行去要去 < > 输出结果( 例如 s1< s2 > s3 < s4 > s5 去掉<> 输出s1s2s3s4s5),第二行是把 ‘…’替换成 s4s3s2s5。
解题思路:找到< 把它到 第一个 > 之间的东西扣出来存在 s[1] 里。第一个 > 到第二个 < 之间的东西存在 s[2]里 ,第二个 < 到第二个 > 之间的东西存在 s[3] ,第二个 > 到行末的东西存到 s[4]。然后根据题目输出。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;int n,len;string x,s[9];char c;int main () {    freopen("xx.in","r",stdin);    scanf("%d\n",&n);    for (int l = 1; l <= n; l++){        len = 0;        getline(cin,x);    //  cout<<x[ x.length()-1]<<endl;        if (x[ x.length()-1] !='\n') x+='\n';        for (int i = 0; i < x.length(); i++)             if (x[i] != '<' ) printf("%c",x[i]);            else  {                i++;                len++;                s[len]="";                while (x[i] != '>') {                    s[len]+=x[i];                    printf("%c",x[i]);                    i++;                }                i++;                len++;                s[len]="";                while (x[i] != '\n' && x[i]!= '<' ){                    s[len]+=x[i];                    printf("%c",x[i]);                    i++;                }                if (x[i] != '<' ){                    printf("%c",x[i]);                    s[len]+=x[i];                } else i--;            }        //cout<<endl;        getline(cin,x);        for (int i = 0; i < x.length(); i++)            if (x[i] != '.') printf("%c",x[i]);            else {                cout<<s[3]<<s[2]<<s[1]<<s[4];                break;            }    }}
0 0