問題描述
在計算機中,對于定點數(shù)有三種不同的表示方法晌端。在本題中,假定碼的長度固定為8位恬砂,從左往右依次編號為第1到8位咧纠,第1位為最高位。
的原碼:最高位為符號位泻骤,正數(shù)符號位為0漆羔,負數(shù)符號位為1梧奢,第2到7位為
的二進制表示。正負0的原碼不同演痒。
的反碼:原碼符號位除外亲轨,其他位按位取反,即1變0鸟顺,0變1惦蚊。
的補碼:正數(shù)的補碼等于原碼,負數(shù)的補碼等于反碼 + 1讯嫂,因此正負0的補碼相同蹦锋。
給定整數(shù),請給出它的原碼欧芽、反碼和補碼莉掂。
輸入
第一行包含一個正整數(shù),表示測試數(shù)據(jù)的組數(shù)千扔。
每組測試數(shù)據(jù)包含一行憎妙,首先是一個符號+
或-
,表示的正負曲楚,然后是一個非負整數(shù)
厘唾,表示
的絕對值為
。
輸出
對于每組數(shù)據(jù)洞渤,輸出三行阅嘶,第一行為原碼属瓣,第二行為反碼载迄,第三行為補碼。
樣例輸入
4
+0
+1
-0
-3
樣例輸出
00000000
01111111
00000000
00000001
01111110
00000001
10000000
11111111
00000000
10000011
11111100
11111101
解題思路
沒啥好說的抡蛙,一個模擬題护昧。
先輸入:
char c;int n;
scanf(" %c%d",&c,&n);
值得注意的是,由于第一個是符號粗截,所以在%c
前務必加上空格惋耙,用于吸收輸入中的回車。
那我們就開始硬核模擬吧熊昌。
我們先定義一個string
绽榛,用于記錄。
然后直接搞????
string str;
while (n != 0)
{
str = (char) (n % 2 + '0') + str;
n /= 2;
}
搞完了婿屹,那就根據(jù)題意灭美,模擬就行了。
正數(shù)比較簡單:
cout << '0' + str << endl;
string f;
for (int i = 0;i < 7;i ++) f += (char) ('0' + 1 - str[i] + '0');
cout << '0' + f << endl;
cout << '0' + str << endl;
負數(shù)的話昂利,補碼稍微注意下就好了届腐。
整體就是一個大數(shù)的思想铁坎。
cout << '1' + str << endl; // 先輸出原碼
string f;
for (int i = 0;i < 7;i ++) f += (char) ('0' + 1 - str[i] + '0');
cout << (f = '1' + f) << endl; // 反碼,同時也把符號位加上犁苏,方便之后的補碼計算
int j = 1; // 進位硬萍,由于補嗎要加1,所以從個位開始围详,就有進位了
int i = 7; // 設置index到個位
while (j) // 只要j還有進位朴乖,那就一直加
{
if (i == -1) { // 特殊判斷-0,這個時候就不要在往前搞了助赞,直接跳出
f[0] = '0';
break;
}
f[i] ++; // '0' + 1 = '1'; '1' + 1 = '2';
j --; // 進位剪掉
if (f[i] == '2') { // 溢出來了
j ++; // 進位
f[i] = '0'; // 置0
}
i --; // index前移
}
cout << f << endl; // 輸出
完整代碼
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <list>
#include <set>
#include <utility> // pair
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm> // sort
#include <string>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
#define ll long long
#define uchar unsigned char
#define ushort unsigned short
#define uint unsigned int
#define ulong unsigned long
#define ull unsigned long long
#define pi acos(-1)
#define mx(a,b) (a) > (b) ? (a) : (b)
#define mn(a,b) (a) < (b) ? (a) : (b)
#define mem(a,b) memset(a,b,sizeof(a))
#define fre(a) freopen(a,"r",stdin)
#define itn int
#define nit int
#define inr int
#define mian main
#define ednl endl
#define fro for
#define fir for
#define reutrn return
#define retunr return
int main()
{
int T;
scanf("%d",&T);
while (T --)
{
char c;int n;
scanf(" %c%d",&c,&n);
string str;
while (n != 0)
{
str = (char) (n % 2 + '0') + str;
n /= 2;
}
while (str.size() != 7) str = '0' + str;
if (c == '+')
{
cout << '0' + str << endl;
string f;
for (int i = 0;i < 7;i ++) f += (char) ('0' + 1 - str[i] + '0');
cout << '0' + f << endl;
cout << '0' + str << endl;
} else {
cout << '1' + str << endl; // 先輸出原碼
string f;
for (int i = 0;i < 7;i ++) f += (char) ('0' + 1 - str[i] + '0');
cout << (f = '1' + f) << endl; // 反碼寒砖,同時也把符號位加上,方便之后的補碼計算
int j = 1; // 進位嫉拐,由于補嗎要加1哩都,所以從個位開始,就有進位了
int i = 7; // 設置index到個位
while (j) // 只要j還有進位婉徘,那就一直加
{
if (i == -1) { // 特殊判斷-0漠嵌,這個時候就不要在往前搞了,直接跳出
f[0] = '0';
break;
}
f[i] ++; // '0' + 1 = '1'; '1' + 1 = '2';
j --; // 進位剪掉
if (f[i] == '2') { // 溢出來了
j ++; // 進位
f[i] = '0'; // 置0
}
i --; // index前移
}
cout << f << endl; // 輸出
}
}
return 0;
}