2016.07.11 完成 9 道题

来源:互联网 发布:网络监控系统原理图 编辑:程序博客网 时间:2024/05/24 06:32

UVA-537

题意:给你一个字符串,让你捕捉其中PIU的信息,通过已经捕获的两个求出第三个的值并输出。
解题思路:捕捉 “P=” , “I=”,”U=”,然后获取出到单位之前的数字和 m M k。然后计算第三个的值。

/*************************************************************************    > File Name: UVA-537.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 08时36分38秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;string s;int t;double P,U,I,z;double cut(int &i){    double k=0;    z=1.0;    while ('0' <= s[i]  &&  s[i] <= '9'){        k = k*10.0 +s[i] - '0';        i++;    }    if (s[i] == '.') {        i++;        while ('0' <= s[i]  &&  s[i] <= '9') {            z = z/10.0;            k = k + (s[i] - '0')*z;            i++;        }    }    if (s[i] == 'm' || s[i] == 'k' ||  s[i] == 'M'){        if (s[i] == 'k') k = k*1000.0;        if (s[i] == 'M') k = k * 1000000.0;        if (s[i] == 'm') k = k/1000.0;        i++;    }    return k;}int main () {    freopen("xx.in","r",stdin);    scanf("%d\n",&t);    for (int k = 1; k <= t; k++) {        printf("Problem #%d\n",k);        getline(cin,s);        P=U=I=0;        for (int i = 0; i < s.length(); i++) {            if (s[i] == 'U' && s[i+1] == '=') {                i=i+2;                U=cut(i);            }             if (s[i] == 'P' && s[i+1] == '=') {                i=i+2;                P=cut(i);            }             if (s[i] == 'I' && s[i+1] == '=') {                i=i+2;                I=cut(i);            }        }        if (P == 0) printf("P=%.2fW\n",U*I);        if (I == 0) printf("I=%.2fA\n",P/U);        if (U == 0) printf("U=%.2fV\n",P/I);        printf("\n");    }}

UVA-409

题意:给你n个关键词和m句话,求m句话中含关键词最多的话。存在多句时任意输出顺序(反正我是按读入顺序)。
解题思路 :复制一遍句子,全部转换成小写,然后暴力比较,记录每个句子匹配到多少个关键词。找到最大的数字,扫一遍输出。

/*************************************************************************    > File Name: UVA-409.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 09时10分06秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;struct node {    string ans,l,key[300];    int len,tag;} s[600];string key[300];int n,m;void cut(int i){    s[i].len = 1;    s[i].key[1]="";    s[i].tag = 0;    for (int x = 0; x < s[i].l.length(); x++)        if ('a' <= s[i].l[x] && s[i].l[x] <= 'z')            s[i].key[s[i].len] +=s[i].l[x];        else             if (s[i].key[s[i].len] != ""){                s[i].len++;                s[i].key[s[i].len]="";            }}int main () {    int num=0;    freopen("xx.in","r",stdin);    while (scanf("%d%d\n",&n,&m) != EOF ) {        num++;        for (int i = 1; i <= n; i++)            getline (cin, key[i]);          for (int i = 1; i <= m; i++){            getline (cin, s[i].ans);            s[i].l = s[i].ans;            for (int j = 0; j < s[i].l.length(); j++)                if ('A' <= s[i].l[j] && s[i].l[j] <= 'Z')                    s[i].l[j] = s[i].l[j] -'A' +'a';            cut(i);        }        for (int i = 1; i <= m; i++)            for (int k = 1; k <= s[i].len; k++)                for (int j = 1; j <= n; j++)                    if (key[j] == s[i].key[k]){                        s[i].tag++;                        break;                    }        int Max=0;        printf("Excuse Set #%d\n",num);        for (int i = 1; i <= m; i++)            if (s[i].tag > Max) Max=s[i].tag;        for (int i = 1; i <= m; i++)            if (s[i].tag == Max)                cout<<s[i].ans<<endl;        printf("\n");    }}

UVA-10878

题意:根据样例推测每一行字符串和对应字符之间的关系,然后通过输入的提示输出字符。
解题思路:细心点会发现,无视掉那个 ’ . ’ 把空格当0,o当1,对应的二进制转换成十进制后会发现其实就是ASCLL码。就简单了。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;string s;int main () {    freopen("xx.in","r",stdin);    while (getline(cin, s)){        if (s == "___________") continue;        int k=0;        for (int i = 0; i < s.length(); i++)            if (s[i] == ' ') k*=2;            else if (s[i] == 'o') k = k*2 +1;        printf("%c",k);    }}

UVA-10815

题意:给若干行字符串,求其中出现的所有单词,全部转换成小写,并按字典序从小到大排序。
解题思路:看捕捉单词的能力了,把每个单词捕捉出来,转换成小写,排序。

/*************************************************************************    > File Name: UVA-10815.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 10时36分30秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;string ans[60000],s;bool cmp(string a, string b){    return a<b;}int len=1;int main () {    freopen("xx.in","r",stdin);    ans[0]="";    ans[1]="";    while (getline (cin,s)){        if (s[s.length()-1] != '\n') s=s+'\n';        for (int i = 0; i < s.length(); i++){            if ('A' <= s[i] && s[i] <= 'Z') s[i]=s[i]-'A'+'a';            if ('a' <= s[i] && s[i] <= 'z') ans[len]+=s[i];              else if (ans[len] != "") {                  len++;                  ans[len]="";              }        }    }    if (ans[len] == "" ) len--;    sort(ans+1,ans+1+len,cmp);    for (int i = 1; i <= len; i++)        if (ans[i] != ans[i-1]) cout<<ans[i]<<endl;}

UVA-644

题意:判断单词会不会成为另一个单词的前缀部分。
解题思路:这题暴力枚举比较。

/*************************************************************************    > File Name: UVA-644.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 10时51分46秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;string ans[6000];int len;bool cmp(const string a,const string b){    return a.length() < b.length();}int main () {    freopen("xx.in","r",stdin);    int num = 0;    while (cin>>ans[1]){        len=1;        while (ans[len] != "9") {            len++;            cin>>ans[len];        }        len--;        bool Tag=true;        sort(ans+1,ans+1+len,cmp);        for (int i = 1; i <= len; i++)            for (int j = i+1; j <= len; j++){                int tag=1;                for (int l = 0; l < ans[i].length(); l++)                    if (ans[i][l] != ans[j][l] ) {                        tag=0;                        break;                    }                if (tag) {                    Tag=false;                    break;                }            }        num++;        printf("Set %d is ",num);        if (!Tag) printf("not ");        printf("immediately decodable\n");    }}

UVA-10115

题意:给定n对替换对,在给一个字符串。求经过这些替换之后字符串变成什么。
解题思路:暴力判断哪里可以替换,然后替换进去。要注意两点:1、当一个替换对无法替换后才会用下一对。2、但前替换对进行替换后,下一次搜索是从头开始搜索,不是从但前位置开始。

/*************************************************************************    > File Name: UVA-10115.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 11时13分20秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;int n;string key[50],change[50],s,ans;bool check(int x,int l){    int i=0;    while (i < key[x].length()  &&  l < s.length()){        if (key[x][i] != s[l+i]) return false;        i++;    }    if (i == key[x].length()) return true;    return false;}int main () {    freopen("xx.in","r",stdin);    while (true) {        scanf("%d\n",&n);        if (n == 0) break;        for (int i = 1; i <= n; i++) {            getline(cin, key[i]);            getline(cin, change[i]);        }        getline(cin,s);        int tag =1;        for (int i = 1; i <= n; i++) {            tag=1;            while (tag) {                tag=0;                ans="";                for (int l = 0; l < s.length(); l++)                    if (check(i,l) && !tag ){                        l+=key[i].length()-1;                        for (int k = 0; k < change[i].length(); k++)                            ans+=change[i][k];                        tag=1;                    }else ans+=s[l];                s= ans;            }        }        cout<<s<<endl;    }}

UVA-424

简单高精加问题。
要注意最后要补 ‘\n’ 。不然会错误。对,我就是这个问题卡了好久,以为是太久没写高精写错了,最后看别人都些 ‘\n’就补了个,就AC了。

/*************************************************************************    > File Name: UVA-424.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 13时06分31秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;string s;int ans[2000],set[2000],la,ls;int max (int a,int b){    return  a>b  ? a :b;}int main () {    freopen("xx.in","r",stdin);    memset(ans,0,sizeof(ans));    la=0;    while (cin>>s){        ls = s.length();        if (s[0] == '0') break;        memset(set,0,sizeof(set));        for (int i = ls-1, j = 1; i >= 0; i--, j++)            set[j]= s[i]-'0';        while (set[ls] == 0) ls--;        la= max(la,ls);        int k=0;        for (int i = 1; i <= la; i++){            k = k +ans[i] +set[i];            ans[i]=k % 10;            k=k/10;        }        if (k) {            la++;            ans[la]=k;        }        while (ans[la] == 0) la--;    }    for (int i = la; i >= 1; i--)        printf("%d",ans[i]);    printf("\n");}

UVA-10106

简单高精乘法。

/*************************************************************************    > File Name: UVA-10106.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 14时03分59秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int x[600],y[600],lx,ly,la,ans[600];string s1,s2;int main () {    freopen("xx.in","r",stdin);    while (cin>>s1>>s2){        memset(x,0,sizeof(x));        memset(y,0,sizeof(y));        memset(ans,0,sizeof(ans));        lx = s1.length();        for (int i = lx-1, j = 1; i >= 0; i--, j++)            x[j] = s1[i] - '0';        ly = s2.length();        for (int i = ly-1, j = 1; i >= 0; i--, j++)            y[j] = s2[i] - '0';        for (int i = 1; i <= lx; i++)            for (int j = 1; j <= ly; j++)                ans[i+j-1] += x[i] * y[j];        la = lx + ly -1;        int k=0;        for (int i = 1; i <= la ; i++){            k = k + ans[i];            ans[i] = k % 10;            k/=10;        }        while (k){            la++;            ans[la]=k%10;            k/=10;        }        while (ans[la] == 0 && la > 1) la--;        for (int i = la; i >= 1; i--)            printf("%d",ans[i]);        printf("\n");    }}

UVA-465

判断第一个数字、第二个数字和结果会不会超高interger的范围。
把字符串转换成double就能过了。

/*************************************************************************    > File Name: UVA-465.cpp    > Author: Narsh    >     > Created Time: 2016年07月11日 星期一 15时32分17秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <string>using namespace std;const int INF = 2147483647;string s1,s2,s3;int main () {    freopen("xx.in","r",stdin);    double a,b;    while (cin>>s1>>s2>>s3) {        a=b=0;        for (int i = 0; i < s1.length(); i++)            a = a*10.0 +s1[i] -'0';        for (int i = 0; i < s3.length(); i++)            b = b*10.0 +s3[i] -'0';        cout<<s1<<' '<<s2<<' '<<s3<<endl;        if (a > INF) printf("first number too big\n");        if (b > INF) printf("second number too big\n");        if (s2[0] == '+') {            if (a+b > INF) printf("result too big\n");        }else if (a*b >INF) printf("result too big\n");    }}
0 0