帶通配符的數

給定一個帶通配符問號的數W,問號能夠表明任意一個一位數字。再給定一個整數X,和W具備相同的長度。問有多少個整數符合W的形式而且比X大?
輸入格式:
多組數據,每組數據兩行,第一行是W,第二行是X,它們長度相同。在[1..10]之間。
輸出格式:
每行一個整數表示結果。
輸入樣例:
36?1?8
236428
8?3
910
輸出例樣
100
參考代碼:
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<exception>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 long int getcount(char str1[],char str2[],int length)
 9 {
10     if(str1==NULL||str2==NULL) return 0;
11     int number=0,num1=0,num2=0;
12     int sum=0;
13     for(int i=0;str1[i]!='\0';++i)//統計'?'出現的次數
14     {
15         if(str1[i]=='?')
16             number++;
17     }
18     if(*str1<*str2&&*str1!='?') return 0;
19     if(number==0) return (atol(str1)>atol(str2) ? 1 : 0);//str1不含'?'
20     if(*str1>*str2&&*str1!='?') return pow(10,number);
21     int c=0;
22     while(*(str1+c)>=*(str2+c)&&*(str1+c)!='\0'||(*(str1+c)=='?'&&(str1+c)!='\0'))
23     {
24         c++;
25         if(*(str1+c)<*(str2+c)&&*(str1+c)!='?')
26         {
27           for(i=0;i<c;i++)
28           {
29               if(*(str1+i)=='?')
30               {
31               sum=sum+('9'-str2[i])*pow(10,number-1-num1);
32               num1++;
33               }
34           }
35               return sum;   
36         }
37     }
38     for(i=0;i<length;i++)
39     {
40         if(str1[i]=='?')
41         {sum=sum+('9'-str2[i])*pow(10,number-1-num2);num2++;}
42     }
43     return sum;
44 
45 }
46 
47 void main()
48 {
49  char str1[]="??234??";
50  char str2[]="2323478";
51  int length1=sizeof(str1)-1;
52  int length2=sizeof(str2)-1;
53  if(length1!=length2) throw exception("Invalid input.");//拋出異常輸入錯誤
54  cout<<length1<<length2<<endl;
55  cout<<getcount(str1,str2,length1)<<endl;
56 }
相關文章
相關標籤/搜索