tickler blog

 | 

2009-11-12

Russian乗算アルゴリズム 20:26

Russian乗算プログラムとは - tickler blog

#include <stdio.h>

main()
{
    int a, b, x, y, z=0, ans;

    printf("x? ");
    scanf("%d",&x);
    printf("y? ");
    scanf("%d",&y);

    a = x; b = y;

    do
    {
        ans = x%2;
        if(ans == 1)
            z = z + y;

        x = x/2;
        y = y*2;
    } while (x != 0);


    printf("%d * %d = %d \n",a , b, z);

        return 0;
}
 |