POJ 3748:位操作
来源:互联网 发布:java byte base64 编辑:程序博客网 时间:2024/05/20 02:56
位操作
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8964 Accepted: 3581
Description
假设你工作在一个32位的机器上,你需要将某一个外设寄存器的第X位设置成0(最低位为第0位,最高位为第31位),将第Y位开始的连续三位设置成110(从高位到低位的顺序),而其他位保持不变。对给定的寄存器值R,及X,Y,编程计算更改后的寄存器值R。
Input
仅一行,包括R,X,Y,以逗号","分隔,R为16进制表示的32位整数,X,Y在0-31之间且Y>=3,(Y-X)的绝对值>=3,保证两次置位不会重合
Output
更改后的寄存器值R(16进制输出)
Sample Input
12345678,0,3
Sample Output
1234567c
做完这个题目AC的时候,再看其他人discuss的时候,哭的心都有了。自己居然模拟做。。。各种WA,RE都搞出来了。。。受不了了,代码各种幼稚我也不优化了。。。
代码:
#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <cstring>using namespace std;int wei[10000];void change_to(char R[10000]){int i,j;for(i=7;i>=0;i--){int temp=R[i];for(j=0;j<=3;j++){wei[(7-i)*4+j]=temp&1;temp=temp>>1;}}}void change(int x,int y){if(x>=0&&x<=31)wei[x]=0;if(y>=2){wei[y]=1;wei[y-1]=1;wei[y-2]=0;}else if(y==1){wei[y]=1;wei[y-1]=1;}else wei[y]=1;}void solve(){char temp;int i;int flag=1;for(i=31;i>=3;i=i-4){int sum= wei[i]*8 + wei[i-1]*4 + wei[i-2]*2 + wei[i-3];if(sum<=9 && sum>=0){temp=sum+'0';}else{temp=sum+87;}if(temp=='0'&&flag==1){}else{flag=0;cout<<temp;}}}int main(){char R[10000],temp_R[10000];char temp;int x,y,i=0,j,k;memset(R,0,sizeof(R));while(scanf("%c",&temp_R[i]),temp_R[i++]!=',');i--;j = 8-i;k = 0;for(i=j;i<8;i++){R[i] = temp_R[k++];}for(x=0;x<8;x++){if(R[x]>='0'&&R[x]<='9')R[x]=R[x]-'0';else if(R[x]>=97)R[x]=R[x]-'a'+10;}scanf("%d,%d",&x,&y);change_to(R);change(x,y);solve();cout<<endl;return 0;}
代码:
#include <iostream>#include <cstdio>using namespace std;int main() {unsigned int r, x, y;while(scanf_s("%x,%d,%d", &r, &x, &y) == 3) {r = r & (~(1<<x));//将r的x位置为0r = r | (1<<y);//将r的y位置为1r = r | (1<<(y-1));r = r & (~(1<<y-2));printf("%x\n", r);}return 0;}
0 0
- poj 3748 位操作
- poj 3748 位操作
- poj 3748位操作
- POJ 3748 位操作
- POJ 3748:位操作
- POJ-3748 位操作
- POJ 3748 位操作(位运算)
- POJ 3748 位操作 C++
- poj 2443 Set Operation (位操作)
- 3748 位操作
- poj 3748 水题位操作
- 位操作
- 位操作
- 位操作
- 位操作
- 位操作
- 位操作
- 位操作
- 黑马程序员--IOS基础第十九天(OC)
- WPF 中双向绑定通知机制之ObservableCollection使用
- 正确使用 Volatile 变量
- protected
- linux 进程理解
- POJ 3748:位操作
- 关于 C变量的存储方式
- 006.Char 字符型
- 小胖说事28------iOS中extern,static和const区别和用法
- 黑马程序员--IOS基础第二十天(OC)
- Unity3D开发:Unity3D中的脚本概述
- Java-线程间通信
- Android SwipeRefreshLayout 官方下拉刷新控件介绍{v4包中带的}
- 使用butterknife注解工程配置