2020-08-04 口算练习题

2020-08-04  本文已影响0人  JalorOo

https://www.luogu.com.cn/problem/P1957

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
using namespace std;

long long qmi(int m, int k)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1) res = res * t;
        t = t * t;
        k >>= 1;
    }
    return res;
}


int read(){
    int x = 0,f = 1;
    char c = getchar();
    while (c<'0'||c>'9') {
        if (c=='-') {
            f = -1;
        }
        c = getchar();
    }
    while (c>='0'&&c<='9') {
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}


void output(int type){
    int a = read();
    int b = read();
    stringstream ss;
    if(type==0){
        ss<<a<<"+"<<b<<"="<<a+b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }else if(type==1){
        ss<<a<<"-"<<b<<"="<<a-b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }else if(type==2){
        ss<<a<<"*"<<b<<"="<<a*b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }
}

void output(int type,int a){
    int b = read();
    stringstream ss;
    if(type==0){
        ss<<a<<"+"<<b<<"="<<a+b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }else if(type==1){
        ss<<a<<"-"<<b<<"="<<a-b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }else if(type==2){
        ss<<a<<"*"<<b<<"="<<a*b;
        string ab = ss.str();
        cout<<ss.str()<<endl<<ss.str().length()<<endl;
    }
}

int main(){
    //定义两个字符串
    int n = read();
    int type = 0;
    for (int i=0;i<n;++i){
        string s;
        cin>>s;
        if(s[0]>='a'){//字母
            type = s[0] - 'a';
            output(type);
        } else {
            int a = 0;
            for (int i = 0; i<s.length(); i++) {
                a += (s[i]-'0')*qmi(10, s.length()-1-i);
            }
            output(type,a);
        }
    }
    return 0;
}
/*
5
abc
aaaa
abc
abcc
12345
============
4
*/

上一篇下一篇

猜你喜欢

热点阅读