HDU 2564 词组缩写

词组缩写

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14681    Accepted Submission(s): 4763

Problem Description
定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写。 比如,C语言里常用的EOF就是end of file的缩写。
 

 

Input
输入的第一行是一个整数T,表示一共有T组测试数据; 接下来有T行,每组测试数据占一行,每行有一个词组,每个词组由一个或多个单词组成;每组的单词个数不超过10个,每个单词有一个或多个大写或小写字母组成; 单词长度不超过10,由一个或多个空格分隔这些单词。
 

 

Output
请为每组测试数据输出规定的缩写,每组输出占一行。
 

 

Sample Input
1
end of file
 

 

Sample Output
EOF
 

 

Author
lemon
 

 

Source
 

 

Recommend
yifenfei
 
 
注意这道题目要考虑特殊情况  如果中间有几个空格怎么办 以及最开始为空格的情况
这份代码还有一点问题   就是第一次输入前面有空格的情况时,什么都不会输出
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 1010
#define INF 99999999
char str[1010];
struct node
{
    char name[15];
}a[105];
int main()
{
    int t;
    while(~scanf("%d%*c",&t))
    {
        gets(str);
        int flag = 0,k = 0;
        for(int i=0;i<strlen(str);i++)
        {
            if(str[i]==' '&&str[i+1]!=' ')
            {
                int j,p;
                for(j=flag,p=0;j<i;j++,p++)
                    a[k].name[p] = str[j];
                a[k].name[p] = '\0';
                k++;
                flag = i+1;
            }
            if(i==strlen(str)-1&&str[i]!=' ')
            {
                int j,p;
                for(j=flag,p=0;j<=i;j++,p++)
                    a[k].name[p] = str[j];
                a[k].name[p] = '\0';
                k++;
            }
        }
        char sr[105];
        int q = 0;
        for(int i=0;i<k;i++)
        {
            if(a[i].name[0]>='A'&&a[i].name[0]<='Z')
               sr[q++] = a[i].name[0];
            else sr[q++] = a[i].name[0] + ('A'-'a');
        }
        sr[q] = '\0';
        printf("%s\n",sr);
    }
    return 0;
}

这是学长的一份代码,用动态数组的

#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <string.h>
using namespace std ;

int n , m ;
char str[1000] ;

int main( )
{
    int c ;
    scanf( "%d" , &c ) ;
    getchar( ) ;
    while( c-- )
    {
        gets(str) ;
        int len = strlen(str) ;
        string tmp = "" ;
        vector<string> words ;
        for( int i = 0 ; i < len ; i++ )
        {
            if( str[i] == ' ' )
            {
                if( tmp != "" )
                {
                    words.push_back(tmp) ;
                    tmp = "" ;
                }
            }
            else
            {
                tmp += str[i] ;
            }
        }
        if( tmp != "" ) words.push_back(tmp) ;
        string res = "" ;
        for( int i = 0 ; i < words.size() ; i++ )
        {
            res += toupper( words[i][0] ) ;
        }
        printf( "%s\n" , res.c_str() ) ;
    }
    return 0 ;
}

 

posted on 2017-03-22 19:03  九月旧约  阅读(314)  评论(0编辑  收藏  举报

导航