NYOJ:题目524 A-B Problem

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860

My思路:

先用两个字符串储存这两个实数,然后再用另外两个字符串储存去掉符号和前后多余的0后的新"实数",最后只需要比较两个化简后的新字符就ok了。

My代码实现:

 1 #include<iostream>
 2 using namespace std;
 3 string simplify(string s) {  //去字符串s的正负号和首尾多余的0
 4   string a;
 5   int k = s.size()-1, l = 0, t = 0;
 6   for(int i = 0; i < s.size(); i++)  //判断是否是小数,如果是小数就需要去除尾部多余的0
 7     if(s[i] == ‘.‘) {
 8       t = 1;
 9       break;
10     }
11   if(s[0] == ‘+‘ || s[0] == ‘-‘) l++;  //去除符号
12   while(s[l] == ‘0‘) l++;  //去除前面多余的0
13   //cout << l << endl;
14   if(t) {  //去除后面多余的0
15     while(s[k] == ‘0‘) k--;
16   }
17   if(s[k] == ‘.‘) k--;
18   //cout << k << endl;
19   while(l <= k) a += s[l++];
20   return a;
21 }
22 int main() {
23   //string x = "-002540000.0000025000000";
24   //cout << simplify(x);
25   string A, B, a, b;
26   char fa, fb;
27   while(cin >> A >> B) {
28     a = simplify(A);  //去除符号和首尾多余的0
29     b = simplify(B);
30     //cout << a << "\n" << b << endl;
31     if(A[0] != ‘+‘ && A[0] != ‘-‘) fa = ‘+‘;  //正负号拿出来单独判断
32     else fa = A[0];
33     if(B[0] != ‘+‘ && B[0] != ‘-‘) fb = ‘+‘;
34     else fb = B[0];
35     if(((fa == ‘+‘ && fb == ‘-‘) || (fa == ‘-‘ && fb == ‘+‘)) && (a != "" && b != "")) cout << "NO\n";  //这里千万要考虑+0=-0的情况
36     else {
37       int i, p = 1;
38       for(i = 0; i < a.size() && i < b.size(); i++)
39         if(a[i] != b[i]) {
40           cout << "NO\n";
41           p = 0;
42           break;
43         }
44       if(i == b.size() && i == a.size()) cout << "YES\n";
45       else if(p) cout << "NO\n";
46     }
47   }
48 }

代码实现(点击展开)

      开始写于:2016.7.31  ----志银

时间: 2024-10-13 15:53:48

NYOJ:题目524 A-B Problem的相关文章

NYOJ 题目477 A+B Problem III

题目描述: 求A+B是否与C相等. 输入 T组测试数据.每组数据中有三个实数A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0)数据保证小数点后不超过4位. 输出 如果相等则输出Yes不相等则输出No 样例输入 3 -11.1 +11.1 0 11 -11.25 -0.25 1 2 +4 样例输出 Yes Yes No #include<stdio.h>int main(){ int N; scanf("%d&q

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVa 524 - Prime Ring Problem

题目:把1-n,连续的放到一个环里,使相邻的数字和为素数,输出所有结果. 分析:搜索+剪枝.如果裸搜,用dancing-links那种拆装的链表,应该差不多满足16的数据量. 这里利用一个性质进行剪枝:相邻的数字一定是奇偶性不同的数字. (如果上述假设不成立,则存在相邻的奇数或偶数,那么他们的和一定是大于2的偶数,不是素数) 根据上面的假设,还有一条推论:只有n为偶数时才有解. (n为奇数,一直至少有一对奇数相邻,同上,矛盾(鸽巢原理)) 因此,每次搜索的数据其实是n/2,时间复杂度为O((n/

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

NYOJ题目216 A problem is easy

//AC  心得:思考题目要求,寻找好合适的条件. #include<stdio.h>int main(){ int T; scanf("%d",&T); while(T--) { int n,i; int a=0; scanf("%d",&n); for(i=1;(i+1)*(i+1)<=n+1;i++)//数学平方数理想化 { if((n+1)%(i+1)==0)//i*j+i+j =N 经过观察,可以变形为i*j+i+j+1=

NYOJ题目57 6174问题

----------------------------------------------------- 感觉这个OJ题目难度划分很不合理,这道理明明很简单却给了2的难度,而之前难度为0的水题有好多难死个人没做出来让我暗暗觉得自己脑子里都是屎... 把题目描述翻译成人话的意思就是多少次以后这个序列会出现,想明白这一点就比较简单了. AC代码: 1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main

NYOJ题目1049自增自减

--------------------------------- 简单的字符判断. AC代码: 1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner sc=new Scanner(System.in); 8 9 int times=Integer.parseInt(sc.nextLine()); 10 while(times-->0

NYOJ题目10505C?5S?

--------------------------------------- 水. AC代码: 1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner sc=new Scanner(System.in); 8 9 int times=sc.nextInt(); 10 while(times-->0){ 11 double a=sc.n

NYOJ题目28大数阶乘

-------------------------------------祭出BigInteger AC代码: import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); BigInteger ans=fac(n);