• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Artimislyy
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU2564 词组缩写

2019-06-03

15:00:38

感觉有有种被坑了的感觉,这道题不难,就是一再的W,😭

 

else if (a[0] >= 'A' && a[0] <= 'Z')
              {
                   cout << a[0];
              }
样例可能第一个为空格
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int t;
 6     while (cin >> t)
 7     {
 8         getchar();
 9         while (t--)
10         {
11             string a;
12             getline(cin, a);
13             
14             int lenth = a.length();
15             if (a[0] >= 'a' && a[0] <= 'z')
16             {
17                 printf("%c", a[0] - 32);
18             }
19             else if (a[0] >= 'A' && a[0] <= 'Z')
20             {
21                 cout << a[0];
22             }
23 
24             for (int i = 1; i < lenth; i++)
25             {
26                 if (a[i - 1] == ' ' && a[i] >= 'a' && a[i] <= 'z')
27                 {
28                     printf("%c", a[i] - 32);
29                 }
30                 if (a[i - 1] == ' ' && a[i] >= 'A' && a[i] <= 'Z')
31                 {
32                     printf("%c", a[i]);
33                 }
34             }
35             cout << endl;
36         }
37     }
38     return 0;
39 }
可以运用另一种方法
 
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d", &t))
    {
        getchar();
        while(t--)
        {
            char a[100];
            gets(a);
            strupr(a);
            if(a[0] >= 'A' && a[0] <='Z')
            {
                cout << a[0];
            }
            for(int i = 1; a[i] != '\0'; i++)
            {
                if(a[i - 1] == ' ' && a[i] >= 'A' && a[i] <= 'Z')
                {
                    cout << a[i];
                }
            }
            cout << endl;
        }
    }
    return 0;
}

 

 
 
 
 
posted @ 2019-06-03 15:07  Artimislyy  阅读(143)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3