2011大纽约程序设计竞赛,D题 Decoding EDSAC Data(题目链接)。

D ⋅ Decoding EDSAC Data

The world's first full-scale, stored-program, electronic, digital computer was the EDSAC (Electronic Delay Storage Automatic Calculator). The EDSAC had an accumulator-based instruction set, operating on 17-bit words(and 35-bit double words), and used a 5-bit teletypewriter code for input and output.

The EDSAC was programmed using a very simple assembly language: a single letter opcode followed by an unsigned decimal address, followed by the the letter 'F' (for full word) or 'D' (for double word). For example, the instruction "A 128 F" would mean "add the full word at location 128 to the accumulator", and would be assembled into the 17-bit binary value, 11100000100000000, consisting of a 5-bit opcode (11100="add"), an 11-bit operand (00010000000 = 128), and a single 0 bit denoting a full word operation (a 1 bit would indicate a double word operation).

Although arithmetic on the EDSAC was fixed point two's complement binary, it was not mere intger arithmetic (as is common with modern machines). The EDSAC hardware assumed a binary point between the leftmost bit and its immediate successor. Thus the hardware could handle only values in the range -1.0 ≤ x < 1.0. For example:

Value Binary Representation
-1.0 10000000000000000
½ 01000000000000000
¾ 01100000000000000
11000000000000000

As you can see, the largest possible positive value was:

01111111111111111 = 0.9999847412109375

and the smallest possible positive value was:

00000000000000001 = 2-16 = 0.0000152587890625

(This also happens to be the increment between successive values on the EDSAC).

By a curious coincidence(or an elegant design decision), the opcode for the add operation(11100) was the same as the teleprinter code for the letter 'A'. The opcode for subtract was the same as the teleprinter code for 'S'(01100), and so on. This simplified the programming for the assembler (which, incidentally, was a mere 31 instructions long). The EDSAC teleprinter alphabet was "PQWERTYUIOJ#SZK*?F@D!HNM&LXGABCV" (with 'P'=00000, 'Q'=00001, and so on, up to 'V'=11111)

Unfortunately, the EDSAC assembler had no special directives for data values. On the other hand, there was no reason that ordinary instructions couldn't be used for this, thus, an EDSAC programmer desiring to reserve space for the constant ¾ (represented as 01100000000000000) would use the instruction "S O F" and for ⅓ (which is approximately represented as 00101010101010101) "T 682 D", and so on.

Your job is to write a program that will translate EDSAC instructions into the appropriate decimal fractions.

Input

The first line of input contains a single integer P ( 1 ≤ P ≤ 1000 ) which is the number of data sets that follow. Each data set is a single line that contains N (the dataset number), followed by a space, followed by an EDSAC instruction of the form: c□d□s, where c is a single character in the EDSAC alphabet, d is an unsigned decimal number (0 ≤ d < 211), and s is either a 'D' or 'F'. Note: □ represents a single space.

Output

For each data set there is one line of output. It contains the data set number (N) followed by a single space, followed by the exact decimal fraction represented by the by the EDSAC instruction, including a minus sign (for negative values). The format for the decimal fraction is: sb.ddd..., where s is an optional minus sign, b is either a 1 or 0, and d is any decimal digit (0-9). There must be at least 1 and at most 16 digits after the decimal point. Trailing zeros in the fraction must be suppressed.

Sample Input Sample Output
13
1 P 0 F
2 I 0 F
3 & 0 F
4 ? 0 F
5 Q 1228 D
6 P 0 D
7 V 2047 D
8 * 2047 D
9 ? 0 D
10 P 256 F
11 V 1536 F
12 T 682 D
13 T 54 F
1 0.0
2 0.5
3 -0.5
4 -1.0
5 0.0999908447265625
6 0.0000152587890625
7 -0.0000152587890625
8 0.9999847412109375
9 -0.9999847412109375
10 0.0078125
11 -0.015625
12 0.3333282470703125
13 0.31414794921875

  这一题,我是直接用机器中的二进制来做的,一位一位地判断。前五位(细分成第一位,接下来的4位),中间11位,后一位,分开来处理。另外,正负也分别处理。

  C语言源代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
const char * code = "PQWERTYUIOJ#SZK*?F@D!HNM&LXGABCV";
int main (void)
{
int testcases;
int testcase;
char firstpart;
int secondpart;
char thirdpart;
int i;
unsigned short number;
long long result;
int sign;
long long current;
char final[100];;
scanf( "%d", &testcases );
while ( testcases -- )
{
scanf( "%d %c %d %c", &testcase, &firstpart, &secondpart, &thirdpart );
for ( i = 0; code[i] != '\0' ; i ++ )
{
if ( code[i] == firstpart )
break;
}
assert( code[i] != '\0' );
if ( i >= 0x10 )
{
number = i - 0x10 ;
sign = -1;
}
else
{
number = i;
sign = 1;
}
number <<= 12;
number += (secondpart << 1);
if ( thirdpart == 'D' )
number ++;
result = 0;
current = 152587890625;
if ( sign > 0 )
{
for ( i = 1 ; i <= 16 ; i ++ )
{
if ( (number & 1 ) == 1 )
{
result += current;
}
number >>= 1;
current <<= 1;
}
#ifdef _WIN32
sprintf( final, "0.%016I64d", result );
#else
sprintf( final, "0.%016lld", result );
#endif
}
else
{
number --;
for ( i = 1 ; i <= 16 ; i ++ )
{
if ( (number & 1 ) == 0 )
{
result += current;
}
number >>= 1;
current <<= 1;
}
#ifdef _WIN32
sprintf( final, "-0.%016I64d", result );
#else
sprintf( final, "-0.%016lld", result );
#endif
}
if ( !strcmp(final, "-0.0000000000000000" ) )
strcpy( final, "-1.0" );
else
{
if ( sign > 0 )
{
for ( i = 17; i > 2 ; i -- )
{
if ( final[i] == '0' )
final[i] = '\0';
else
break;
}
}
else
{
for ( i = 18; i > 3 ; i -- )
{
if ( final[i] == '0' )
final[i] = '\0';
else
break;
}
}
}
printf( "%d %s\n", testcase, final );
}
return EXIT_SUCCESS;
}

2011大纽约区域赛试题 Decoding EDSAC Data 解题报告相关推荐

  1. 【2020大数据应用赛试题】Spark分析处理

    文章目录 2020大数据应用赛试题 任务一.Spark技术栈有哪些组件?简述其功能,及应用场景. 任务二.本题目使用spark进行数据分析 数据说明 题目 题目一 题目二 题目三 题目四 2020大数 ...

  2. 武汉工程大学第一届程序设计女生赛(牛客contest 4746)解题报告 Apare_xzc

    武汉工程大学第一届程序设计女生赛解题报告 xzc 2020.3.8 比赛链接:武汉工程大学第一届程序设计女生赛 A. Multiplication (101/861) 分析: 问x平方几次后就会> ...

  3. [CF/AT]各大网站网赛 体验部部长第一季度工作报告

    文章目录 CodeForces #712 (Div. 1)--1503 A. Balance the Bits B. 3-Coloring C. Travelling Salesman Problem ...

  4. HDOJ 4239 - Decoding EDSAC Data 模拟

    题意: 给了一系列的用操作数转换为17位二进制数的关系..又给出了17位二进制数转化成十进制小数的关系...现在给出操作数..请退出其对应十进制小数. 题解: 这个不恶心..简单题...控制小数位.. ...

  5. 22行代码AC_试题 历届试题 油漆面积【解题报告】

    励志用更少的代码做更高效的表达 X星球的一批考古机器人正在一片废墟上考古. 该区域的地面坚硬如石.平整如镜. 管理人员为方便,建立了标准的直角坐标系. 每个机器人都各有特长.身怀绝技.它们感兴趣的内容 ...

  6. hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一 最详细的解题报告

    题目来源:小Hi小Ho的惊天大作战:扫雷·一 解题思路:因为只要确定了第一个是否有地雷就可以推算出后面是否有地雷(要么为0,要么为1,如果不是这两个值就说明这个方案行不通),如果两种可能中有一种成功, ...

  7. ACM/ICPC2016沈阳网络赛(不完全)解题报告

    比赛地址: http://acm.hdu.edu.cn/contests/contest_show.php?cid=724 1003.hannnnah_j's Biological Test 题目大意 ...

  8. 2020蓝桥杯国赛Java大学B组解题报告

    文章目录 试题 A: 美丽的 2 试题 B: 扩散 试题 C: 阶乘约数 试题 D: 本质上升序列 试题 E: 玩具蛇 试题 F: 蓝肽子序列 试题 H: 画廊 试题 A: 美丽的 2 问题描述 小蓝 ...

  9. 【WZOI第二次NOIP模拟赛Day1T2】世界末日 解题报告

    [WZOI第二次NOIP模拟赛Day1T2]世界末日 Problem 2 世界末日 (doomsday.pas/c/cpp) 背景 话说CWQ大牛终于打开了那扇神秘大门,但迎接他的不是什么神秘的东西, ...

最新文章

  1. mysql导入csv文件
  2. java使用类似ini文件IniProperties的类
  3. 【ijkplayer】编译 Android 版本的 ijkplayer ⑤ ( 执行 init-android-libyuv.sh | 执行 init-android-soundtouch.sh )
  4. .net mvc 报表_Web在线报表设计器使用指南
  5. 怎么查看端口占用情况?
  6. SQL中的left join与right join
  7. 大数据平台应用 17 个知识点汇总
  8. python爬虫 单线程的多任务异步协程
  9. php 将表情存入数据库,php + mysql 存入表情 【如何轉義emoji表情,讓它可以存入utf8的數據庫】...
  10. linux pcie组raid_大概是市面上带金属 PCIE 装甲和背板中最便宜的一款主板。华擎 Z390 Phantom Gaming X 开箱评测...
  11. fastjson map转json_Java对象转JSON咋这么头疼?不!那是你还没使用Fastjson
  12. 超文本标记语言HTML
  13. Django中应用celery
  14. linux之shell快速入门系列<8> | shell工具cut、sed、awk、sort
  15. 计算机网络(第七版)谢希仁知识点总结
  16. 金山词霸2003/2005/2006词典丢失的解决方法
  17. 用cmd打开jar文件
  18. 《关于雪糕刺客与雪糕护卫激发中国人的创作灵感这件事》
  19. 新计算机的word无法输入文字,【WORD为什么无法输入文字?】word如何转换excel
  20. 微服务生态组件之Spring Cloud LoadBalancer详解和源码分析

热门文章

  1. CentOS6 64位系统安装步骤
  2. ucos 和uclinux的区别及各自的特点
  3. 湖南省衡阳市谷歌高清卫星地图下载
  4. 第二人生的源码分析(102)脚本的构造
  5. python 可视化 ploty 画3dmesh网格图
  6. 【产业互联网周报】外媒:英特尔等公司暂停向俄罗斯发货;阿里云季度营收195亿元;第四范式再次提交上市申请...
  7. Vulkan_Shader_Day06—光照(多光源_Multiple lights)
  8. 计算机主机故障有哪些,电脑主机电源常见的问题与解决方法_电脑故障
  9. 揭秘中国商品期货市场的9大重要因子
  10. swift 判断是否设置了代理