zoukankan      html  css  js  c++  java
  • #C++初学记录(ACM试题1)

    **A - Diverse Strings **
    A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.

    Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).

    You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
    Input
    The first line contains integer n (1≤n≤100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.

    Output
    Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
    Example
    Input
    8
    fced
    xyz
    r
    dabcef
    az
    aa
    bad
    babc
    Output
    Yes
    Yes
    Yes
    Yes
    No
    No
    No
    No
    正确代码

    #include<cstdio>
    #include<cstring>
    int main(){
        char a[105];
        int t;
        scanf("%d", &t);
        while(t--){
            memset(a, 0, sizeof(a));
            scanf("%s", a);
            int len = strlen(a), flag = 0;
            for(int i = 0; i < len; i++){
                for(int j = i+1; j < len; j++){
                    if(a[i] > a[j]){
                        char temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            for(int i = 1; i < len; i++){
                if(a[i] - a[i-1] != 1){
                    printf("No
    ");
                    flag = 1;
                    break;
                }
            }
            if(flag == 1){
                continue;
            }else{
                printf("Yes
    ");
            }
        }
        return 0;
    }
    
    

    题目理解
    该题较为简单,该题的正确做法是对输入的字母进行判断,若有相近的字母则输出YES,若没有则输出NO,若输入的是单个字符则也输出YES。
    相关知识点
    %s是进行字符串的整体输入,即

    int c[200];
    scanf("%s",%c);
    

    是将整个字符串拆分成单个字符储存进数组a,即输入abcde则a[1]=b。以及对单个字符进行判断,判断只需要在判断NO和YES是加一点小聪明.

            for(int i = 1; i < len; i++){
                if(a[i] - a[i-1] != 1){
                    printf("No
    ");
                    flag = 1;
                    break;
                }
            }
            if(flag == 1){
                continue;
            }else{
                printf("Yes
    ");
    

    在这个if条件判断中,因为单个字符的长度len=1,所以在进行No的判断中是不符合的,因为No的判断条件是i=1;i<len;因此直接进行else循环。

  • 相关阅读:
    LSB最低有效位和MSB最高有效位
    STM32(十二)通过I2C总线向EEPROM总线中读写数据的过程
    EEPROM和Flash的区别
    Prometheus监控系统的从无到有
    shell 提取第一个指定参数前面的内容
    myBase7试用到期 破解步骤
    docker入门(六)端口映射于容器关联
    docker入门(五)docker数据管理
    针对ansible的一次调研
    docker入门(四)访问docker仓库
  • 原文地址:https://www.cnblogs.com/xiaofengqaq/p/10685409.html
Copyright © 2011-2022 走看看