1049. Counting Ones (30)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (<=230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:12Sample Output:
5
以为pow计算出来的全是整数,结果悲剧了!!!
#include<iostream> #include<vector> #include<cmath> using namespace std; typedef long long LL; int main() { int n=0; cin>>n; vector<int> vv; int tt=n; for(int i=0;tt/10!=0;i++){ vv.push_back((tt%10)); tt/=10; //cout<<vv[i]<<endl; } vv.push_back(tt); //cout<<vv[vv.size()-1]<<endl; int count=0; for(int i=0;i<vv.size();i++){ if(vv[i]==0){ count+=(n/(int)(pow(10,i+1)))*(pow(10,i)); //cout<<(n/(pow(10,i+1)))<<endl; }else if(vv[i]==1){ count+=(n/(int)(pow(10,i+1)))*(pow(10,i))+(n%((int)(pow(10,i))))+1; //cout<<(n/(pow(10,i+1)))<<endl; }else{ count+=((n/(int)(pow(10,i+1)))+1)*(pow(10,i)); //cout<<(n/(pow(10,i+1)))<<endl; } //cout<<count<<endl; } cout<<count<<endl; return 0; }