• poj2295


    给定一个方程的字符串,要求解一元一次方程,要细心。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    using namespace std;

    string st;
    int a, b, c, d;

    int cal(string st)
    {
    int a;
    sscanf(st.c_str(),
    "%d", &a);
    return a;
    }

    void make(string st, int &a, int &b)
    {
    if (st == "x")
    {
    a
    ++;
    return;
    }
    if (st == "-x")
    {
    a
    --;
    return;
    }
    if (st[st.length() - 1] == 'x')
    a
    += cal(st.substr(0, st.length() - 1));
    else
    b
    += cal(st);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    while (t--)
    {
    cin
    >> st;
    a
    = b = c = d = 0;
    int now = 1;
    int start = 0;
    while (1)
    {
    if (st[now] == '-' || st[now] == '+' || st[now] == '=')
    {
    make(st.substr(start, now
    - start), a, b);
    start
    = now;
    }
    if (st[now] == '=')
    break;
    now
    ++;
    }
    now
    ++;
    start
    = now;
    now
    ++;
    while (1)
    {
    if (st[now] == '-' || st[now] == '+' || st[now] == '\0')
    {
    make(st.substr(start, now
    - start), c, d);
    start
    = now;
    }
    if (st[now] == '\0')
    break;
    now
    ++;
    }
    a
    -= c;
    d
    -= b;
    if (a == 0 && d == 0)
    printf(
    "IDENTITY\n");
    else if (a == 0 && d != 0)
    printf(
    "IMPOSSIBLE\n");
    else
    printf(
    "%d\n",(int)floor(d * 1.0 / a));
    }
    return 0;
    }
  • 相关阅读:
    Android状态栏和导航栏
    ScrollView小记
    iOS本地推送
    java-集合框架-泛型1
    java-集合框架4---foreach使用--for的增强使用
    java-集合框架3-迭代器的使用
    JAVA-集合框架2
    Number 数据类型转化 NaN 函数isNaN
    数据类型分类
    VScode 插件推荐安装
  • 原文地址:https://www.cnblogs.com/rainydays/p/2136680.html
Copyright © 2020-2023  润新知