15 条题解

  • 2
    @ 2025-3-13 23:24:27
    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
    
        string s;
        int c=0;
        cin>>s;
        while(cin>>s){
            c+=s.length();
        }
        cout<<c;
        return 0;
    }
    
    • 1
      @ 2025-3-22 22:19:14
      #include<bits/stdc++.h>
      using namespace std;
      
      int main(){
      int n;
      int i=0,num=0;
      string s;
      scanf("%d\n",&n);
      getline(cin,s);
      while(i<n)
      {
      if(s[i]!=' ') num++;
      i++;
      }
      
      cout<<num;
      return 0;
      }
      
      • 1
        @ 2025-3-1 15:58:14

        👍

        #include<bits/stdc++.h>
        using namespace std;
        int main(){
           string n;
           string s;
           getline(cin,n);
           getline(cin,s);
           int count=0;
           for(int i=0;i<stoi(n);i++){
              if(s[i]!=' ') count++;
           }
           cout<<count; 
           return 0;
        }
        
        • 1
          @ 2024-4-9 16:52:44
          #include <bits/stdc++.h>
          using namespace std;
          int main()
          {
          	ios::sync_with_stdio(0);
          	cin.tie(0);
          	cout.tie(0);
          	string n;
          	string s;
          	getline(cin,n);
          	getline(cin,s);
          	int n1 = stoi(n);
          	int count = 0;
          	for(int i = 0; i < n1; i++){
          		if(s[i] != ' '){
          			count++;
          		}
          	}
          	cout << count << endl;
          	
          	
          	
          	return 0;
           } 
          
          • @ 2024-4-9 19:50:37

            请问我不可以用getchar清楚缓冲区吗

          • @ 2024-4-9 19:50:56
            #include<bits/stdc++.h>
            using namespace std;
            int main()
            {
                string a;
                getline(cin,a);
                int sum=0;
                for(int i =0;i < a.length();i++)
                {
                    if(a[i]!=' ')
                    sum++;
                }
                cout<<sum;
                return 0;
            }
            
        • 0
          @ 2025-3-25 20:45:27

          #include<iostream> #include<string> using namespace std; int main() { int n; int i = 0, num = 0; string s; scanf("%d\n", &n); //不好用cin>>n //cin >> n 会读取整数 5,但不会消耗后面的换行符 \n。 //后续 getline(cin, str) 会立即读取到残留的 \n,导致 str 被赋值为空字符串。 getline(cin, s); while (i < n) { if (s[i] != ' ') num++; i++; } cout << num; return 0; }

          • 0
            @ 2024-12-13 11:17:03
            #include <bits/stdc++.h>
            
            using namespace std;
            
            int n;
            string str, strn;
            int cnt = 0;
            
            int main() {
                getline(cin, strn);
                n = stoi(strn);
                getline(cin, str);
                for (int i = 0; i < n; i++) 
                    if (str[i] != ' ' && str[i] != '\n') 
                        cnt++;
                cout << cnt << endl;
                return 0;
            }
            
            • 0
              @ 2024-8-12 16:46:35
              #include <iostream>
              #include <vector>
              #include <limits>
              #include<string>
              using namespace std;
              int main() {
                  int a;
                  string b;
                  cin >> a;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  getline(cin, b);
                  b = b.substr(0, a);
                  int c = 0;
                  for (char d : b) {
                      if (d != ' ') {
                          c++;
                      }
                  }
                  cout << c;
                  return 0;
              }
              //输入a,b然后直接历遍非空输出结果
              
              • 0
                @ 2024-7-8 4:08:38

                `` #include <iostream> #include<stdio.h> #include <vector> #include <string> using namespace std;

                int main() { string str,t; int n=0,m=0; getline(cin, t); getline(cin, str); n = stoi(t); int size = str.length(); for (int i = 0; i < n; i++) { if (str[i] != ' ')m++; } cout << m; return 0; }

                • 0
                  @ 2024-5-16 17:07:53
                  #include<bits/stdc++.h>
                  using namespace std;
                  int main()
                  {
                  
                    int n;
                    scanf("%d",&n);
                    //清空输入标准缓冲区'\n'--->x
                    char x;
                    scanf("%c\n",&x);
                    int ans=0;
                     string str;
                     getline(cin,str);
                     for(int i=0;i<n;i++)
                     {
                      if(str[i]!=' ')
                      {
                          ans++;
                      }
                     }
                      cout<<ans;
                      return 0;
                  } 
                  
                  • 0
                    @ 2024-4-7 12:06:07
                    import java.util.Scanner;
                    
                    public class Main {
                    	public static void main(String[] args) {
                    		Scanner sc = new Scanner(System.in);
                    		int idx = sc.nextInt();
                    		sc.nextLine(); // 读掉换行符防止影响s字符串的读取
                    		String s = sc.nextLine();
                    		sc.close();
                    		String[] st = s.split(" ");
                    		int sum = 0;
                    		for (int i = 0; i < st.length; i++) {
                    			sum += st[i].length();
                    		}
                    		System.out.println(sum);
                    	}
                    }
                    
                    • 0
                      @ 2024-4-6 11:01:07
                      #include<bits/stdc++.h>
                      using namespace std;
                      int main()
                      {
                      	ios::sync_with_stdio(0);
                      	cin.tie(0);
                      	cout.tie(0);
                      	
                      	string x1;
                      	string s;
                      	getline(cin,x1);
                      	getline(cin,s);
                      	int n = stoi(x1);
                      	int a=0;
                      	for(int i=0;i<n;i++)
                      	{
                      		if(s[i]!=' ')
                      			a++;
                      	}
                      	cout<<a;
                      	return 0;
                      }
                      
                      • 0
                        @ 2024-4-6 9:28:42
                        #include<bits/stdc++.h>
                        using namespace std;
                        
                        string n;
                        string str;
                        int sum;
                        
                        int main()
                        {
                        	getline(cin,n);
                        	//getchar();
                        	getline(cin,str);
                        	int t = stoi(n);
                        	for(int i=0;i<t;i++)
                        	{
                        		if(str[i]!=' ') sum++;
                        	}
                        	
                        	cout<<sum<<endl;
                        	
                        	return 0;
                        }
                        
                        • 0
                          @ 2024-4-5 18:42:31
                          #include <bits/stdc++.h>
                          
                          using namespace std;
                          
                          int main()
                          {
                              string k;
                              getline(cin,k);
                              string s;
                              getline(cin,s);
                          
                              int cnt=0;
                              int t = stoi(k); 
                              //cin不能和getline一起用,p7是将k用string类型读入,但是k是整数类型,这行是将k类型由string再转换成int
                          
                              for(int i=0;i<t;i++)
                              {
                                  if(s[i] != ' ') cnt++;
                              }
                              cout<<cnt<<endl;
                          }
                          
                          
                          • 0
                            @ 2024-4-5 10:16:27
                            #include <bits/stdc++.h>
                            using namespace std;
                            
                            int main()
                            {
                            	string num,s;
                            	int n = 0;
                            	getline(cin,num);
                            	getline(cin,s);
                            	int t = stoi(num);
                            	for(int i = 0;i < t;i++)
                            	{
                            		if(s[i] != ' ') n++ ;
                            	}
                            	cout << n;
                            }
                            
                            • -1
                              @ 2025-3-6 11:55:46
                              #include <bits/stdc++.h>
                              using namespace std;
                              
                              int main()
                              {
                                  //思路分析
                                  //1. 用char数组接收数据 vector<char>str 
                                      // 用字符串接收 string str;
                                  //2. 循环解决 数组:for (int i = 0; i < length ; i++)
                                      // 字符串: for(char c: str) ==> char变量 循环依次获取字符串数据
                                  //3. count++
                                  string n;
                                  cin >> n; 
                                  cin.ignore();//消除换行符 cin.ignore();
                                  int count = 0; // 记录字符数量
                                  string str; //用字符串则可以使用getline()函数
                                  //cin.getline(str);//此时里面含有空格 getline(cin, str)
                                  getline(cin , str);
                                  for (char c: str){
                                      if ( c != ' ' && c != '\n'){
                                          count++;
                                      }
                                  }    
                                  //输出结果
                                  cout << count;
                                  return 0;
                              }
                                  //创建数组
                                  //vector<char>str; 动态数组不支持getline用法
                                  //获取数据 
                                  //cin >> 获取会在遇到空格时就停止获取 
                                  //cin.getline()再遇到换行符时停止
                                  //cin.getline(str);// 只能由实际大小数组来使用
                              
                              • 1

                              信息

                              ID
                              12
                              时间
                              1000ms
                              内存
                              256MiB
                              难度
                              7
                              标签
                              递交数
                              2862
                              已通过
                              712
                              上传者