[蓝桥杯]ALGO-186.算法训练_P0501

  输入两个无符号整数x, y, 用位操作实现无符号整数的乘法运算。不用考虑整数的溢出。
输入:
  235 657
输出:
  154395
题目描述

代码如下:

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main(void)
 6 {
 7     int i,res=0;
 8     unsigned int x,y;
 9     scanf("%d%d",&x,&y);
10     for (i=0 ; i<32 ; i++)    //32位长度为满足测试数据,大小可根据实际修改 
11     {
12         if ((y&1) == 1)    //y的最低位是否为1 
13         {
14             res += x;    //计算相乘的结果 
15         }
16         x <<= 1;
17         y >>= 1;     
18     }
19     printf("%d",res);
20     return 0;
21 }
C解法

解题思路:

位操作相乘参考:https://blog.csdn.net/luolan9611/article/details/81772481

猜你喜欢

转载自www.cnblogs.com/mind000761/p/10344494.html
今日推荐