1245. 特别数的和
Opened this issue · 6 comments
maninbule commented
1245. 特别数的和
小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。
请问,在 1 到 n 中,所有这样的数的和是多少?
输入格式
共一行,包含一个整数 n。
输出格式
共一行,包含一个整数,表示满足条件的数的和。
数据范围
1≤n≤10000
输入样例:
40
输出样例:
574
maninbule commented
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 1e9;
int N;
ll cnt = 0;
int main(){
cin>>N;
for(int i = 1;i<=N;i++){
int t = i;
while(t){
int tail = t%10;
if(tail == 2 || tail == 0 || tail == 1 || tail == 9) {
cnt += i;
break;
}
t/=10;
}
}
cout<<cnt<<endl;
return 0;
}
patern2000 commented
#include
#include
using namespace std;
int a[10];
int main()
{
int n;
int sum=0;
cin>>n;
for(int i=1;i<=n;i++){
int j=0;
int x=i;
while(x){
a[j]=x%10;
x/=10;
j++;
}
for(int k=0;k<j;k++){
if(a[k]==2||a[k]==0||a[k]==1||a[k]==9){
sum+=i;
break;
}
}
}
cout<<sum<<endl;
return 0;
}
maobohui commented
#include<iostream>
using namespace std;
typedef long long ll;
int n;
bool judge(int i){
int p=i;
while(p){
if(p%10==2||p%10==0||p%10==1||p%10==9){
return true;
}
p/=10;
}
return false;
}
ll ans = 0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
if(judge(i)){
ans+=i;
}
}
cout<<ans<<endl;
return 0;
}
LJM-Jeson commented
#include <bits/stdc++.h>
using namespace std;
long long sum;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >>n;
for(int i = 1; i <= n; i ++){
int x = i;
while(x){
int t = x % 10;
x /= 10;
if(t == 2 || t == 0 || t == 1 || t == 9){
sum +=i;
break;
}
}
}
cout << sum << endl;
return 0;
}
consult98 commented
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define LL long long
using namespace std;
const int N = 1e5+7;
int n;
int a[10];
int check(int x){
while(x){
if(a[x%10]) return 1;
else x/=10;
}
return 0;
}
int main()
{
cin>>n;
a[2]++,a[0]++,a[1]++,a[9]++;
int ans =0;
for(int i=1;i<=n;i++){
if(check(i)) ans+=i;
}
cout<<ans<<endl;
return 0;
}
xunzhang123 commented
include
using namespace std;
bool check(int a){
while(a){
int t = a%10;
if(t==2 || t==0 || t==1 || t==9){
return true;
}
a/=10;
}
return false;
}
int main( )
{
int n;
cin>>n;
long long sum = 0;
int a=0;
while((++a)!=n+1){
if(check(a)){
sum+=a;
}
}
cout<<sum<<endl;
return 0;
}