HDU_4239_Decoding EDSAC Data(模拟)
2013-07-24 09:42
176 查看
Decoding EDSAC Data
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
The world's first full-scale, stored-program, electronic, digital computer was the EDSAC (Electronic Delay Storage AutomaticCalculator).
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 integer 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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
x <
1.0. For example:
As you can see, the largest possible positive value was:
[align=CENTER]01111111111111111 = 0.9999847412109375[/align]
and the smallest possible positive value was:
[align=CENTER]00000000000000001 = 2-16 = 0.0000152587890625[/align]
(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 3/4 (represented as 01100000000000000) would use the instruction `S 0 F' and for 1/3 (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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
P
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
d
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
s',
where c is a single character in the EDSAC alphabet, d is an unsigned decimal number ( 0
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
d <
211), and s is either a `D' or `F'.
Note:
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
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
题型:模拟
题意:与hdu4238的输入输出相反,将用符号表示的二进制数转变为十进制。
分析:
首先将输入那些符号根据题意变为二进制,共有17位,其中第一位为符号位,用于判断正负,其余的按照二进制求十进制小数部分的方法来求就行了。
代码:
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
The world's first full-scale, stored-program, electronic, digital computer was the EDSAC (Electronic Delay Storage AutomaticCalculator).
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 integer 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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
x <
1.0. For example:
Value | Binary Representation |
-1.0 | 10000000000000000 |
1/2 | 01000000000000000 |
3/4 | 01100000000000000 |
-1/2 | 11000000000000000 |
[align=CENTER]01111111111111111 = 0.9999847412109375[/align]
and the smallest possible positive value was:
[align=CENTER]00000000000000001 = 2-16 = 0.0000152587890625[/align]
(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 3/4 (represented as 01100000000000000) would use the instruction `S 0 F' and for 1/3 (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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
P
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
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
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
d
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
s',
where c is a single character in the EDSAC alphabet, d is an unsigned decimal number ( 0
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img1.png)
d <
211), and s is either a `D' or `F'.
Note:
![](https://icpcarchive.ecs.baylor.edu/external/58/5803img2.png)
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
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
Sample Output
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
题型:模拟
题意:与hdu4238的输入输出相反,将用符号表示的二进制数转变为十进制。
分析:
首先将输入那些符号根据题意变为二进制,共有17位,其中第一位为符号位,用于判断正负,其余的按照二进制求十进制小数部分的方法来求就行了。
代码:
#include<stdio.h> #include<string.h> char map[33] = {'P' , 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , 'O' , 'J' , '#' , 'S' , 'Z' , 'K' , '*' , '?' , 'F' , '@' , 'D' , '!' , 'H' , 'N' , 'M' , '&' , 'L' , 'X' , 'G' , 'A','B','C','V'}; char a[3] , b[3]; int n , num; double ans; char tans[10000]; char str[30]; void f(char t){ int d = 4; for(int i = 0 ; i < 32 ; i ++){ if(t == map[i]){ while(i != 0){ str[d --] = i % 2 + '0'; i /= 2; } break; } } while(d >= 0){ str[d --] = '0'; } return ; } void ff(int t){ int d = 15; while(t != 0){ str[d --] = t % 2 + '0'; t /= 2; } while(d >= 5){ str[d --] = '0'; } return ; } void fff(char t){ if(t == 'F') str[16] = '0'; if(t == 'D') str[16] = '1'; } int main(){ scanf("%d" , &n); for(int i = 1 ; i <= n ; i ++){ ans = 0; double c = 0.5; scanf("%*d%s%d%s" , a , &num , b); f(a[0]); // ff(num); fff(b[0]); str[17] = '\0'; //printf("%s\n" , str); printf("%d " , i); if(str[0] == '0'){ for(int i = 1 ; i < 17 ; i ++){ ans += (str[i] - '0') * c; c /= 2; } sprintf(tans , "%.16lf" , ans); } else{ for(int i = 1 ; i < 17 ; i ++){ ans += (str[i] - '0') * c; c /= 2; //printf("ans = %lf\n" , ans); } ans += -1.0; sprintf(tans , "%.16lf" , ans); } if(ans == 0.0){ printf("0.0\n"); } else if(ans == -1.0){ printf("-1.0\n"); } else{ int di; for(di = strlen(tans) - 1 ; di >= 0 ; di --){ if(tans[di] != '0') break; } tans[di + 1] = '\0'; //printf("di = %d\n" , di); printf("%s" , tans); printf("\n"); } /*if(str[0] == '0') sprintf(tans , "%.16lf" , ans); else sprintf(tans , "%.16lf" , -(1 - ans)); //for(int i = ) printf("%d " , i); if(ans != 0){ int di; for(di = strlen(tans) - 1 ; di >= 0 ; di --){ if(tans[di] != '0') break; } tans[di + 1] = '\0'; //printf("di = %d\n" , di); printf("%s" , tans); printf("\n"); } else if (ans == 0 && str[0] == '1'){ printf("-1.0\n"); } else{ printf("0.0\n"); }*/ } return 0; }
相关文章推荐
- HDOJ 4239 - Decoding EDSAC Data 模拟
- 2011大纽约区域赛试题 Decoding EDSAC Data 解题报告
- 2011大纽约区域赛试题 Decoding EDSAC Data 解题报告
- Data Handler 大模拟 + 双端链表 hdu 4268
- hdu 5929 Basic Data Structure(找规律,模拟)
- HDU 4286 Data Handler // 模拟链表
- HDU - 5929 Basic Data Structure(模拟+规律)
- Basic Data Structure HDU 5929(模拟)
- HDU 1987 Decoding (模拟)
- HDU 5929 Basic Data Structure(模拟,deque)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- hdu 6168 模拟+优先队列
- HDU 5414 CRB and String (字符串,模拟)
- HDU 4740 模拟题意
- HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)
- HDU 1053 Entropy 模拟哈夫曼树
- HDU 5965 枚举模拟 + dp(?)
- HDU 1020 Encoding 模拟
- POJ 3505 && HDU 2341 Tower Parking(简单模拟)
- HDU 2384 | POJ 2920 | Ranking 模拟
- hdu 5071 Chat(模拟)