Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

又一个水题,咋木人做?

Posted by KatrineYang at 2017-01-22 13:29:36 on Problem 1559
#include <iostream>
#include <stdio.h>
using namespace std;

int n;
int oprd[100];
char oprt[100];
int succ[100];

char var[16];

int ans(int op1, int op2, char op){
	if(op == '+') return op1+op2;
	if(op == '-') return op1-op2;
	if(op == '*') return op1*op2;
	if(op == '/') return op1/op2;
	return 0;
}

void print(){
	int start = 0;
	printf("%d ", oprd[0]);
	while(1){
		start = succ[start];
		if(start == -1) break;
		printf("%c %d ", oprt[start], oprd[start]);
	}
	printf("= %s\n", var);
}

int main() {
	while(scanf("%d", &oprd[0]) > 0){
		n = 1;
		int cc = 0;
		while(1){
			scanf("\n%c", &oprt[n]);
			if(oprt[n] == '='){
				n--;
				break;
			}
			if(oprt[n] == '*' || oprt[n] == '/') cc++;
			scanf("%d", &oprd[n]);
			n++;
		}
		scanf("%s", var);
		for(int i = 0; i < n; i++){
			succ[i] = i+1;
		}
		succ[n] = -1;
		print();
		for(int i = 0; i < cc; i++){
			int tar = 0, tarNext = succ[0];
			while(1){
				if(oprt[tarNext] == '*' || oprt[tarNext] == '/') break;
				tar = tarNext;
				tarNext = succ[tarNext];
			}
			oprd[tar] = ans(oprd[tar], oprd[tarNext], oprt[tarNext]);
			succ[tar] = succ[tarNext];
			print();
		}
		for(int i = 0; i < n-cc; i++){
			oprd[0] = ans(oprd[0], oprd[succ[0]], oprt[succ[0]]);
			succ[0] = succ[succ[0]];
			print();
		}
		printf("\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator