zoukankan      html  css  js  c++  java
  • 算法笔记 上机训练实战指南 第3章 入门篇(1) --入门模拟 3.6字符串处理 学习笔记

    PAT B1006 换个格式输出整数 (15分)

    让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n<1000),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。

    输入格式:

    每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。

    输出格式:

    每个测试用例的输出占一行,用规定的格式输出 n。

    输入样例 1:

    234

    输出样例 1:

    BBSSS1234

    输入样例 2:

    23

    输出样例 2:

    SS123
    #include<cstdio>
    int main(){
        int n;
        scanf("%d",&n);
        int ans[3]={0};
        for(int i=0;i<3;i++){
            ans[i] = n%10;
            n = n/10;
        }
        for(int i=0;i<ans[2];i++)
            printf("B");
        for(int i=0;i<ans[1];i++)
            printf("S");
        for(int i=1;i<=ans[0];i++)
            printf("%d",i);
        return 0;
    }
    PAT B1021 个位数统计 (15分)

    给定一个 k 位整数 1 (0, ,, dk1​​>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3。

    输入格式:

    每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。

    输出格式:

    对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。

    输入样例:

    100311

    输出样例:

    0:2
    1:3
    3:1
    #include<cstdio>
    #include<cstring>
    int main(){
        char str[1010];
        scanf("%s",str);
        int count[15]={0};
        int len = strlen(str);
        for(int i=0;i<len;i++){
            count[str[i]-'0']++;
        }
        for(int i=0;i<=9;i++){
            if(count[i]>0){
                printf("%d:%d
    ",i,count[i]);
            }
        }
        return 0;
    }
    PAT B1031 查验身份证 (15分)

    一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

    首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

    Z:0 1 2 3 4 5 6 7 8 9 10
    M:1 0 X 9 8 7 6 5 4 3 2

    现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

    输入格式:

    输入第一行给出正整数N(≤)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

    输出格式:

    按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出All passed

    输入样例1:

    4
    320124198808240056
    12010X198901011234
    110108196711301866
    37070419881216001X

    输出样例1:

    12010X198901011234
    110108196711301866
    37070419881216001X

    输入样例2:

    2
    320124198808240056
    110108196711301862

    输出样例2:

    All passed
    #include<cstdio>
    #include<cstring>
    int main(){
        int n;
        char code[15]={'1','0','X','9','8','7','6','5','4','3','2','1'};
        int weight[20]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        scanf("%d",&n);
        bool flag = true;
        while(n--){
            char str[20];
            scanf("%s",str);
            int sum=0;
            int i;
            for(i=0;i<17;i++){
                if(str[i]>='0' && str[i]<='9'){
                    sum = sum + (str[i]-'0') * weight[i];
                }
                else{
                    break;
                }    
            }    
            if(i < 17){
                flag = false;
                printf("%s
    ",str);
            }
            else{
                sum = sum % 11;
                if(code[sum] != str[17]){
                    flag = false;
                    printf("%s
    ",str);
                }
            }
            
        }
        if(flag==true){
            printf("All passed
    ");
        }
        return 0;
    }
    PAT B1002 写出这个数 (20分)

    读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

    输入格式:

    每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 1。

    输出格式:

    在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

    输入样例:

    1234567890987654321123456789

    输出样例:

    yi san wu
    #include<cstdio>
    #include<cstring>
    int main(){
        char say[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
        char str[110];
        scanf("%s",str);
        int sum=0;
        int len = strlen(str);
        for(int i=0;i<len;i++){
            sum = sum + (str[i]-'0');
        }
        int ans[100];
        int num=0;
        do{
            ans[num++] = sum%10;
            sum /= 10;
        }while(sum!=0);
        for(int i=num-1;i>=0;i--){
            printf("%s",say[ans[i]]);
            if(i>0)
                printf(" ");
        }
        return 0;
    }
     
    PAT B1014 福尔摩斯的约会 (20分) A1061

    大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 23 点由数字 0 到 9、以及大写字母 A 到 N 表示);后面两字符串第 1 对相同的英文字母 s 出现在第 4 个位置(从 0 开始计数)上,代表第 4 分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。

    输入格式:

    输入在 4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。

    输出格式:

    在一行中输出约会的时间,格式为 DAY HH:MM,其中 DAY 是某星期的 3 字符缩写,即 MON 表示星期一,TUE 表示星期二,WED 表示星期三,THU 表示星期四,FRI 表示星期五,SAT 表示星期六,SUN 表示星期日。题目输入保证每个测试存在唯一解。

    输入样例:

    3485djDkxh4hhGE 
    2984akDfkkkkggEdsb 
    s&hgsfdk 
    d&Hyscvnm

    输出样例:

    THU 14:04
    #include<cstdio>
    #include<cstring>
    int main(){
        char week[7][5]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
        char str1[70],str2[70],str3[70],str4[70];
        scanf("%s",str1);
        scanf("%s",str2);
        scanf("%s",str3);
        scanf("%s",str4);
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        int len3 = strlen(str3);
        int len4 = strlen(str4);
        int i;
        for(i=0;i<len1 && i<len2;i++){
            if( (str1[i]>='A' && str1[i]<='G') && (str1[i] == str2[i])){
                printf("%s ",week[str1[i]-'A']);
                break;
            }
        }
        for(i++;i<len1 && i<len2;i++){
            if((str1[i]>='0' && str1[i]<='9') && (str1[i]==str2[i])){
                printf("%02d:",str1[i]-'0');
                break;
            }else if((str1[i]>='A' && str1[i]<='N') && (str1[i]==str2[i])){
                printf("%02d:",str1[i]-'A'+10);
                break;
            }
        }
        for(i=0;i<len3 && i<len4;i++){
            if(str3[i]==str4[i]){
                if( (str3[i]>='a' && str3[i]<='z') || (str3[i]>='A' && str3[i]<='Z')){
                    printf("%02d",i);
                    break;
                }
            
            }
        }
        return 0;
    } 
    PAT B1024 科学计数法 (20分) /A1073

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

    现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

    输入格式:

    每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

    输出格式:

    对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

    输入样例 1:

    +1.23400E-03

    输出样例 1:

    0.00123400

    输入样例 2:

    -1.2E+10

    输出样例 2:

    -12000000000
    #include<cstdio>
    #include<cstring>
    int main(){
        char str[10010];
        scanf("%s",str);
        int pos = 0;
        while(str[pos]!='E'){
            pos++;
        }
        int len = strlen(str);
        if(str[0]=='-'){
            printf("-");
        }
        int exp = 0;
        for(int i=pos+2;i<len;i++){
            exp = exp *10 + (str[i]-'0');
        }
        
        if(exp==0){
            for(int i=1;i<pos;i++){
                printf("%c",str[i]);
            }
        }
        
        if(str[pos+1] == '-'){
            printf("0.");
            for(int i=0;i<exp-1;i++){
                printf("0");
            }
            for(int i=1;i<pos;i++){
                if(str[i]=='.')
                    continue;
                else
                    printf("%c",str[i]);
            }
        }else{
            for(int i=1;i<pos;i++){
                if(str[i]=='.') 
                    continue;
                printf("%c",str[i]);
                if(i == exp+2 && (pos-3) != exp)
                    printf(".");
            }
            for(int i=0;i<exp - (pos-3);i++)
                printf("0");
        }
        return 0;
    }

    PAT A1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

    Input Specification:

    Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.

    Output Specification:

    For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

    Sample Input:

    -1000000 9

    Sample Output:

    -999,991
    #include<cstdio>
    int main(){
        int a,b;
        scanf("%d %d",&a,&b);
        int sum= a + b;
        if(sum<0){
            printf("-");
            sum = -sum;
        }
        int ans[20];
        int num = 0;
        if(sum == 0) 
            ans[num++] = 0;
        while(sum!=0){
            ans[num++] = sum%10;
            sum /= 10;
        }
        for(int i = num-1;i>=0;i--){
            printf("%d",ans[i]);
            if(i%3==0 && i>0)
                printf(",");
        }
        return 0;
    } 
    PAT A1005 Spell It Right (20分)

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

    Input Specification:

    Each input file contains one test case. Each case occupies one line which contains an N (≤).

    Output Specification:

    For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

    Sample Input:

    12345

    Sample Output:

    one five
    #include<cstdio>
    #include<cstring>
    char spell[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    int main(){
        char num[110];
        scanf("%s",num);
        int sum = 0;
        int len = strlen(num);
        for(int i=0;i<len;i++){
            sum = sum + (num[i]-'0');
        }
        int ans[110];
        int k=0;
        do{
            ans[k++] = sum%10;
            sum /= 10;
        }while(sum!=0);
        for(int i=k-1;i>=0;i--){
            printf("%s",spell[ans[i]]);
            if(i != 0)
                printf(" ");
        }
        return 0;
    }

    PAT A1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @0 (zero) by %l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

    Input Specification:

    Each input file contains one test case. Each case contains a positive integer N (≤), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

    Output Specification:

    For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

    Sample Input 1:

    3
    Team000002 Rlsp0dfa
    Team000003 perfectpwd
    Team000001 R1spOdfa

    Sample Output 1:

    2
    Team000002 RLsp%dfa
    Team000001 R@spodfa

    Sample Input 2:

    1
    team110 abcdefg332

    Sample Output 2:

    There is 1 account and no account is modified

    Sample Input 3:

    2
    team110 abcdefg222
    team220 abcdefg333

    Sample Output 3:

    There are 2 accounts and no account is modified
    #include<cstdio>
    #include<cstring>
    struct node{
        char name[20],password[20];
        bool ismodify;
    }user[1010];
    void modify(node &no,int &count){
        int len = strlen(no.password);
        for(int i =0;i<len;i++){
            if(no.password[i] == '1'){
                no.password[i] = '@';
                no.ismodify = true;
            }else if(no.password[i] == '0'){
                no.password[i] = '%';
                no.ismodify = true;
            }else if(no.password[i] == 'l'){
                no.password[i] = 'L';
                no.ismodify = true;
            }else if(no.password[i]== 'O'){
                no.password[i] = 'o';
                no.ismodify = true;
            }
        }
        if(no.ismodify==true){
            count++;
        }
    }
    int main(){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s %s",user[i].name,user[i].password);
            user[i].ismodify = false;
        }
        int count = 0;
        for(int i=0;i<n;i++){
            modify(user[i],count);
        }
        
        if(count == 0 && n==1){
            printf("There is 1 account and no account is modified
    ");
        }else if(count == 0){
            printf("There are %d accounts and no account is modified
    ",n);
        }else{
            printf("%d
    ",count);
            for(int i=0;i<n;i++){
                if(user[i].ismodify == true){
                    printf("%s %s
    ",user[i].name,user[i].password);
                }
            }
        }
        return 0;
    }
    PAT A1077 Kuchiguse (20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

    • Itai nyan~ (It hurts, nyan~)

    • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

    Now given a few lines spoken by the same character, can you find her Kuchiguse?

    Input Specification:

    Each input file contains one test case. For each case, the first line is an integer N (2). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

    Output Specification:

    For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.

    Sample Input 1:

    3
    Itai nyan~
    Ninjin wa iyadanyan~
    uhhh nyan~

    Sample Output 1:

    nyan~

    Sample Input 2:

    3
    Itai!
    Ninjinnwaiyada T_T
    T_T

    Sample Output 2:

    nai

    #include<cstdio>
    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    string str[200];
    int main(){
        int n;
        scanf("%d",&n);
        getchar();
        int minlen = 256;
        for(int i=0;i<n;i++){
            getline(cin,str[i]);
            if(str[i].size() < minlen){
                minlen = str[i].size();
            }
            reverse(str[i].begin(),str[i].end());
        }
        int ans = 0;
        for(int i=0;i<minlen;i++){
            char ch = str[0][i];
            bool same = true;
            for(int j=1;j<n;j++){
                if(ch != str[j][i]){
                    same = false;
                    break;
                }
            }
            if(same==true){
                ans++;
            }else{
                break;
            }
        }
        if(ans!=0){
            for(int i = ans - 1;i >= 0;i--){
                printf("%c",str[0][i]);
            }
        }else{
            printf("nai
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    andorid jar/库源码解析之Butterknife
    JavaScript DOM 鼠标拖拽
    JavaScript JSON 与 AJAX
    JavaScript DOM 事件模型
    JavaScript DOM 样式操作
    JavaScript DOM 常用尺寸
    JavaScript 日期与计时器
    JavaScript DOM 基础
    JavaScript 数组
    JavaScript 对象拷贝
  • 原文地址:https://www.cnblogs.com/coderying/p/12218383.html
Copyright © 2011-2022 走看看