Created
September 15, 2018 03:52
-
-
Save completejavascript/c057a3e85e3009b4309f829edfbb3f97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
const int MAX = 1000; | |
bool IsDigit(char ch) | |
{ | |
if((ch >= '0' && ch <= '9')) return true; | |
return false; | |
} | |
/* | |
* Trả về độ dài xâu nếu chúng chứa toàn chữ số từ 0 đến 9 | |
* Nếu trong xâu có tồn tại kí tự không phải là số thì trả về -1 | |
*/ | |
int GetLength(char* arr) | |
{ | |
int length = 0; | |
for(int i = 0; i < MAX; i++) | |
{ | |
if(arr[i] == '') break; | |
else if(IsDigit(arr[i])) length++; | |
else | |
{ | |
length = -1; | |
break; | |
} | |
} | |
return length; | |
} | |
/* | |
* Chuyển đổi xâu chứa toàn chữ số thành số nguyên kiểu int | |
* Nếu xâu đầu vào không hợp lệ, tức chứa kí tự không phải số | |
* thì trả về số -1 | |
* ngược lại thì trả về giá trị của số tương ứng | |
*/ | |
int ConvertArrayToInt(char* str) | |
{ | |
int length = GetLength(str); | |
if(length < 0) return -1; | |
int r = 0; | |
for(int i = 0; i < length; i++) | |
r = r * 10 + str[i] - '0'; | |
return r; | |
} | |
int main() | |
{ | |
ios::sync_with_stdio(false); | |
// comment dòng này khi submit | |
freopen("input.txt","r",stdin); | |
int T = 0; | |
cin >> T; | |
// tạo ra mảng 5 phần tử để lưu 5 thành phần trong biểu thức | |
int a[5]; | |
for(int tc = 0; tc < T; tc++) | |
{ | |
for(int i = 0; i < 5; i++) | |
{ | |
char str[MAX]; | |
cin >> str; | |
a[i] = ConvertArrayToInt(str); | |
} | |
// Thực tế ta chỉ cần quan tâm đến phần tử a[0], a[2], a[4] | |
// vì chúng là số | |
int n1 = a[0]; | |
int n2 = a[2]; | |
int n3 = a[4]; | |
// Vì mỗi biểu thức chỉ có một cách duy nhất để hoàn thành, | |
// nên chỉ có khả năng | |
// là 1 trong 3 vị trí bị dính mực | |
if(n1 < 0) a[0] = a[4] - a[2]; | |
else if (n2 < 0) a[2] = a[4] - a[0]; | |
else a[4] = a[0] + a[2]; | |
cout << a[0] << " + " << a[2] << " = " << a[4] << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment