Turn into a p-ary algorithm C language

Today did not even play the next not think of it,

It is actually very simple.

for example:

$64 = 2 \times 3^3 + 1 \times 3^2 + 3^0$

The proposed 3 per Horner's method, i.e. $ 3 (2 \ times 3 ^ 2 + 1 \ times 3 ^ 1) + 1 = 64 $,

Visible only need to get the lowest coefficient to take more than 3,

The n divided by 3, repeating the above steps.

#include <stdio.h> void Shift ( int n, int p)    // will be converted to the p-ary n {
     the while (n) 
    { 
        the printf ( " % D " , n% p);    // from low to high output 
        = n-n-/ P; 
    } 
} void SHIFT2 ( int n-, int P)   // forcibly written recursively, HH {
     IF (n-== 0 )   return ; 
    the printf ( " % D " , n-% P); 
    SHIFT2 (n- /





p, p);
}

int main()
{
    shift(64, 3);  //1 0 1 2
}

 

Guess you like

Origin www.cnblogs.com/lfri/p/11525608.html