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 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4282781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值