⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3091264_wa.java

📁 北大大牛代码 1240道题的原代码 超级权威
💻 JAVA
字号:
import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        String exp, l, r;
        int t, p;
        
        t = cin.nextInt();
        while(t-- > 0)
        {
            exp = cin.next();
            int a [] = new int [2];
            int b [] = new int [2];
            p = exp.indexOf('=');
            l = exp.substring(0,p);
            r = exp.substring(p+1);
            solve(l,a,b,0);
            solve(r,a,b,1);
            if(a[0]==a[1])
            {
                if(b[0]==b[1])
                    System.out.println("IDENTITY");
                else
                    System.out.println(" IMPOSSIBLE");
            }
            else
            {
                System.out.println((b[1]-b[0])/(a[0]-a[1]));
            }
        }
    }

    private static void solve(String exp, int [] a, int [] b,int id) 
    {
        int j, i, st, d;
        String tmp;
        
        a[id] = b[id] = 0;
        exp = "+" + exp;
        st = 0;
        while(st < exp.length())
        {
            for(j = st+1; ; j++)
            {
                if(j==exp.length()||exp.charAt(j)=='-'||exp.charAt(j)=='+')
                {
                    tmp = exp.substring(st+1,j);
                    d = tmp.indexOf('x');
                    if(d < 0)
                    {
                        if(exp.charAt(st)=='+')
                        {
                            b[id] += Integer.parseInt(tmp);
                        }
                        else
                        {
                            b[id] -= Integer.parseInt(tmp);
                        }
                    }
                    else
                    {
                        if(tmp.length()==1)
                        {
                            if(exp.charAt(st)=='+')
                            {
                                a[id] ++;
                            }
                            else
                            {
                                a[id] --;
                            }
                        }
                        else
                        {
                            if(exp.charAt(st)=='+')
                            {
                                a[id] += Integer.parseInt(tmp.substring(0,tmp.length()-1));
                            }
                            else
                            {
                                a[id] -= Integer.parseInt(tmp.substring(0,tmp.length()-1));
                            }
                        }
                    }
                    st = j;
                    break;
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -