*1的個數
資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
輸入正整數n,判斷從1到n之中,數字1一共要出現幾次。例如1123這個數,則出現了兩次1。例如15,那麼從1到15之中,一共出現了8個1。
輸入格式
一個正整數n
輸出格式
一個整數,表示1出現的資料
樣例輸入
15
樣例輸出
8
數據規模和約定
n不超過30000
/*
Name: 藍橋杯:1的個數
Copyright: 拔劍四顧心茫然
Author: 拔劍四顧心茫然
Date: 20/03/31 21:46
Description: dev-cpp 5.11
*/
#include<iostream>
using namespace std;
int main()
{
int n,cnt=0,temp;
cin >> n;
for(int i=1;i<=n;i++)
{
temp = i;
while(temp)
{
if(temp % 10 == 1)
cnt++;
temp /= 10;
}
}
cout << cnt;
return 0;
}