Blue Bridge Cup: the sum of special numbers
answer:
Since Xiao Ming is only interested in 2, 0, 1, and 9, we directly traverse 1 to n, find the numbers that meet Xiao Ming's interest, and add them.
Since the maximum is five digits, we can use the digits separated from the five digits for comparison.
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int sum = 0;
int place[6];
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int temp = i;
place[1] = temp/10000;
temp = temp-10000*place[1];
place[2] = temp/1000;
temp = temp-1000*place[2];
place[3] = temp/100;
temp = temp-100*place[3];
place[4] = temp/10;
temp = temp-10*place[4];
place[5] = temp;
int j = 1;
for(;j<=5;j++)
{
if(place[j]==0)
{
continue;
}
else
{
break;
}
}
while(j<=5)
{
if(place[j]==2||place[j]==0||place[j]==1||place[j]==9)
{
sum=sum+i;
break;
}
else
j++;
}
}
printf(" %d \n",sum);
return 0;
}