F

来源:互联网 发布:手机怎么淘宝实名认证 编辑:程序博客网 时间:2024/05/16 10:54

看过电视剧《马大帅2》的同学,一定对其中“马大帅代课”一段情节有深刻的印象。当时,马大帅的打工子弟小学的四年级数学老师有事不能上课,马大帅到处找老师找不到,无奈只好亲自出马,代这堂数学课。这堂课本来应该讲方程题,可马大帅一道方程题也不会。结果,在学生的问题面前出尽了洋相,令人忍俊不禁:

现在,请你来帮帮可怜的马大帅,解决一些简单的一元一次方程。

Input
每组数据占一行。每组数据是一个一元一次方程,以小写的x为未知数,且只由数字、x、=、+、-这几种字符组成。每组数据都是一个有效的方程,而不会是类似“ x+=5=3x- ”这样的没有意义的字符串。无论常数项还是x的系数的绝对值都不会大于10000,且没有前导0。每个方程只有一个等号。方程内部没有空格。每个方程不会超过100个字符。

Output
对于每组数据,如果有解,先输出“x=”,然后输出它的解。如果解是整数,就输出这个整数。如果不是整数,把它化为最简分数再输出。输出格式参见Sample。

如果无解,输出“No solution”。如果有无数个解,输出“Infinite many solutions”。

Sample Input
6x-35=13
12-4x=8+2x
2x+x+25+3x=3x+8
20x+40-100=-15x-60+35x
7+5x=8x+6-3x
Sample Output
x=8
x=2/3
x=-17/3
Infinite many solutions
No solution

水题靠技术

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<algorithm>#include<stack>#include<queue>#include<cctype>using namespace std;const int MAX = 999999;const double Eps = 1e-12;const double PI = acos(-1.0);int gcd(int x, int y){    return x%y == 0 ? y : gcd(y, x%y);}int main(){    char a[200];    while (~scanf("%s", a))    {        int numc, numx;        int stu, t;        numc = numx = 0;        stu = t = 0;        int len = strlen(a);        int i;        for (i = 0; i < len; i++)        {            if (a[i] >= '0'&&a[i] <='9')            {                if (stu == 1 || stu == 0)                    t = t * 10 + (a[i] - '0');                else if (stu == -1)                    t = t * 10 - (a[i] - '0');            }            else if (a[i] == 'x')            {                if (t)                {                    numx += t;                    t = 0;                }                else if (a[i-1] == '-')                    numx--;                else if (a[i-1] == '+' || i == 0)                    numx++;            }            else if (a[i] == '=' || a[i] == '+' || a[i] == '-')            {                numc += t;                t = 0;                 if (a[i] == '+')                    stu = 1;                else if (a[i] == '-')                    stu = -1;                else if (a[i] == '=')                    break;            }        }        //cout << numc << "____________" << numx << endl;        stu = t = 0;        for (; i < len; i++)        {            if (a[i] >= '0'&&a[i] <='9')            {                if (stu == 1 || stu == 0)                    t = t * 10 + (a[i] - '0');                else if (stu == -1)                    t = t * 10 - (a[i] - '0');            }            else if (a[i] == 'x')            {                if (t)                {                    numx -= t;                    t = 0;                }                else if (a[i-1] == '-')                    numx++;                else if (a[i-1] == '+' || a[i-1] == '=')                    numx--;            }            else if (a[i] == '=' || a[i] == '+' || a[i] == '-')            {                    numc -= t;                    t = 0;                 if (a[i] == '+')                    stu = 1;                else if (a[i] == '-')                    stu = -1;            }        }        if (t)            numc -= t;        numc *= -1;        if (numc == 0 && numx == 0)            puts("Infinite many solutions");        else if (numx == 0 && numc != 0)            puts("No solution");        else if (numc == 0 && &numx != 0)            puts("x=0");        else        {            if (numx < 0 && numc>0 || numx > 0 && numc < 0)            {                numc = abs(numc);                numx = abs(numx);                int g = gcd(numx, numc);                numx /= g;                numc /= g;                if (numx == 1)                    printf("x=-%d\n", numc);                else                    printf("x=-%d/%d\n", numc, numx);            }            else            {                numc = abs(numc);                numx = abs(numx);                int g = gcd(numx, numc);                numx /= g;                numc /= g;                if (numx == 1)                    printf("x=%d\n", numc);                else                    printf("x=%d/%d\n", numc, numx);            }        }    }    return 0;}
0 0