1 条题解

  • 0
    @ 2025-4-10 5:24:23

    第七题:

    2572:Hard to Believe, but True!

    题目链接:

    P2572——Hard to Believe, but True!——柒行(www.oj7.cn)

    题目来源:

    Ulm Local 2001

    题目描述

    总时间限制:

    1000ms

    内存限制:

    65536kB

    Description

    The fight goes on, whether to store numbers starting with their most significant digit or their least significant digit. Sometimes this is also called the "Endian War". The battleground dates far back into the early days of computer science. Joe Stoy, in his (by the way excellent) book "Denotational Semantics", tells following story: "The decision which way round the digits run is, of course, mathematically trivial. Indeed, one early British computer had numbers running from right to left (because the spot on an oscilloscope tube runs from left to right, but in serial logic the least significant digits are dealt with first). Turing used to mystify audiences at public lectures when, quite by accident, he would slip into this mode even for decimal arithmetic, and write things like 73+42=16. The next version of the machine was made more conventional simply by crossing the x-deflection wires: this, however, worried the engineers, whose waveforms were all backwards. That problem was in turn solved by providing a little window so that the engineers (who tended to be behind the computer anyway) could view the oscilloscope screen from the back. [C. Strachey - private communication.]"

    You will play the role of the audience and judge on the truth value of Turing's equations.

    输入

    The input contains several test cases. Each specifies on a single line a Turing equation. A Turing equation has the form "a+b=c", where a, b, c are numbers made up of the digits 0,...,9. Each number will consist of at most 7 digits. This includes possible leading or trailing zeros. The equation "0+0=0" will finish the input and has to be processed, too. The equations will not contain any spaces.

    输出

    For each test case generate a line containing the word "True" or the word "False", if the equation is true or false, respectively, in Turing's interpretation, i.e. the numbers being read backwards.

    输入数据

    73+42=16
    5+8=13
    10+20=30
    0001000+000200=00030
    1234+5=1239
    1+0=0
    7000+8000=51
    0+0=0
    

    输出数据

    True
    False
    True
    True
    False
    False
    True
    True
    

    中文题面:

    描述

    这场争论仍在继续,究竟是应该从最高有效位开始存储数字,还是从最低有效位开始。有时,这也被称为“字节序大战”。这场争论可以追溯到计算机科学的早期。乔·斯托伊(Joe Stoy)在他(顺便说一句,非常棒的)书《指称语义学》(Denotational Semantics)中讲述了以下故事: “数字排列方式的决定,当然,在数学上是微不足道的。事实上,有一台早期的英国计算机是从右到左处理数字的(因为示波器上的点是从左向右移动的,但在串行逻辑中,最不重要的数字会先被处理)。图灵过去在公开演讲时,有时会不小心进入这种模式,即使是进行十进制运算,也会写出像 73+42=1673+42=16这样的东西。下一版本的机器通过交叉x偏转线变得更加传统;然而,这让工程师们感到担忧,因为他们的波形都是反的。这个问题随后通过提供一个小窗口得到了解决,这样工程师们(他们通常都会站在计算机后面)就可以从后面查看示波器屏幕了。 [C. Strachey - 私人通信]” 你将扮演听众的角色,并判断图灵方程的真假值。 输入 输入包含多个测试用例。每个测试用例都在一行中指定一个图灵方程。图灵方程的形式为 a+b=ca+b=c ,其中abca、b、c是由数字090-9组成的数字。每个数字最多由77位组成,包括可能的前导零或尾随零。方程0+0=00+0=0将结束输入,并且也必须被处理。这些方程不会包含任何空格。 输出 对于每个测试用例,如果方程在图灵的解释下(即数字被倒序读取)为真或假,则生成一行包含单词“TrueTrue”或“FalseFalse”。

    输入

    输入包含多个测试用例。每个测试用例在单行上指定一个图灵方程。图灵方程的形式为“a+b=ca+b=c”,其中abca、b、c是由数字0099组成的数字。每个数字最多由77位组成。这包括可能的前导零或尾随零。方程“0+0=00+0=0”将结束输入,并且也必须被处理。方程中不包含任何空格。

    输出

    对于每个测试用例,生成一行,包含单词“TrueTrue”或单词“FalseFalse”,如果方程在图灵的解释下(即数字被倒序读取)为真或假,则分别输出这两个单词。

    算法标签

    字符串

    解题思路:

    读取多个输入,每个测试用例是一个图灵方程。 利用strtok函数将字符串分割:将每个方程按照“++”和“==”进行分割,提取出aba、bcc三个部分并分别反转,得到它们在图灵解释下的值,检查反转后的aabb之和是否等于反转后的cc

    实现步骤:

    输入读取:

    读取图灵方程,将每组图灵方程存储到字符串数组里,利用strcmp函数使读取的图灵方程与0+0=00+0=0比较,若完全相等(值为00)则停止输入

    字符串分割:

    利用strtok函数将每个方程按照“++”和“==”进行分割,提取出aba、bcc三个部分。

    数字反转:

    编写reversed函数,实现aba、bcc的反转,得到它们在图灵解释下的值。

    字符串转化数字:

    利用atoi函数将字符串转化为数字。

    条件判断:

    检查反转后的aabb之和是否等于反转后的cc

    输出结果:

    根据判断结果输出“TrueTrue”或“FalseFalse”。

    C++实现:

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    void reversed(char *str){
        int len=strlen(str);
        for(int i=0; i<len/2; i++){
            char temp=str[i];
            str[i]=str[len-i-1];
            str[len-i-1]=temp;
        }
    }
    int main(){
        char num[20];
        while(1){
            cin>>num;
            if(strcmp(num,"0+0=0")==0){
                cout<<"True"<<endl;
                break;
            }
            char *a=strtok(num,"+=");
            char *b=strtok(NULL,"+=");
            char *c=strtok(NULL,"+=");
            reversed(a);
            reversed(b);
            reversed(c);
            int numa=atoi(a);
            int numb=atoi(b);
            int numc=atoi(c);
            if(numa+numb==numc){
            	cout<<"True"<<endl;
            }else{
            	cout<<"False"<<endl;
            }
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    1573
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者