zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    C陷阱与缺陷学习笔记
    C陷阱与缺陷学习笔记
    Linux Socket编程(不限Linux)
    Linux Socket编程(不限Linux)
    Windows API 教程(九) 网络编程
    Windows API 教程(九) 网络编程
    sockaddr与sockaddr_in的关系
    winsocket入门学习
    09-C语言数组
    08-C语言循环
  • 原文地址:https://www.cnblogs.com/rainydays/p/2136680.html
Copyright © 2011-2022 走看看