NowCoder每天要给许多客户写电子邮件。正如你所知,如果一封邮件中包含多个收件人,收件人姓名之间会用一个逗号和空格隔开;如果收件人姓名也包含空格或逗号,则姓名需要用双引号包含。
现在给你一组收件人姓名,请你帮他生成相应的收件人列表。
输入包含多组数据。
每组数据的第一行是一个整数n (1≤n≤128),表示后面有n个姓名。
紧接着n行,每一行包含一个收件人的姓名。姓名长度不超过16个字符。
对应每一组输入,输出一行收件人列表。
3 Joe Quan, William Letendre,Bruce 2 Leon Kewell
Joe, "Quan, William", "Letendre,Bruce" Leon, Kewell
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int n=in.nextInt(); int tn=n; StringBuilder sb=new StringBuilder(); while(n--!=-1){ String s=in.nextLine(); int len=s.length(); char[] cA = s.toCharArray(); boolean hasFh=false; for (int i = 0; i < len; i++) { char c = cA[i]; if(c==',' || c==' '){ hasFh=true; break; } } if(hasFh){ sb.append("\""+s+"\""); } else{ sb.append(s); } if(n!=-1 && n!=tn-1){ sb.append(", "); } } System.out.println(sb); } } }
// write your code here cpp #include<iostream> #include<string> using namespace std; int main() { int n=0; while(cin>>n) { string str; getchar();//数字后面有个换行符不要忘了 for(int i=0;i<n;i++){ getline(cin,str); if(str.find(' ')!=str.npos||str.find(',')!=str.npos) cout<<'"'<<str<<'"'; else cout<<str; (i+1!=n)?cout<<", ":cout<<endl;//最后一个不用输出逗号空格按要求输出换行 } } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(){ int n; while(cin >> n){ if(n == 0){ continue; } cin.get(); vector<string> v(n); for(int i = 0; i < n; ++i){ getline(cin, v[i]); if(v[i].find(",") != string::npos || v[i].find(" ") != string::npos){ v[i].insert(0, "\""); v[i].insert(v[i].size(), "\""); } } for(int i = 0; i < n; ++i){ cout << v[i]; if(i < n - 1){ cout << ", "; } else{ cout << endl; } } } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){ int n=scanner.nextInt(); scanner.nextLine(); ArrayList<String> arrayList=new ArrayList<String>(); for(int i=0;i<n;i++){ String name=scanner.nextLine(); if(name.contains(" ")||name.contains(",")){ arrayList.add("\""+name+"\""); }else{ arrayList.add(name); } } for(int i=0;i<n-1;i++){ System.out.print(arrayList.get(i)+", "); } System.out.println(arrayList.get(n-1)); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int n = Integer.parseInt(sc.nextLine()); String[] arr = new String[n]; for (int i = 0; i < n; i ++ ) { String s = sc.nextLine(); if(s.contains(",") || s.contains(" ")) s = "\"" + s + "\""; arr[i] = s; } for (int i = 0; i < n; i ++ ) { if(i == n - 1) System.out.println(arr[i]); else System.out.print(arr[i] + ", "); } } } }
#include<iostream> (720)#include<string> using namespace std; int main() { int n; string name; string namelist; while (cin>>n) { getchar( ); namelist.clear(); while (n> 0) { getline(cin, name); if (name.find(',') != -1 || name.find(' ') != -1) { name.resize(name.size() + 1); for (int i = name.size() - 1; i > 0; i--) { name[i] = name[i - 1]; } name[0] = '"'; name.push_back('"'); if (n != 1) { name.push_back(','); name.push_back(' '); } namelist += name; n--; } else { if (n != 1) { name.push_back(','); name.push_back(' '); } namelist += name; n--; } } cout << namelist << endl; } return 0; }
int main() { int n; while(cin>>n) { getchar(); //刷新缓冲区 while(n--) { string str; getline(cin, str); if(str.find(',')!= -1 || str.find(' ')!= -1) { str.insert(str.begin(), '"'); str.insert(str.end(), '"'); } if(n == 0) cout<<str<<endl; else cout<<str<<", "; } } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n; while(cin>>n) { cin.get(); string name; for(int i=0;i<n;i++) { getline(cin,name); if(i!=n-1) { if(name.find(',')!=-1||name.find(' ')!=-1) cout<<'\"'<<name<<'\"'<<','<<' '; else cout<<name<<','<<' '; } else { if(name.find(',')!=-1||name.find(' ')!=-1) cout<<'\"'<<name<<'\"'<<endl; else cout<<name<<endl; } } } return 0; }
// write your code here import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { String s = sc.nextLine(); if (s.contains(" ") || s.contains(",")) { System.out.print("\"" + s + "\""); } else { System.out.print(s); } if (i < n - 1) System.out.print(", "); } System.out.println(); } } }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <functional> #include <climits> #include<unordered_map> #include<unordered_set> using namespace std; bool containSpaceOrComma(string& s) { for (auto&c : s) { if (c == ' ' || c == ',') return true; } return false; } int main() { //freopen("in.txt", "r", stdin); int n; while (cin >> n) { getchar(); string s; for (int i = 0; i < n; ++i) { getline(cin, s); if (containSpaceOrComma(s)) cout << "\"" << s << "\""; else cout << s; if (i < n - 1) cout << ", "; } cout << endl; } }
import java.util.Scanner; public class Main6 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()){ int first = Integer.parseInt(scan.nextLine()); String[] arr1 = new String[first]; for(int i = 0; i < first; i++){ String line = scan.nextLine(); if (line.contains(" ") || line.contains(",")){ line = "\"" + line + "\""; } arr1[i] = line; } if (arr1.length > 0){ System.out.println(String.join(", ", arr1)); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); in.nextLine(); StringBuffer s = new StringBuffer(); while(n != 0) { String name = in.nextLine(); if(name.contains(",") || name.contains(" ")) { s.append("\""); s.append(name); s.append("\""); } else { s.append(name); } if(n != 1) { s.append(", "); } n--; } System.out.println(s); } } }
import java.util.Scanner; /* * 收件人列表 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String strNum = sc.nextLine(); int num = Integer.parseInt(strNum); for (int i = 0; i < num; i++) { String str = sc.nextLine(); if (i == num - 1) { if (str.contains(",") || str.contains(" ")) { System.out.print("\"" + str + "\""); } else { System.out.print(str+" "); } } else { if (str.contains(",") || str.contains(" ")) { System.out.print("\"" + str + "\"" + ", "); } else { System.out.print(str + ", "); } } } System.out.println(); } } }
#include<iostream> #include<string> #include<vector> using namespace std; int main() { int n; while(cin >> n) { getchar(); vector<string> v(n); for(int i = 0; i < n; i++) { string name = ""; getline(cin,name); v[i] = name; } for(int i = 0; i < n; i++) { if(v[i].find(',') != string::npos || v[i].find(' ') != string::npos){ string tmp = "\"" + v[i] + "\""; swap(tmp,v[i]); } } for(int i = 0; i < v.size()-1; i++){ cout << v[i] << ", "; } cout << v[v.size()-1] << endl; } return 0; }
// write your code here
/*重点是审题,用一个逗号和空格隔开;如果收件人姓名也包含空格或逗号 */
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
int count = sc.nextInt();
sc.nextLine();
ArrayList<String> names = new ArrayList<String>();
for(int i = 0; i <count;i++){
String nameStr = sc.nextLine();
if(nameStr.indexOf(',')>=0 || nameStr.indexOf(' ')>= 0){
nameStr = "\""+nameStr+"\"";
}
names.add(nameStr);
}
for(int j = 0; j <names.size();j++){
String name = names.get(j);
System.out.print(name);
if(j<count-1){
System.out.print(", ");
}
}
System.out.println();
}
}
}
// write your code here import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNextLine()){ int n=sc.nextInt(); for(int i=0;i<n;i++){ String str=sc.nextLine(); if(str.contains(" ")||str.contains(",")){ if(i==n-1){ System.out.print("\""+str+"\""); } else{ System.out.print("\""+str+"\""); System.out.print(", "); } } else{ if(i==n-1){ System.out.print(str); } else{ System.out.print(str+", "); } } } System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int n = Integer.parseInt(sc.nextLine()); String result = ""; for (int i = 0; i < n; i++) { String cur = sc.nextLine(); if (cur.contains(",") || cur.contains(" ")) cur = "\"" + cur + "\""; if(i != n-1) { result += cur; result += ", "; }else result += cur; } System.out.println(result); } } }2. 用数组存储(注意输出格式)
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int n = Integer.parseInt(in.nextLine()); String[] arr = new String[n]; for (int i = 0; i < n; i++) { String cur = in.nextLine(); if (cur.contains(",") || cur.contains(" ")) cur = "\"" + cur + "\""; arr[i] = cur; } for (int i = 0; i < n; i++) { if(i == n-1) System.out.println(arr[i]); else System.out.print(arr[i] + ", "); } } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); in.nextLine();//换行操作 List<String> list = new ArrayList<>(); for(int i = 0;i < n;i++){ String arr = in.nextLine(); if(arr.contains(",") || arr.contains(" ")){ list.add("\""+arr+"\""); }else{ list.add(arr); } } for(int i = 0;i < n - 1;i++){ System.out.print(list.get(i) + ", "); } System.out.println(list.get(n - 1)); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); sc.nextLine(); for(int i = 0;i < n;i++){ String s = sc.nextLine(); if(s.contains(" ") || s.contains(",")){ System.out.print("\"" + s + "\""); }else{ System.out.print( s ); } if(i == n - 1){ System.out.println(); }else{ System.out.print( ", " ); } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = Integer.parseInt(scanner.nextLine()); String ans = ""; for (int i = n; i > 0; i--) { String s = scanner.nextLine(); if (s.contains(",") || s.contains(" ")) { s = "\"" + s + "\""; } if (i != 1) { ans = ans + s + ", "; } else { ans = ans + s; } } System.out.println(ans); } } }