新手村 循环 计数问题

来源:互联网 发布:英语关键词软件 编辑:程序博客网 时间:2024/06/07 22:26

这题差点翻车。

首先,我居然没有想着使用暴力,这显然是不对的,我一开始没有计算复杂度,因此想着用递推的方式求解。但是,该写暴力的时候还是要写暴力。

其次,我写暴力的时候居然填错了函数的参数,我觉得我已经意识模糊了。还是睡觉吧。

#include <cstring>#include <iostream>#include <cmath>#include <stdio.h>#include <stdlib.h>#include <string>#include <iomanip>using namespace std;int count(int n, int x) {    int cnt = 0;    while(n) {        if(n % 10 == x) {            cnt++;        }        n /= 10;    }    return cnt;} int main() {    int n, x;    cin >> n >> x;    int total = 0;    for(int i = 1; i <= n; i++) {        total += count(i, x);    }    cout << total << endl;}
原创粉丝点击