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; }