请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如
输入:EricZ
输出:1=EricZ
输入:David
输出:1=David 2=EricZ
输入:Peter
输出:1=Peter 2=David 3=EricZ
输入:Alan
输出:1=Alan 2=Peter 3=David 4=EricZ
输入:Jane
输出:1=Jane 2=Alan 3=Peter 4=David
请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如
输入:EricZ
输出:1=EricZ
输入:David
输出:1=David 2=EricZ
输入:Peter
输出:1=Peter 2=David 3=EricZ
输入:Alan
输出:1=Alan 2=Peter 3=David 4=EricZ
输入:Jane
输出:1=Jane 2=Alan 3=Peter 4=David
第一行为字符串个数m,接下来m行每行一个字符床,m不超过100,每个字符床长度不超过20。
输出m行,每行按照样例格式输出,注意用一个空格隔开。
5 EricZ David Peter Alan Jane
1=EricZ 1=David 2=EricZ 1=Peter 2=David 3=EricZ 1=Alan 2=Peter 3=David 4=EricZ 1=Jane 2=Alan 3=Peter 4=David
#include<iostream> using namespace std; int main() { int m; char ch[100][20]; cin >> m; for (int i = 0; i < m; i++) cin >> ch[i]; for (int i = 0; i < m; i++) { for (int j = i; j >= 0 && j >= i - 3; j--) cout << i - j + 1 << "=" << ch[j] << " "; cout << endl; } }
#include<bits/stdc++.h> int main(){ int n; char a[4][21]={};//设置可以容纳4个字符串的首尾相连的环形数组 while(scanf("%d ",&n)!=EOF) for(int i=0;i<n;i++){ int s=i%4;//只保存最新的4个字符串 scanf("%s ",a[s]); for(int j=0;j<4;j++) if(a[s][0]!=0){ printf("%d=%s ",j+1,a[s]); s=(s+3)%4; }//倒序输出4个字符串 putchar('\n'); } }
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Stack<String> stack = new Stack<>(); for (int i = 0; i < n; i++) { String s = scanner.next(); stack.remove(s); stack.push(s); for (int j = 1; j <= Math.min(stack.size(),4); j++) System.out.print(j+"="+stack.get(stack.size()-j)+" "); System.out.println(); } } }
用了栈的思想
#include <bits/stdc++.h> using namespace std; void printstack(stack <string> s){ int i=4; while(i--){ if(s.empty()) break; else cout<<4-i<<"="<<s.top()<<" "; s.pop(); } cout<<endl; } int main(){ int m; while(cin>>m){ stack<string>s; string str; while(m--){ cin>>str; s.push(str); printstack(s); } } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(){ int n; cin >> n; string str; vector<string> vec; while(n--){ cin >> str; vec.push_back(str); if(vec.size()<=4){ int j=1; for(int i=vec.size()-1; i>=1; --i, ++j){ cout << j << "=" << vec[i] << ' '; } cout << j << "=" << vec[0] << endl; }else{ for(int i=vec.size()-1, j=1; i>=vec.size()-3; --i, ++j){ cout << j << "=" << vec[i] << ' '; } cout << "4=" << vec[vec.size()-4] << endl; } } }
#include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int main(){ int m; while(scanf("%d",&m) != EOF){ string str; vector<string> vec; for(int i = 0;i < m; ++i){ cin>>str; vec.insert(vec.begin(),str); //vector头部插入字符串 for(int j = 0; j < vec.size() && j <4; ++j){ cout<<j+1<<"="<<vec[j]<<" "; } cout<<endl; } } return 0; }
//双端队列 #include <iostream> #include <string> #include <deque> using namespace std; int main() { int n; while (cin >> n){ getchar(); deque<string> mdeque; for (int i = 0; i < n; ++i){ string s; getline(cin, s); if (mdeque.size() == 4) mdeque.pop_back(); mdeque.push_front(s); for (int j = 0; j < mdeque.size(); ++j) cout << j + 1 << '=' << mdeque[j] << " "; cout << endl; } } }
#include <iostream> using namespace std; int main() { int m; char str[100][20]; cin >> m; for (int i = 0; i < m; i++) { cin >> str[i]; for (int j = i, count = 1; j >= 0 && count <= 4; j--, count++) { cout << count << "=" << str[j]; if (j == 0 || count == 4)cout << endl; else cout << " "; } } }
#include <bits/stdc++.h> using namespace std; int main (){ int n; cin >> n; vector<string> name; while (n --){ string s; cin >> s; name.push_back(s); if (name.size() > 4) name.erase(name.begin()); for (int i = name.size() - 1; i >= 0; -- i){ cout << name.size() - i << "=" << name[i] << " " ; } cout << endl; } }
#include<stdio.h> #include<string.h> typedef struct fact { char c[20]; }fact; int main() { fact a[100]; int n; while (scanf("%d",&n)!=EOF) { int i,j,k,c=0,sum; getchar(); for(i=0;i<n;i++) scanf("%s",a[i].c); j=0; k=0; while (c!=n) { sum=1; for(i=k;i>=j;i--) { printf("%d=%s ",sum,a[i].c); sum++; } printf("\n"); if(k<3) k++; else { k++; j++; } c++; } } return 0; }
#include <iostream> #include <string> #include <stack> using namespace std; void output(stack <string> st) { if(!st.empty()) { int tmp=st.size(); if(tmp>4) tmp=4; for(int i=0;i<tmp;i++) { cout<<i+1<<"="<<st.top()<<" "; st.pop(); } } } int main() { int N; while(cin>>N) { stack <string> st; for(int i=0;i<N;i++) { string s; cin>>s; st.push(s); output(st); cout<<endl } } return 0; }