zoukankan      html  css  js  c++  java
  • poj 2572 Hard to Believe, but True!

    Hard to Believe, but True!
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3537   Accepted: 2024

    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.

    Input

    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.

    Output

    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.

    Sample Input

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

    Sample Output

    True
    False
    True
    True
    False
    False
    True
    True
    

    Source

    分析:

    思路比较简单

    自己的做法:

     1 #include<string>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 int main(){//7
     6     string s;
     7     int a[8],b[8],c[8];
     8     while(cin>>s){
     9         if(s=="0+0=0"){         //注意
    10            cout<<"True"<<endl;
    11            break;    
    12         }
    13         int i=0;
    14         int j=0;
    15         memset(a,0,sizeof(a));
    16         memset(b,0,sizeof(b));
    17         memset(c,0,sizeof(c));
    18         while(s[i]!='+'){
    19             a[j++]=s[i++]-'0';
    20         }
    21         j=0;
    22         i++;
    23         while(s[i]!='='){
    24             b[j++]=s[i++]-'0';
    25         }
    26         j=0;
    27         i++;
    28         while(s[i]){
    29             c[j++]=s[i++]-'0';
    30             //cout<<c[j-1]<<endl;
    31         }
    32         for(i=0;i<=7;i++){
    33             a[i+1]+=(a[i]+b[i])/10;
    34             a[i]=(a[i]+b[i])%10;
    35         }
    36         for(i=0;i<7;i++){
    37             if(a[i]!=c[i])
    38                break;
    39         }
    40         if(i==7)
    41         cout<<"True"<<endl;
    42         else
    43         cout<<"False"<<endl;
    44     }
    45     return 0;
    46 }

    网上的代码:

    学习点:

    1.string.find(char a):返回字符a在字符串中的位置(从0开始)

    2.string.substr(a,b):返回字符串从a开始的b个字符的字符子串

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int trans(string s) {
     5     int a=0;
     6     for (int i=s.length()-1;i>=0;i--) 
     7         a=a*10+s[i]-'0';
     8     return a;    
     9 }
    10 int main() {
    11     string s,s1,s2,s3;
    12     while (cin >> s) {
    13           if (s=="0+0=0") {
    14                           cout << "True" << endl;
    15                           break;
    16           }
    17           bool flag=true;
    18           int p1=s.find("+");
    19           int p2=s.find("=");
    20           s1=s.substr(0,p1);
    21           s2=s.substr(p1+1,p2-p1-1);
    22           s3=s.substr(p2+1,s.length()-1-p2);
    23           if (trans(s1)+trans(s2)!=trans(s3)) flag=false;
    24           if (flag) cout << "True" << endl;
    25           else cout << "False" << endl;
    26     }27     return 0;
    28 }
  • 相关阅读:
    设计模式之美学习-接口隔离原则(七)
    设计模式之美学习-里式替换原则(六)
    设计模式之美学习-开闭原则(五)
    设计模式之美学习-设计原则之单一职责(四)
    设计模式之美学习-如何进行面向对象设计(三)
    ffmpeg 从内存中读取数据(或将数据输出到内存)
    CImage 对话框初始化时候显示透明 PNG
    RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)
    RTMPdump(libRTMP) 源代码分析 8: 发送消息(Message)
    RTMPdump(libRTMP) 源代码分析 7: 建立一个流媒体连接 (NetStream部分 2)
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4282781.html
Copyright © 2011-2022 走看看