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

Ulm Local 2001

分析:

思路比较简单

自己的做法:

 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 }
时间: 2024-10-13 17:17:13

poj 2572 Hard to Believe, but True!的相关文章

POJ 2572

1 #include<stdio.h> 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 6 int main() 7 { 8 //freopen("acm.acm","r",stdin); 9 string s; 10 int pos; 11 int pos1; 12 string s1; 13 string s2; 14 string s3; 15

POJ 1417 True Liars 并查集+背包

题目链接:http://poj.org/problem?id=1417 解题思路:比较容易想到的是并查集,然后把第三组数据测试一下之后发现这并不是简单的并查集,而是需要合并之后然后判断的.并且鉴于题目要求输出数据,因此还要记录数据,可以说是非常有意思的题目. 首先,如果a b yes,那么a与b一定都是圣人或者恶人:反之,如果a b no,那么两者一个圣人,一个恶人.因此可以将所有元素分为若干个集合,每个集合中放两个小集合,表示两种不一样的人,当然并不知道到底哪个里面是圣人,恶人. 另一个比较棘

POJ 1417 True Liars

POJ连炸多日-- 1.发现a,b相同会回答yes 否则回答no 2.套用并查集模板 画图 设边权:相同为0,不同为1 猜\(fav[fa1]=fav[a] \) xor \( fav[b] \;\) xor \( d\),枚举各种情况果然成立-- 3.得到一棵树 其实中途会进行路径压缩-- 可以统计每棵树中与代表元素相同的个数(A[i])和不同的个数(B[i])(边权为1说明不同,为0说明相同) 然后问题就转换成 \(n = 树的棵数\) \(X[i] = A[i]\;\;\;or\;\;\;

POJ 1417 True Liars(并查集+DP)

大意: 一个岛上有神与恶魔两个种族,神会说真话,恶魔会说假话.已知神与恶魔的个数,但不知道具体个人是属于哪个. n,x,y 这个人问n次 ,x为神的个数,y为恶魔的个数. 每次的问题为 xi,yi,a 问xi ,yi是否为神? a为yes/no.注意xi,yi可能为同一个人 若最终可得出哪些是神则从小到大输出神的编号,并最终输出end 否则输出no 思路: 经过简单推理可得,只要是说yes,xi,yi为同一个族,no则不为同一个族. 这样通过使用并查集+relation(relation[i]为

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6

POJ 1328 Radar Installation

http://poj.org/problem?id=1328 //第一次:从左边第一个未被覆盖的island开始 -->>失败 因为还有y坐标这一因素 不能保证贪心//第二次:找两个点 确定一个圆 ----->>>其实早就应该发现错误 漏洞百出 不具有普遍性//从左边第一个未覆盖的点作为基点 找到第一个 y坐标>=的点(如果没有找到) 做这两个点的公共圆//如果不能做这两个点的公共圆 或者 没有y>=的点 那么做这个圆的右极限圆//更新覆盖的点//蠢-->&

poj 1113 凸包周长

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w