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循环。

  • 相关阅读:
    后台管理、编辑器上传图片、修改用户头像、bbs小总结
    侧边栏制作成inclusion_tag、文章的点赞点踩、文章的评论
    登陆功能、bbs首页搭建、admin后台管理、首页文章展示、用户头像展示、图片防盗链、个人站点页面搭建、侧边栏展示功能、侧边栏筛选功能、将侧边栏制作成inclusion_tag
    表创建及同步、注册功能、登陆功能、搭建bbs首页
    毕设进度7
    毕设进度6
    毕设进度5
    毕设进度4
    毕设进度3
    学习进度2
  • 原文地址:https://www.cnblogs.com/xiaofengqaq/p/10685409.html
Copyright © 2011-2022 走看看