输入包括多行字符串,字符串的长度len(1<=len<=1000)。
按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
hello she sorry he
he hello sorry
#include <string> #include <vector> #include <iostream> using namespace std; int main(){ string s; vector<string> min_group,max_group; while(getline(cin,s)){ if(min_group.empty()&&max_group.empty()){ min_group.push_back(s); max_group.push_back(s); } else{ if(s.size()<=min_group[0].size()){ if(s.size()<min_group[0].size()) min_group.clear(); min_group.push_back(s); } if(s.size()>=max_group[0].size()){ if(s.size()>max_group[0].size()) max_group.clear(); max_group.push_back(s); } } } for(const auto&x:min_group) cout<<x<<endl; for(const auto&x:max_group) cout<<x<<endl; return 0; }字符串有空格是真的坑。。
#include <bits/stdc++.h> using namespace std; bool cmp (string a, string b){ return a.size() < b.size(); } int main (){ string s; vector<string> text; while (getline(cin, s)) text.push_back(s); stable_sort (text.begin(), text.end(), cmp); for (int i = 0; i < text.size() && text[i].size() == text[0].size(); ++ i) cout << text[i] << endl; for (int i = 0; i < text.size(); ++ i){ if (text[i].size() == text[text.size() - 1].size()) cout << text[i] << endl; } }// 挺坑的,字符串里面有空格一开始没注意到被卡了一下,这代码应该挺好理解的了,就不加别的注释了XD
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(const string& s1,const string& s2){
return s1.size()<s2.size();
}
int main(){
string str;
int min,max;
vector<string> all;
while(getline(cin,str))
all.push_back(str);
stable_sort(all.begin(),all.end(),cmp);
min=all[0].size();
max=all[all.size()-1].size();
for(string s:all){
if(s.si***||s.size()==max)
cout<<s<<endl;
}
return 0;
}
#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> vecs; string s; while (getline(cin, s)) { vecs.push_back(s); } vector<int> minIndex; vector<int> maxIndex; int min = 1001; int max = 0; for (int i = 0; i < vecs.size(); ++i) { if (vecs[i].length() == min) { minIndex.push_back(i); } else if (vecs[i].size() < min) { min = vecs[i].length();//不知为何代码会被和谐成乱码,上面用length也是这个原因,醉了 minIndex.clear(); minIndex.push_back(i); } if (vecs[i].size() == max) { maxIndex.push_back(i); } else if (vecs[i].size() > max) { max = vecs[i].size(); maxIndex.clear(); maxIndex.push_back(i); } } for (int i = 0; i < minIndex.size(); ++i) { cout << vecs[minIndex[i]] << endl; } for (int i = 0; i < maxIndex.size(); ++i) { cout << vecs[maxIndex[i]] << endl; } return 0; }
#include <iostream> using namespace std; int main() { string ln,sh,tem; getline(cin,sh); ln=sh; int slen=sh.length(); int llen= ln.length(); while(getline(cin,tem)) { int x=tem.length(); if(x>llen){ ln=tem; llen=x; } else if(x==llen) ln+="\n"+tem; else if(x<slen){ sh=tem; slen=x; } else if(x==slen) sh+="\n"+tem; } cout<<sh<<endl<<ln<<endl; }
#include <bits/stdc++.h> using namespace std; const int maxn=1005; string s[maxn],t; bool cmp(string a,string b) { return a.size()<b.size(); } int main() { int n=0; while(getline(cin,t)) { s[n++]=t; } stable_sort(s,s+n,cmp); int minlen=s[0].size(), maxlen=s[n-1].size(); for(int i=0;i<n;++i) { if(s[i].si***len||s[i].size()==maxlen) cout<<s[i]<<endl; } return 0; }
虽然通过了,但是如果有两行字符串是相同的,那是输出一个还是两个呢?
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct my_string { char s[1000]; int len; }str; int main() { str strings[100]; int sum = 0; int min = 1000; int max = 0; while(gets(strings[sum].s)!= NULL) { strings[sum].len = strlen(strings[sum].s); if(strings[sum].len < min) { min = strings[sum].len; } if(strings[sum].len > max) { max = strings[sum].len; } sum++; } for(int i = 0; i < sum; i++) { if(strings[i].len == min) { printf("%s\n", strings[i].s); } } for(int i = 0; i < sum; i++) { if(strings[i].len == max) { printf("%s\n", strings[i].s); } } return 0; }
#include <stdio.h>//方法2,1.找出所有字符串的长度,并找到最大最小值 最大值初始化为0最小值初始化为1000方便计算 #include <string.h>//2.对应最大最小值的字符串输出//3.此方法省去了排序的过程效率更高 struct String { char s[1000]; int len; }str[100]; int main() { int i,n=0,min=1000,max=1;//反着初始化,最长0最短1000 while(gets(str[n].s)!=NULL)//gets对应NULL,scanf对应EOF {//不加结束语句在本地测试的时候输入停不下来 str[n].len= strlen(str[n].s); if(str[n].len<min)//找最小值 min=str[n].len; if(str[n].len > max)//找最大值 max=str[n].len; n++;//n作为s[]的下标 } for(i=0;i<n; i++)//输出所有长度一致的最小值 if(str[i].len == min) printf("%s\n", str[i].s); for(i=0;i<n;i++)//输出所有长度一致的最大值 if(str[i].len==max) printf("%s\n", str[i].s); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ vector<string> temp; string a; int max = 0,min = 1001; while(getline(cin,a)){ // 边读入边记录最长和最短的长度,然后扫两遍数组,输出 temp.push_back(a); if(a.size()>max) max = a.size(); if(a.size()<min) min = a.size(); } for(int i=0;i<temp.size();i++){ if(temp[i].si***) cout<<temp[i]<<endl; } for(int i=0;i<temp.size();i++){ if(temp[i].size()==max) cout<<temp[i]<<endl; } }
#include <climits> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string str; int maxlen = 0, minlen = INT_MAX; //最短和最长字符串长度 vector<string>shortest, longest; //最短和最长字符串 while (getline(cin, str)) { if (str.length() > maxlen) { maxlen = str.length(); longest.clear(); longest.push_back(str); } else if (str.length() == maxlen) { longest.push_back(str); } if (str.length() < minlen) { minlen = str.length(); shortest.clear(); shortest.push_back(str); } else if (str.length() == minlen) { shortest.push_back(str); } } for (const auto& str : shortest) { //输出最短字符串 cout << str << endl; } for (const auto& str : longest) { //输出最长字符串 cout << str << endl; } return 0; }
using namespace std;
int main() {
string str; string res[max_len] = {}; int count = 0; while (getline(cin,str)) { res[count] = str; count++; } for (int i = 0; i count-1; i++) { for (int j = 0; j count - i-1; j++) { if (res[j].length() > res[j + 1].length()) { string tmp = res[j]; res[j] = res[j + 1]; res[j + 1] = tmp; } } } string s1 = res[0]; string s2 = res[count-1]; for (int i = 0; i count ; i++) { if (res[i].length() == s1.length()) { cout res[i] endl; } } for (int i = 0; i count ; i++) { if (res[i].length() == s2.length()) { cout res[i] endl; } } return 0;
}
#include <iostream> #include <string> using namespace std; int main() { string strArr[100]; string str; int len = 0; while (getline(cin, str)) {// 注意 while 处理多个 case strArr[len++] = str; } // 按照字符串长度排序 for (int i = 0; i < len; i++) { for (int j = len - 1; j > i; j--) { if (strArr[j].size() < strArr[j - 1].size()) { string temp = strArr[j]; strArr[j] = strArr[j - 1]; strArr[j - 1] = temp; } } } int minLen = strArr[0].size(); for (int i = 0; i < len; i++) { if (strArr[i].size() == minLen) { cout << strArr[i] << endl; } } int maxLen = strArr[len - 1].size(); int count = 0; for (int i = len - 1; i >= 0; i--) { if (strArr[i].size() == maxLen) { count++; } } for (int i = len - count; i < len; i++) { cout << strArr[i] << endl; } } // 64 位输出请用 printf("%lld")
#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> using namespace std; int main() { vector<string> shorts,longs; string temp; int slen=0,llen=0; while(getline(cin,temp)){ if(slen==0&&llen==0){ slen=temp.size(); llen=temp.size(); shorts.push_back(temp); longs.push_back(temp); } else{ if(temp.size()==slen){ shorts.push_back(temp); } else if(temp.size()<slen){ shorts.clear(); shorts.push_back(temp); slen=temp.size(); } if(temp.size()==llen){ longs.push_back(temp); } else if(temp.size()>llen){ longs.clear(); longs.push_back(temp); llen=temp.size(); } } } for(int i=0;i<shorts.size();i++){ cout<<shorts[i]<<endl; } for(int i=0;i<longs.size();i++){ cout<<longs[i]<<endl; } }
#include <cstdio> #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool comp(string lhs,string rhs){ return lhs.length() < rhs.length(); } int main(){ vector<string> vec; string str; while(getline(cin,str)){ vec.push_back(str); } sort(vec.begin(),vec.end(),comp);//按字符串长度递增排序 int maxlen = 0,minlen = 1000; vector<string>::iterator it; for(it = vec.begin();it != vec.end();++it){ if((*it).length() > maxlen) maxlen = it->length(); if((*it).length() < minlen) minlen = it->length(); } for(it = vec.begin();it != vec.end();++it){ if(it->length() == minlen || it->length() == maxlen){ cout<<*it<<endl; } } return 0; }