1944.cpp

来自「这是哈尔滨工业大学acmOJ的源代码」· C++ 代码 · 共 57 行

CPP
57
字号
/*  This Code is Submitted by wywcgs for Problem 1944 on 2006-01-29 at 17:21:47 */ 
#include <cstdio>
#include <algorithm>
using namespace std;

int coe, con, left;

int gcd(int, int);
void scan(int);

int main()
{
	char ch;

	while((ch = getchar()) != EOF) {
		coe = con = 0; left = 1;
		int sign = 1;
		if(ch == '-') sign = -1;
		else ungetc(ch, stdin); 
		scan(sign);
		while((ch = getchar()) != '\n') {
			switch(ch) {
			case '=': left = -1;
			case '+': sign = 1; break;
			case '-': sign = -1; break;
			}
			scan(sign);
		}
		if(coe == 0) {
			if(con == 0) printf("Infinite many solutions\n");
			else printf("No solution\n");
		} else {
			int d = gcd(abs(coe), abs(con));
			coe /= d; con /= d;
			if(coe < 0) coe = -coe, con = -con;
			printf("x=%d", con);
			if(coe != 1) printf("/%d", coe);
			putchar('\n');
		}
	}
	
	return 0;
}

int gcd(int a, int b)
{
	if(b == 0) return a;
	else return gcd(b, a%b);
}
void scan(int sign)
{
	int c = 1; scanf("%d", &c);
	char ch = getchar();
	if(ch == 'x') coe += left*sign*c;
	else ungetc(ch, stdin), con -= left*sign*c;
}

⌨️ 快捷键说明

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