[Zhengqing] [1773] Lovely simple problem two

Problem Description

Loading...

Problem Analysis

At first there was no HINT
so the approach I came up with was rather convoluted
The super-long code is as follows:

cpp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include<stdio.h>
#include<string.h>
int main() {
	char s[100];
	int x,l,i;
	int re,t,p;
	while(scanf("%s",s)!=EOF) {
		l=strlen(s);
		for(i=x=re=0; i<l; i++) {
			if(i==0) {
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(re=0; '0'<=s[i]&&s[i]<='9'; i++) {
					re=re*10+s[i]-'0';
				}
				if(x==1) {
					x=0;
					re=-re;
				}
			}
			if(s[i]=='+') {
				i++;
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(p=0; '0'<=s[i]&&s[i]<='9'; i++) {
					p=p*10+s[i]-'0';
				}
				if(x==1) {
					x=0;
					p=-p;
				}
				re=re+p;
			} else if(s[i]=='-') {
				i++;
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(p=0; '0'<=s[i]&&s[i]<='9'; i++) {
					p=p*10+s[i]-'0';
				}
				if(x==1) {
					x=0;
					p=-p;
				}
				re=re-p;
			} else if(s[i]=='*') {
				i++;
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(p=0; '0'<=s[i]&&s[i]<='9'; i++) {
					p=p*10+s[i]-'0';
				}
				if(x==1) {
					x=0;
					p=-p;
				}
				re=re*p;
			} else if(s[i]=='/') {
				i++;
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(p=0; '0'<=s[i]&&s[i]<='9'; i++) {
					p=p*10+s[i]-'0';
				}
				if(x==1) {
					x=0;
					p=-p;
				}
				re=re/p;
			}
			if(s[i]=='=') {
				i++;
				if(s[i]=='-') {
					i++;
					x=1;
				}
				for(p=0; '0'<=s[i]&&s[i]<='9'; i++) {
					p=p*10+s[i]-'0';
				}
				if(x==1)
					p=-p;
				if(re==p)
					printf("YES\n");
				else
					printf("NO\n");
			} else
				i--;
		}
	}
	return 0;
}

Also, here is @A_ice’s infuriatingly short code:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
int main()
{
	int a,b,d,sum;
	char s,w;
	while(~scanf("%d%c%d%c%d",&a,&s,&b,&w,&d))
	{
		if(s=='+')
			sum=a+b;
		if(s=='-')
			sum=a-b;
		if(s=='*')
			sum=a*b;
		if(s=='/')
			sum=a/b;
		if(sum==d)
			printf("YES\n");
		else
			printf("NO\n");
	}	
	return 0;
}

After reading it, all I could think was…
Holy crap… that actually works -.-

Problem link: HDU 1773 Lovely simple problem two

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...