1、取出x中的每一位数字
int t = x % 10;
x /= 10;
例如:
对数位中含有 2、0 的数字很感兴趣,在 1 到 40 中这样的数包括 2、10 至 32和 40,共 16个,他们的和是 371。请问,在 1 到 n 中,所有这样的数的和是多少?
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int main(){
cin>>n;
int res=0;
for(int i=1;i<=n;i++){
int x=i;
while(x){
int t=x%10;
if(t==2||t==0){
res+=i;
break;
}
x/=10;
}
}
cout<<res;
return 0;
}
2、将字符型数字转为整型数字
int x = 0;
for (int i = 0; i < str.size(); i ++ )
x = x * 10 + str[i] - '0';