POJ 1496
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int fac(int num); 6 7 int C(int n,int m); 8 int com( int n, int k ); 9 int main() 10 { 11 //freopen("acm.acm","r",stdin); 12 string s; 13 int sum; 14 int j; 15 int i; 16 // cout<<com(4,2)<<endl; 17 18 while(cin>>s) 19 { 20 sum = 0; 21 for(i = 0; i < s.length()-1; ++ i) 22 { 23 sum += com(26,i+1); 24 } 25 // cout<<s<<endl; 26 // cout<<"sum "<<sum<<endl; 27 28 //cout<<"len "<<s.length()<<endl; 29 for(i = 0; i < s.length(); ++ i) 30 { 31 for(j = i+1; j < s.length(); ++ j) 32 { 33 if(s[i] >= s[j]) 34 { 35 break; 36 } 37 } 38 if(j != s.length()) 39 { 40 break; 41 } 42 } 43 if(i != s.length()) 44 { 45 cout<<0<<endl; 46 continue; 47 } 48 for(i = 0; i < s.length(); ++ i) 49 { 50 if(i == 0) 51 { 52 j = 0; 53 } 54 else 55 { 56 j = s[i-1]-'a'+1; 57 } 58 for(; j < s[i]-'a'; ++ j) 59 { 60 sum += com(26-j-1,s.length()-i-1); 61 } 62 } 63 cout<<sum+1<<endl; 64 65 } 66 } 67 68 int C(int n,int m) 69 { 70 return fac(n)/(fac(m)*fac(n-m)); 71 } 72 73 int fac(int num) 74 { 75 int i; 76 int sum = 1; 77 for(i = 1; i <= num; ++ i) 78 { 79 sum *= i; 80 } 81 return sum; 82 } 83 84 int com( int n, int k ) 85 { 86 87 if ( k>0 ) 88 { 89 return com(n-1, k-1 ) * n / k;//组合公式 90 } 91 return 1; 92 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com