zoukankan      html  css  js  c++  java
  • 题解 P5015 【标题统计】

    既然这个题这么水

    大家不如来盘点一下算法


    首先说一个事:逗号表达式
    这玩意的值是最后一个表达式的值
    那么我们就可以愉快的放进循环条件里摩擦
    话说这个应该是基础吧,大多数代码都可以这样干
    具体可以后面看代码(会有注释)


    好的,下面是正题(真·不正)


    方法一:getchar法

    (相当于cin.get(a))
    这个知识点在我的题解里:
    https://www.luogu.org/blog/jelly123/solution-p1200
    代码:(getchar)

    #include <cstdio>
    using namespace std;
    int main()
    {
        char a;int s=0;
        while(a=getchar(),a!='
    ')
        {//↑就是了,循环先输入后判断,真香QWQ 
        	if (a!=' ')
    		{
        		s++;
    		}
    	}
    	printf("%d",s);
        return 0;
    }
    

    好了,我们暂且撤离,看看cin.get()版:

    #include <cstdio>
    using namespace std;
    int main(){char a;int s=0;
        while(cin.get(a),a!='
    ')if (a!=' ')s++;
    	cout<<s;
        return 0;}
    

    方法二:getline法

    这个方法小心食用

    #include <iostream>
    #include <cstring>
    using namespace std;
    int main(){int count=0,i=0;string input;
    	getline(cin,input);
    	for (i=0;i<input.size();i++){
    		if (input[i]!=' '&&input[i]!='
    ')count++;}
    	cout<<count;return 0;}
    //为了缩短长度,将就着看吧 
    

    但是cin.getline还没研究出来...
    (真是一个蒟蒻啊QAQ)


    方法三:gets法

    这个方法必须小心(作死)使用!!!

    @shm124
    感谢这位同学提供的for循环里的核心

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    int main(){int ans=0,i=0;char s[8];
    	memset(s,0,sizeof(s));gets(s);
    	for(int i=0;i<8;i++)
            if(s[i]>='A'&&s[i]<='Z')ans++;
            if(s[i]>='a'&&s[i]<='z')ans++;
            if(s[i]>='0'&&s[i]<='9')ans++;
    	cout<<ans;return 0;}
    

    好的,我们现在有了几种基础方法,那能不能看看题...
    没错,5个字符,直接顺序不就好了吗...
    所以...


    方法四:顺序结构硬输入法!!!

    @Lhc_fl 真鸡贼啊

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        int ans=0;
        char c;
        if(cin>>c)ans++; //cin自动去除空格换行
        if(cin>>c)ans++; //cin在读不到数据时返回0
        if(cin>>c)ans++;
        if(cin>>c)ans++;
        if(cin>>c)ans++;
        cout<<ans;
    }
    

    经过了这么多蒟蒻做法(打卡),
    不如来看看库函数之类的蹭一蹭...


    方法五:库函数摩擦法(附加迭代)

    感谢dalao@黄H睿R %%%

    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;
    string st;
    int ans;
    int main()
    {
        getline(cin,st);
        for(string::iterator it=st.begin();it!=st.end();it++)//使用迭代器
         if(isalnum(*it))
          ans++;
        cout<<ans;
        return 0;
    }
    

    方法五里面:#include是库函数的库,
    库函数是isalnum(*it),没错,一开始我没有加上...


    然而我们的方法并没有完...

    彩蛋一:

    #include<iostream>
    using namespace std;
    int ans=0;
    char s;
    int main()
    {
        while(cin>>s)if(s!=' ') ans++;
        cout<<ans<<endl;
        return 0;
    }
    

    强制输入...注意win 系统下调试
    ((ctrl+z)+enter)×2即可..

    彩蛋二:

    现在我们的c++更新真快啊..
    合并都只需要+-了吗?!
    然而是的...
    @Nova_守门员
    你的题解我进行了优化,主函数真是简单

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    string input()
    {
    	char ch;
        string str="";
        ch=getchar();
        while(ch!='
    ')
    	{
            if (ch!=' ')str+=ch;
            ch=getchar();
        }
        return str;
    }
    int main()
    {
    	string s;
    	s=input();
    	cout<<s.size();
    }
    

    好了,就说这么多(真少)
    有什么问题请评论...
    纯知识点整理贴(放心,真人测试可AC)
    蒟蒻求过,辛苦到家

  • 相关阅读:
    Hive 使用问题集锦
    scala def/val/lazy val区别以及call-by-name和call-by-value
    spark学习流程
    Hadoop
    Hive
    Chrome快捷键
    Java API帮助文档
    Java 访问修饰符与非访问修饰符
    java 关键字
    Solr配置Ikanalyzer分词器
  • 原文地址:https://www.cnblogs.com/jelly123/p/10385850.html
Copyright © 2011-2022 走看看