http://hero.csdn.net/OnlineCompiler/Index?ID=351&ExamID=346&ExamResultID=139076&ExamNum=3&random=1072116641&from=0
给定一个带通配符问号的数W,问号可以代表任意一个一位数字。
再给定一个整数X,和W具有同样的长度。
问有多少个整数符合W的形式并且比X大?
输入格式
多组数据,每组数据两行,第一行是W,第二行是X,它们长度相同。在[1..10]之间.
输出格式
每行一个整数表示结果。
答题说明:
输入样例
36?1?8
236428
8?3
910
?
5
输出样例
100
0
4
这道题虽然没过。。。。。。但是做了很久,挺有价值的。。。还是写一篇吧
刚开始我想的是直接暴力搜索,每次 排列的列举0-9到问号的地方,然后记录下来这个串
再到下一个问号,最后到最后一个问号的时候枚举0-9,再比较前面的大小这样暴力出来结果是对的,但是超时
以为递归10层,每次10个数据。。。。。。。。。
#include<stdio.h>
char num[15];
int ans=0,last;
void dfs(char *hehe)
{
for(int i=0;i<=last;i++)
{
if(i==last)
{
for(int k='0';k<='9';k++)
{
hehe[i]=k;
for(int j=0;num[j]!='\0';j++)
{
if(num[j]==hehe[j])
{
continue;
}
else if(num[j]>hehe[j])
{
break;
}
else
{
ans++;
break;
}
}
}
hehe[i]='?';
break;
}
if(hehe[i]=='?')
{
for(int j='0';j<='9';j++)
{
hehe[i]=j;
dfs(hehe);
}
hehe[i]='?';
break;
}
}
}
int main()
{
char hehe[15];
while(scanf("%s%s",&hehe,&num)!=EOF)
{
ans=0;
for(int i=0;num[i]!='\0';i++)
{
if(hehe[i]=='?')
{
last=i;
}
}
dfs(hehe);
printf("%d\n",ans);
}
}
然后想到的一个方法是每次不考虑相等的情况,直接只考虑大于,然后把后面的 加上
再考虑相等,这样就可以视为前面的都相等,这样就只考虑后面的
但是执行测试用例失败,第224行输出与标准输出不同,你的输出是:7359
跪了。。。。。。。。找了半天。。。。不知道哪错了。。。。。
中间在第90行数据跪过一次,以为没有考虑到10的10次方 超int 最后用的_int64
没过啊啊啊啊啊啊啊啊啊啊
#include<stdio.h>
char num[40];
char hehe[40];
__int64 ans=0;
int dfs(int n)
{
if(hehe[n]=='?')
{
__int64 temp=1;
for(int i=n+1;num[i]!='\0';i++)
{
if(hehe[i]=='?')
{
temp*=10;
}
}
temp*=('9'-num[n]);
ans+=temp;
if(num[n+1]!='\0')
dfs(n+1);
return 0;
}
if(num[n]>hehe[n])
return 0;
else if(num[n]==hehe[n])
{
dfs(n+1);
}
else
{
__int64 temp=1;
for(int i=n+1;num[i]!='\0';i++)
{
if(hehe[i]=='?')
{
temp*=10;
}
}
ans+=temp;
}
}
int main()
{
while(scanf("%s%s",&hehe,&num)!=EOF)
{
ans=0;
dfs(0);
printf("%I64d\n",ans);
}
}