美文网首页
二进制码 - 第四届全国中医药院校大学生程序设计竞赛

二进制码 - 第四届全国中医药院校大学生程序设计竞赛

作者: Void_Caller | 来源:发表于2020-02-19 23:45 被阅读0次

问题描述

在计算机中,对于定点数有三种不同的表示方法。在本题中,假定码的长度固定为8位,从左往右依次编号为第1到8位,第1位为最高位。

x原码最高位为符号位,正数符号位为0,负数符号位为1,第2到7位为x的二进制表示。正负0的原码不同。

x反码:原码符号位除外,其他位按位取反,即1变0,0变1。

x补码正数补码等于原码负数补码等于反码 + 1,因此正负0的补码相同。

给定整数x,请给出它的原码、反码和补码。

输入

第一行包含一个正整数T(1 <= T <= 300),表示测试数据的组数。

每组测试数据包含一行,首先是一个符号+-,表示x的正负,然后是一个非负整数y(0 <= y <= 100),表示x的绝对值为y

输出

对于每组数据,输出行,第一行为原码,第二行为反码,第三行为补码

样例输入

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;
}

搞完了,那就根据题意,模拟就行了。

正数比较简单:

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;

负数的话,补码稍微注意下就好了。

整体就是一个大数的思想。

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;
}

相关文章

网友评论

      本文标题:二进制码 - 第四届全国中医药院校大学生程序设计竞赛

      本文链接:https://www.haomeiwen.com/subject/uwzzfhtx.html