【vijos】P1848 記數問題
阿新 • • 發佈:2019-02-01
描述
試計算在區間 1 到 n 的所有整數中,數字 x(0 ≤ x ≤ 9)共出現了多少次?例如,在 1 到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,數字 1 出現了 4 次。
格式
輸入格式
輸入共 1 行,包含 2 個整數 n、x,之間用一個空格隔開。
輸出格式
輸出共 1 行,包含一個整數,表示 x 出現的次數。
樣例1
樣例輸入1[複製]
11 1
樣例輸出1[複製]
4
限制
每個測試點1s。
提示
對於 100%的資料,1≤ n ≤ 1,000,000,0 ≤ x ≤ 9。
程式碼:
#include<iostream> using namespace std; int main() { int n,x; cin>>n>>x; int cnt=0; for(int i=1;i<=n;i++){ int t=i; while(t){ if(t%10==x) cnt++; t/=10; } } cout<<cnt<<endl; return 0; }