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 }