教你夺冠


私信TA

用户名:835685327

访问量:148702

签 名:

相互交流 相互学习

等  级
排  名 13
经  验 21616
参赛次数 0
文章发表 84
年  龄 0
在职情况 学生
学  校 辣鸡施工大学
专  业

  自我简介:

努力刷题 熟能生巧!

花了很长时间才写出来,其实不难,注意数组的位置。

参考代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
	int n;
	unsigned int a;
	
	scanf("%u %d", &a, &n);
	
	//将a用32位的二进制模式表示的各位数存放在一个数组中
	int bits[32] = { 0 };
	int bitsindex = 31;
	int i = 32;
	while (i > 0)
	{
		bits[bitsindex--] = a % 2;
		a = a / 2;
		i--;
	}
	
	/*
	for (i = 0; i < 32; i++)
	{
		printf("%d", bits[i]);
	}
	printf("\n");
	*/

	//用一个临时数组存放要移位的数,并将原数组中的对应位置置为0
	int tmp[n]; 
	for (i = 0; i < n; i++)
	{
		tmp[i] = bits[31 - n + 1 + i];
		bits[31 - n + 1 + i] = 0;
	}
	//然后将这个临时数组赋给原数组的前n位
	for (i = 0; i < n; i++)
	{
		bits[i] = tmp[i];
	}
	
	unsigned int res = 0;
	for (i = 31; i >= 0; i--)
	{
		res += bits[i] * pow(2, 31 - i);
	}
	printf("%u\n", res);
	
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区