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
#include <cstdio>#include <cstdlib>#include <iostream>#include <deque>#include <queue>#include <cstring>#include <vector>#include <string>#include <iomanip>#include <map>#include <set>#include <cmath>#include <stack>#include <cmath>#include <algorithm>using namespace std;#define max1 1000000int main() { string n; cin>>n; int sum=0; for(int i=0;i<n.size();i++){int mid=n[i]-'0';int left=atoi(n.substr(0,i).c_str());int right=atoi(n.substr(i+1,n.size()).c_str());int ex=n.size()-1-i;if(mid==0)sum+=left*pow(10,ex);else if(mid==1)sum+=pow(10,ex)*left+1+right;else sum+=pow(10,ex)*(left+1);}cout<<sum<<endl; return 0;}
新闻热点
疑难解答