百炼OJ 2972 2973

一、2972相邻数字的基数等比:确定进制

     所谓基数等比就是后一个数与前一个数有倍数的关系。如 111 = 1 + 1 * 2(1 + 2 * 1);

     给定 如 11 11 121 确定进制是3 和 10 ,只输出最小的进制,即3;

  

 1 /*            author :bakari
 2  *            2-16进制的计算
 3  *            特点:相邻数字的基数等比
 4  *            规律:x1x2x3(任意进制) ---> d1d2d3(十进制)(进制为p)
 5  *            x1*p(k-1) + x2*p(k-2) + x3*p(k-3) = p(k-3)(x3p + x1*p(2)) = p(k-3)(p(x3 + x1*p));
 6  */
 7 #include <iostream>
 8 #include <cstring>
 9 using namespace std;
10 
11 int const Max = 8;
12 
13 long RanToTen(const char *x ,const int& b)   //p代表进制
14 {
15     int nLen = strlen(x);
16     long ret = 0;
17     for (int i = 0; i != nLen; ++i){
18         if ((x[i] - '0') >= b) return -1;
19         ret *= b;
20         ret += x[i] - '0';
21 
22     }
23     return ret;
24 }
25 
26 int main()
27 {
28     char p[Max],q[Max],r[Max];
29     long pa , qa , ra;
30     int n ;
31     cin >> n;
32     while(n--){
33         int i;
34         cin >> p >> q >> r;
35         for (i = 2; i <= 16; ++i){
36             pa = RanToTen(p,i);
37             qa = RanToTen(q,i);
38             ra = RanToTen(r,i);
39             if (-1 == pa || -1 == qa || -1 == ra) continue;
40             if (pa * qa == ra){
41                 cout << i <<endl;
42                 break;
43             }
44         }
45         if (17 == i) cout<<"0"<<endl;
46     }
47     return 0;
48 }

二、2973相邻数字的基数不等比 :俗称skew数

      给定如 10120 转换后为 1 * (2^5 - 1)+ 0 * (2^4 - 1)+ 1 * (2^3 - 1)+ 2 * (2^2 - 1)+0 * (2^1 - 1)

      

 1 /*                author:bakari
 2  *                和一般进制转换不同,此进制基数不等比
 3  *                第k位的值Xk = Xk * (2^(k+1) - 1)
 4  *                令base[0] = 1;则:base[k] = 2^(k+1) - 1 = 2 * (2^k - 1) + 1 = 2 * base[k-1]+1;
 5  */
 6 
 7 #include <iostream>
 8 #include <cstring>
 9 using namespace std;
10 
11 const int MaxBit = 31;
12 int base[MaxBit];
13 
14 long int Transe(const char *x);
15 
16 int main()
17 {
18     char str[MaxBit];
19     base[0] = 1;
20     for (int i = 1; i != MaxBit; ++i)
21         base[i] = 2 * base[i-1] + 1;
22     while (1){
23         cin >> str;
24         if (Transe(str) == 0)
25             break;
26         else cout << Transe(str) <<endl;
27     }
28     return 0;
29 }
30 
31 long int Transe(const char *x)
32 {
33     int nLen = strlen(x);
34     long ret = 0;
35     if (strcmp(x,"0") == 0) return 0;
36     for (int i = 0,k = nLen - 1; i != nLen; ++i,--k){
37         ret += (x[i] -'0') * base[k];
38     }
39     return ret;    
40 }

个人总结:多角度思考问题

上一篇:jQuery_子元素过滤|学习笔记


下一篇:局部变量,静态局部变量,全局变量,静态全局变量在内存中的存放区别(转)