2 条题解
-
1
#include<bits/stdc++.h> using namespace std; int main(){ int ans1=0,ans2=0x3f3f3f,cnt; string s,s1,s2,s0; getline(cin,s); for(int i=0;i<s.length();i++){ if(s[i]==' '||s[i]==','||s[i]=='.'){ if(cnt>ans1){ s1=s0; ans1=cnt; } if(cnt<ans2){ s2=s0; ans2=cnt; } cnt=0; s0.clear(); } else{ s0+=s[i]; cnt+=1; } } cout<<s1<<endl<<s2<<endl; return 0; } -
-1
#include #include #include using namespace std;
int main() { string line; getline(cin, line);
string longest, shortest; int maxLen = -1, minLen = 1000; // 初始化 int i = 0, n = line.length(); while (i < n) { // 跳过非字母字符 while (i < n && !isalpha(line[i])) { i++; } if (i >= n) break; // 提取单词 int start = i; while (i < n && isalpha(line[i])) { i++; } string word = line.substr(start, i - start); int len = word.length(); // 更新最长单词(第一个遇到的最长) if (len > maxLen) { maxLen = len; longest = word; } // 更新最短单词(第一个遇到的最短) if (len < minLen) { minLen = len; shortest = word; } } cout << longest << endl; cout << shortest << endl; return 0;}
- 1
信息
- ID
- 6978
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 3
- 已通过
- 2
- 上传者