15 条题解
-
0
#include <bits/stdc++.h> using namespace std;
int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string s; string c; getline(cin,s); for(int i=0;i<s.length();++i) { if(i==0||i==s.length()-1) c.push_back(s[i]); if(i>0&&i<=s.length()-2) { if(s[i] ==' '&&s[i+1]==' ') { continue; } else c.push_back(s[i]); } } cout<<c<<"\n"; return 0;
}
-
0
/* 思路分析 1. 接收字符串 只有一行 第一种想法:循环接收数据为字符数组 因为cin遇到空格会停止 最后循环加上一个空格 但是好像没有字符串数组 char数组好像也不行 第二种想法:用字符串直接接收 再用循环在不是空格的字符处传给动态字符数组 再循环加上一个空格 这个方法也废了 因为这样只能在每个字符后加空格 不是单词之后 第三种想法: 我创建一个字符数组 用cin输入遇到空格就停止 就是一个单词 再把它立即输出 输出时加上一个空格 perfect 遗憾的是这个想法也报废了 因为动态数组不能这样输入 幸运的是直接用字符串输入就解决啦 学到第四种: 使用stringstream A(B)分割函数 A是分割后的字符串 它的里面是动态字符串流 分割后不会存在空格 可以添加和提取 原理跟cin 和 cout 相似 A << 输入 A >> 输出 #include <bits/stdc++.h> using namespace std; int main() { string str; while(cin >> str) { cout << str << ' '; } return 0; } #include <bits/stdc++.h> using namespace std; int main() { string str; getline(cin , str); stringstream str1(str); //分割str放在str1字符串流里面 string str2; while (str1 >> str2) { //str2逐一提取每个单词 cout << str2 << ' '; } return 0; } */
-
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string str; getline(cin,str); for(int i = 0; i < str.size();i++){ if((str[i] == ' ' && str[i + 1] != ' ') || str[i] != ' '){ cout << str[i]; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; while(cin >> s){ cout << s << " "; } return 0; }
-
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); sc.close(); String[] st = s.split("\\s+"); // 正则表达式 \\s+ 表示 一个或多个空白字符 for (int i = 0; i < st.length; i++) { if(i == st.length - 1) System.out.print(st[i]); else { System.out.print(st[i]+" "); } } } }
- 1
信息
- ID
- 11
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 2203
- 已通过
- 961
- 上传者