2 条题解

  • 2
    @ 2024-9-1 13:06:39

    非常简单的题目 我选择用to_string来解决 知州所众,to_string函数可以把int类型转为string 例如:

    int n;
    cin>>n;
    string s=to_string n;//转换!
    cout<<s;
    

    此时 如果输入任意不超过int整形的数,就会按原数输出 以此类推,可以加一个check函数,来判断该数中是否存在字符'4',就像这样:

    bool check(string s){//bool类型返回值的函数
      for(int i=0;i<s.length();i++)//遍历字符串
        if(s[i]=='4')return 0;//返回否定(false)
      return 1;//返回肯定(true)
    }
    

    最后就一目了然了好吧: 先定义一个储存次数的变量cnt=0 接着从10000遍历到99999,然后把每一个遍历到的数用to_string函数转为字符串,接下来用check函数检查字符串是否合法(不出现字符'4'),若合法,cnt自加(++)

    AC Code

    #include<bits/stdc++.h>//万能头文件
    using namespace std;//标准命名空间
    bool check(string s){//check函数
    	for(int i=0;i<s.length();i++)if(s[i]=='4')return 0;
    	return 1;
    }
    int main() {
    	int cnt=0;//计数变量
    	for(int i=10000;i<=99999;i++){//遍历
    		string s=to_string(i);//转为字符串
    		if(check(s))cnt++;//判断是否合法,cnt++
    	}
    	cout<<cnt;//输出计数器
    	return 0;
    }
    

    请勿抄袭,弄懂为止

    • 0
      @ 2024-8-13 8:10:50
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
      cout<<"52488";
      return 0;
      }
      
      
      
      

      以前有bug现在修复了

      • 1

      信息

      ID
      6
      时间
      1000ms
      内存
      128MiB
      难度
      9
      标签
      递交数
      13
      已通过
      5
      上传者