First, title:
Given a positive integer in decimal, write down all integers starting with 1, to N, and then count the number of "1" that appears. Requirements: 1. Write a function f (n) and return the number of "1" that appears between 1 and N. For example F (12) = 5. 2. In the range of 32-bit integers, the maximum n of "f (n) =n" satisfying the condition is what.
Two. Design ideas.
Figure out the number of occurrences of each of the 1, and add up to the total number of times.
Third, the source program
#include <iostream.h>
#include <math.h>
int main ()
{
int num,a[10],n,k,s=0,b,c;
Double b,c;
cout<< "Please enter the number of digits of the input number:" <<endl;
cin>>n;
cout<< "Please enter Number:" <<endl;
cin>>num;
cout<<endl;
K=num;
for (int i=0;i<n;i++)
{
a[i]=num%10;
NUM=NUM/10;
}
for (i=0;i<n;i++)
{
if (a[i]>1)
{
B=pow (i+1);
C=pow (10,i);
s=s+ (k/b+1) *c;
cout<<a[i]<< "" <<s<<endl;
}
else if (a[i]==1)
{
B=pow (i+1);
C=pow (10,i);
s=s+k/b*c+k%c+1;
cout<<a[i]<< "<<k/b*c<<" "<<k%c<<" "<<s<<endl;
}
Else
{
B=pow (i+1);
C=pow (10,i);
S=s+k/b*c;
cout<<a[i]<< "" <<s<<endl;
}
}
cout<< "from 0 to" <<k<< "1 of the number is:" <<endl;
cout<<s<<endl;
return 0;
}
Iv. Results
V. Summary of the Experiment
This experiment is relatively simple, but the main is to find the law, first find out the law, write algorithm. Later in the experiment also need to think of a number of different methods of calculation.
Number 1 Games