15 条题解

  • 1
    @ 2025-3-1 15:47:25
    #include<bits/stdc++.h>  
    using namespace std;
    int main(){
          string  s=" ";
          getline(cin,s);
          for(int i=0;i<s.size();i++){
          if(s[i]==' '&&s[i+1]==' ') continue;
                  cout<<s[i];
         }
       return 0;
    }
    
    • 1
      @ 2024-4-7 15:04:10
      #include <stdio.h>
      #include <iostream>
      #include <string>
      using namespace std;
      int main(){
      
      string str;
      
      while(cin>>str){
      	cout<<str<<' ';
      	}
      }
      
      • 1
        @ 2024-4-7 8:54:53

        只要不是连续空格就输出

        #include<bits/stdc++.h>
        using namespace std;
        typedef long long LL;
        
        int main()
        {
        	string str;
        	getline(cin,str);
        	for(int i=0;i<str.length();i++)
        	{
        		if(!(str[i]==' '&&str[i+1]==' '))
        		cout<<str[i];	}
        	return 0;
        }
        
        • @ 2024-4-7 8:58:41

          建议使用str.size() 速度比length快很多

      • 0
        @ 2025-4-6 14:22:52

        #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
          @ 2025-3-25 20:02:39

          #include<iostream> #include<string> using namespace std; int main() { string str; while (cin >> str) { cout << str << ' '; } //直接输入字符串,然后打印每一个字符串,且后面跟一个空格 return 0; }

          • 0
            @ 2025-3-6 18:14:58
            /*
            思路分析
            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
              @ 2024-10-12 19:40:17

              #include<bits/stdc++.h> using namespace std; int main() { string n; string a[12]; int i=0; while(cin>>n) { a[i]=n; i+=2; }

              for(int i=0;i<12;i++) { if(i%2==0) { cout<<a[i]; } else cout<<" "; }

              return 0;
              

              } // 0 1 2 3 4 5 6 7 8 9 10 11

              • 0
                @ 2024-7-8 3:28:06
                #include <iostream>
                #include<stdio.h>
                #include <vector>
                #include <string>
                using namespace std;
                
                
                int main() {
                	string str;
                	while (cin >> str)
                		cout << str << ' ';
                	
                	return 0;
                }
                
                • 0
                  @ 2024-7-8 3:25:41
                  #include <iostream>
                  #include<stdio.h>
                  #include <vector>
                  #include <string>
                  using namespace std;
                  
                  
                  int main() {
                  	string str;
                  	
                  	getline(cin, str);
                  	int n = str.size();
                  	for (int i = 0; i < n; i++) {
                  		if (str[i] != ' ' || (str[i] == ' ' && str[i + 1] != ' '))
                  			cout << str[i];
                  	}
                  	return 0;
                  }
                  
                  • 0
                    @ 2024-4-9 9:29:48
                    #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
                      @ 2024-4-7 22:55:16

                      #include<bits/stdc++.h> using namespace std; int main(){ int n=0; string s; getline(cin,s); n=s.size(); for(int i=0;i<n;i++){ if(s[i]' '&&s[i+1]' ') ; else cout <<s[i]; } return 0; }

                      • 0
                        @ 2024-4-7 12:01:40
                        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]+" ");
                        			}
                        		}
                        		
                        	}
                        }
                        
                        • 0
                          @ 2024-4-6 10:27:21
                          #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<<" ";
                              }
                          
                          • 0
                            @ 2024-4-6 9:09:10
                            #include<bits/stdc++.h>
                            using namespace std;
                            
                            int main(){
                            	string str;
                            	getline(cin,str);
                            	int n=str.length();
                            	for(int i=0; i<n; i++){
                            		if(str[i]==' '&&str[i+1]==' '){
                            			continue;
                            		}
                            		cout<<str[i];
                            	}
                            	
                            	return 0;
                            }
                            
                            • 0
                              @ 2024-4-5 9:40:59
                              #include <bits/stdc++.h>
                              using namespace std;
                              
                              int main()
                              {
                                  string s;
                                  getline(cin,s); 
                                  for(int i = 0;i < s.size();i++)
                                  {
                                      if((s[i] != ' ')||(s[i] == ' '&& s[i+1] != ' '))
                                      cout << s[i];
                                  } 
                                  return 0;  
                              }
                              
                              • 1

                              信息

                              ID
                              11
                              时间
                              1000ms
                              内存
                              256MiB
                              难度
                              4
                              标签
                              递交数
                              2203
                              已通过
                              961
                              上传者