【NOIP2013】【Luogu1980】计数问题

problem

  • 给定数字n(<1000000)和x(<=9)
  • 求将数字1~n排列在一起中出现了多少次“字符x”
  • 样例:对于11 11234567891011中有4个1,答案即为4。

solution

  • 枚举数字1~n,对于每个数,直接模拟除法得到每一位,若==x,答案累加1、

codes

#include<iostream>
using namespace std;
int main(){
    int n, x, ans = 0;  cin>>n>>x;
    for(int i = 1; i <= n; i++){
        int t = i;
        while(t > 0){ if(t%10==x)ans++; t/=10;}
    }
    cout<<ans;
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444682.html