题解分享
题解分享简介
特别数的和(编程题) - 题解
直接转成string再调find方法就行了
```
#include<bits/stdc++.h>
using namespace std;
bool check(int n)
{
string s = to_string(n);
if(s.find("2") != string::npos || s.find("0") != string::npos ||
s.find("1") != string::npos || s.find("9") != string::npos)
{
return true;
}
return false;
}
int main()
{
int n, sum = 0;
cin >> n;
for(int i = 1; i <= n ; i++)
{
if(check(i))
{
sum += i;
}
}
cout << sum;
}
```
查看全文
0
0
1
0
特别数的和(编程题) - 题解
注释已加,思路简单易理解
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;cin >> n;//从1到n
int sum = 0;//感兴趣的数之和
for(int i = 1;i <= n; ++ i){//循环枚举每一个数
int j = i;//用来记录i值
int flag = 0;//标记数是否满足需求
while(j)//检查每一位
{
int tem = j % 10;//拿出最后一位
if(tem == 0 || tem == 1 || tem == 2 || tem == 9){//判断
flag = 1;
break;
}
j /= 10;//去掉最后一位
}
if(flag == 1){//判断标记状态
sum += i;
}
}
cout << sum;//输出结果
return 0;
}
```
查看全文
0
0
1
2
特别数的和(编程题) - 题解
这道题比较容易理解
核心就是取该数的最后一位进行判断即mod运算,如果不是再 / 运算去除该数当前的最后一位再回到mod 运算。。。。循环判断。
```
#include<bits/stdc++.h>
using namespace std;
int n;
int sum;
bool check(int x){
while(x){ //只要数中除首位不是0外,其他位有一位是 0、1、2、9 即可
if(x%10 <= 2 || x%10 == 9) //数位分离判断每一位上是否含有2, 0, 1, 9中的数字
return true;
x/=10; // 若当前位数字不符合条件,使用x /= 10将x除以10并取整,丢弃最低位数字。如:25 :25%10=5 不满足,然后25/10=2再进入if判断
}
return false;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
if(check(i))
sum+=i;
cout<<sum<<endl;
return 0;
}
```
查看全文
0
0
1
1
特别数的和(编程题) - 题解
```
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
int num=i;
while(num>0){
if(num % 10 <= 2 || num % 10 == 9){
sum+=i;
break;
}
num/=10;
}
}
System.out.println(sum);
}
}
```
查看全文
0
0
1
1
特别数的和(编程题) - 题解
```
public static void main(String[] args) throws IOException {
int n = nextInt();
long ans = 0;
for (int i = 1; i < n + 1; i++) {
String s = "" + i;
if (s.contains("2") || s.contains("0") || s.contains("1") || s.contains("9")) {
ans += i;
}
}
out.print(ans);
out.flush();
}
```
查看全文
0
0
0
6
特别数的和(编程题) - 题解
```
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
if (contains(i)) {
sum += i;
}
}
System.out.println(sum);
}
private static boolean contains(int number) {
while (number > 0) {
int digit = number % 10;
if (digit == 2 || digit == 0 || digit == 1 || digit == 9) {
return true;
}
number /= 10;
}
return false;
}
}
```
查看全文
0
0
0
6
特别数的和(编程题) - 题解
```
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int sum=0;
cin>>n;
for(int i=1;i<=n;i++){
string s = to_string(i);
for(int j=0;j<s.length();j++){
if(s[j]=='2'||s[j]=='0'||s[j]=='1'||s[j]=='9'){
sum+=i;
break;
}
}
}
cout<<sum;
return 0;
}
```
查看全文
0
0
0
1
特别数的和(编程题) - 题解
include
using namespace std;
bool check(int x) {
while (x > 0) {
if (x % 10 == 2 || x % 10 == 0 || x % 10 == 1 || x % 10 == 9) {
return true;
}
x /= 10;
}
return false;
}
int main() {
int x, cnt = 0;
cin >> x;
for (int i = 1; i <= x; i++) {
if (check(i)) cnt += i;
}
cout << cnt;
return 0;
}
查看全文
0
0
0
1
特别数的和(编程题) - 题解
```
#include <iostream>
#include <cstring>
using namespace std;
bool check(int x)
{
while(x>0)
{
if(x%10<=2||x%10==9)//判断最后一位数是否含有0、1、2、9四个数字
{
return true;
}
x/=10;
//去除这一位数,循环继续判断左边一位是否符合要求
}
return false;
}
int main()
{
int n;
cin>>n;
int cnt=0;
for(int i=1;i<=n;i++)
{
if(check(i))
{
cnt+=i;
}
}
cout<<cnt;
return 0;
}
```
查看全文
0
0
0
1
特别数的和(编程题) - 题解
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int sum=0;
for(int i=1;i<=n;i++){
int x=i;
while(x){
if(x%10==2 || x%10==0 || x%10==1 || x%10==9){
sum += i;
break;
}
x /= 10;
}
}
cout<<sum<<endl;
return 0;
}
```
查看全文
0
0
0
0



