洛谷 P1957 口算练习题

来源:互联网 发布:linux系统运维简历 编辑:程序博客网 时间:2024/06/09 19:42

并不难的一条题目

**就几个点要注意:
1. 计算数字的长度时 要记得可能有负数 对负数要特殊处理
2. 对于这种不是很难 操作却略繁琐的题目 要先将每一步在草稿纸上分析好 再写 以便保持思路的清晰(特别是我这种思维混乱的人)**

//P1957 口算练习题//2016.11.13#include <cstdio>#include <iostream>#include <stdio.h>#include <algorithm>#define MAXN 10000 + 2using namespace std;int n, a, b, c, len;char d;string t;int length(int x){    int len = 0, t = 1;    if (x < 0) len++, x = -x;    for (int i = 1; ;i++){        t = t * 10;        if (x < t) return i + len;    }}int main(){    cin >> n;    for (int i = 0; i < n; i++){        int j = 1;        a = b = 0;        cin >> t;        if (t == "a") d = '+', j--;        else if (t == "b") d = '-', j--;        else if (t == "c") d = '*', j--;        else            for (int j = 0; j < t.size(); j++)                a = a * 10 + t[j] - '0';        if (j) cin >> b;        if (!j) cin>> a >> b;        if (d == '+') c = a + b;        if (d == '-') c = a - b;        if (d == '*') c = a * b;        len = 2;        len = length(a) + length(b) + length(c) + len;          cout << a << d << b << "=" << c << endl;        cout << len << endl;    }    return 0;}
0 0
原创粉丝点击