欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

华为2016研发工程师编程题2/3

程序员文章站 2024-03-12 15:20:20
...

[编程题]字符集合

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 32M,其他语言64M

输入一个字符串,求出该字符串包含的字符集合
 

输入描述:

每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。

 

输出描述:

每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出。

 

输入例子1:

abcqweracb

 

输出例子1:

abcqwer
#include <stdio.h>
int main()
{
    char str[101];
    while (scanf("%s", &str) != EOF)
    {
        int a[256] = { 0 };
        for (int i = 0; str[i] != '\0'; i++)
        {      
            a[str[i]] ++;
            if (a[str[i]] == 1)
                printf("%c", str[i]);
        }
        printf("\n");
    }
    return 0;
}

起初我的想法是按字符读入,导致无法区分两个输入的字符串,无法满足多次输入字符串的要求:

#include<stdio.h>

int main(){
    int a[52] = {0};
    char t;
    while(~scanf("%c", &t)){
        if(t <= 90){
            if(a[t-65] == 0){
                printf("%c", t);
                a[t-65] = 1;
            }
        }
        else{
            if(a[t-97+26] == 0){
                printf("%c", t);
                a[t-97+26] = 1;
            }
        }
    }
    printf("\n");
    return 0;
}

华为2016研发工程师编程题2/3