F - Undoubtedly Lucky Numbers CodeForces - 244B 4.19
一个数中只能出现两个相同的数字,找出比n小的数中存在多少这样的数。
#include <iostream>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>//sort
#include <map>
#include <string>//map
#include<math.h>
#define ll long long
using namespace std;
ll n;
set<ll>s;//定义集合方便去重
void dfs(int x,int y,ll ans)
{
s.insert(ans);
ll xx=10*ans+x;//这个数只由x和y两个数组成
ll yy=10*ans+y;
if(xx&&xx<=n)//排除xx=0
{
dfs(x,y,xx);
}
if(yy&&yy<=n)
{
dfs(x,y,yy);
}
}
int main( )
{
cin>>n;
s.clear();
for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
dfs(i,j,0);
}
}
cout<<s.size()-1<<endl;//-1是去掉n
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>//sort
#include <map>
#include <string>//map
#include<math.h>
#define ll long long
using namespace std;
ll n;
set<ll>s;//定义集合方便去重
void dfs(int x,int y,ll ans)
{
s.insert(ans);
ll xx=10*ans+x;//这个数只由x和y两个数组成
ll yy=10*ans+y;
if(xx&&xx<=n)//排除xx=0
{
dfs(x,y,xx);
}
if(yy&&yy<=n)
{
dfs(x,y,yy);
}
}
int main( )
{
cin>>n;
s.clear();
for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
dfs(i,j,0);
}
}
cout<<s.size()-1<<endl;//-1是去掉n
return 0;
}
}

浙公网安备 33010602011771号