2018年7月23日 星期一

itsa [C_CH20-易]盜墓驚魂

1.把錢轉成2進位
2.用XOR做運算

#include<iostream>
using namespace std;

int main()
{
int n,num,sum,flag;
int arr[8] = {0};
num = 0;
sum = 0;
cin >> n;
int p[100];
int b[100] = {0};
for (int j = 0;j < n; j++)
{
cin >> p[j];
}
for (int k = 0;k < n; k++)
{
for (int x = 7;x >= 0; x--)
{
arr[x] = p[k] % 2;
p[k] /= 2;
}
int y = 1;
for (int x = 7;x >= 0; x--)
{
b[k] += arr[x] * y;
y *= 10;
}
}
for (int z = 0;z < n; z++)
{
if (z == 0)
num = b[z];
else
num = num ^ b[z];
}
flag = num ^ 0;
if (flag != 0)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}

itsa [C_BT03-中]正方形

1.每根棍子長度不一定相同,需判斷不能組成的時候
2.先把棍子的總和長度算出來,再除以4,算出邊長(即組成正方形的最大長度)
3.超出邊長長度,就是無法組成正方形
註:雖然這樣是過了,但是不知有沒有例外(゚ー゚)

#include<iostream>
using namespace std;

int main()
{
int n,sum,ans;
int m[1000];
sum = 0;
ans = 0;
cin >> n;
for (int i = 0;i < n; i++)
{
cin >> m[i];
sum += m[i];
}
for (int j = 0;j < n; j++)
{
if (m[j] > sum / 4)
ans = 1;
}
if (ans == 0)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}

itsa [C_AR111-易]對話機器人

#include<iostream>
#include<string>
using namespace std;

int main()
{
int n = 0;
string s;
string say[5] = { "Sorry","Hi","Hello","How do you do","How are you" };
while (getline(cin, s))
{
if (s == "Hi" || s == "Hello" || s == "How do you do" || s == "How are you")
{
n++;
cout << say[n] << endl;
if (n == 4)
n = 0;
}
else
{
n = 0;
cout << say[0] << endl;
}
}
return 0;
}

itsa [C_AR021-易]成績統計

1.需要再四捨五入到第一位
(floor( X * 10 + 0.5)) / 10;  //10需根據小數幾位而變化

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
int n;
double chinese, english, math, allavg = 0, cavg = 0, eavg = 0, mavg = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> chinese >> english >> math;
allavg += chinese + english + math;
cavg += chinese;
eavg += english;
mavg += math;
}
allavg = (floor((allavg / (3 * n)) * 10 + 0.5)) / 10;
cavg = (floor((cavg / n) * 10 + 0.5)) / 10;
eavg = (floor((eavg / n) * 10 + 0.5)) / 10;
mavg = (floor((mavg / n) * 10 + 0.5)) / 10;
cout << fixed << setprecision(1) << allavg << " " << cavg << " " << eavg << " " << mavg << endl;
return 0;
}

itsa [C_AR20-易]檢查數值是否有重複

#include<iostream>
using namespace std;

int main()
{
int n, ans = 0;
int count[129] = { 0 };
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (arr[j] == arr[i])
count[arr[j]]++;
if (count[arr[j]] > 1)
{
ans = 1;
break;
}
}
if (ans == 1)
break;
}
if (ans == 0)
cout << "1" << endl;
else
cout << "0" << endl;
return 0;
}

itsa [C_AR19-易]最大連續子序列

#include<iostream>
using namespace std;

int main()
{
int N, count = 0, len = 0, max = 0, a = 0, b = 0;
cin >> N;
int *n = new int[N];
int *tmp = new int[N];
for (int i = 0; i < N; i++)
cin >> n[i];
for (int j = N; j > 0; j--)
{
if (j >= 2)
{
for (int i = 0; i <= N - j; i++)
{
a = 0;
count = 0;
for (int x = i; x < i + j; x++)
{
tmp[a] = n[x];
a++;
}
for (int x = 0; x <= N - j; x++)
{
b = 0;
for (int y = x; y < x + j; y++)
{
if (tmp[b] == n[y])
{
b++;
if (b == j)
{
b = 0;
count++;
}
}
else
break;
}
}
if (count > 1)
{
len = j;
if (len > max)
max = len;
}
}
}
else
{
for (int i = 0; i < N; i++)
{
count = 0;
tmp[0] = n[i];
for (int x = 0; x < N; x++)
{
if (tmp[0] == n[x])
count++;
}
if (count > 1)
{
len = j;
if (len > max)
max = len;
}
}
}
}
cout << max << endl;
return 0;
}

itsa [C_AR17-中]不同基底整數轉換

1.先將原進位轉成10進位,再從10進位轉欲轉成的進位。
2.如果數字為0,則直接輸出。
3.注意英文轉換

#include<iostream>
#include<string>
using namespace std;

int tenMode(char array[], int num, int len);
void transF(int value, int num);

int main()
{
int B1, B2;
char num[30];
string X1;
getline(cin, X1);
int len = X1.length();
cin >> B1;
cin >> B2;
for (int i = 0; i < len; i++)
num[i] = X1[i];
int X1value = tenMode(num, B1, len);
if (X1value != -1)
{
if (X1value == 0)
cout << "0";
else
transF(X1value, B2);
cout << endl;
}
else
cout << "Input Error" << endl;
return 0;
}

int tenMode(char array[], int num, int len)
{
int value = 0, seat = 1;
for (int i = len - 1; i >= 0; i--)
{
int tmp = 0;
if (array[i] >= 'A' && array[i] <= 'F')
tmp = array[i] - 'A' + 10;
else
tmp = array[i] - '0';
if (tmp >= num)
return -1;
else
value += tmp * seat;
seat *= num;
}
return value;
}

void transF(int value, int num)
{
if (value == 0)
return;
else
{
transF((int)(value / num), num);
if (value % num >= 10)
cout << (char)((value % num) - 10 + 'A');
else
cout << value % num;
}
}

itsa [C_AR16-易]統一發票對獎

#include<iostream>
#include<string>
using namespace std;

int main()
{
int N,count,tmp;
long int sum;
int arr[7] = {0};
int mon[7] = {2000000,200000,40000,10000,4000,1000,200};
string t, h1, h2, h3, num;
getline(cin, t);
getline(cin, h1);
getline(cin, h2);
getline(cin, h3);
sum = 0;
cin >> N;
cin.ignore();
for (int i = 0; i < N; i++)
{
tmp = 0;
count = 0;
getline(cin, num);
if (num == t)
{
arr[0]++;
}
else
{
for (int j = 0; j < 3; j++)
{
if (j == 0)
{
for (int a = 7; a >= 0; a--)
{
if (num[a] == h1[a])
count++;
else
break;
}
if (count >= 3)
tmp = count;
count = 0;
}
else if (j == 1)
{
for (int a = 7; a >= 0; a--)
{
if (num[a] == h2[a])
count++;
else
break;
}
if (count >= 3)
if (count > tmp)
tmp = count;
count = 0;
}
else if (j == 2)
{
for (int a = 7; a >= 0; a--)
{
if (num[a] == h3[a])
count++;
else
break;
}
if (count >= 3)
if (count > tmp)
tmp = count;
arr[9 - tmp]++;
count = 0;
}
}
}
}
for (int c = 0; c < 7; c++)
{
sum += mon[c] * arr[c];
}
for (int k = 0;k < 7; k++)
{
cout << arr[k];
if (k < 6)
cout << " ";
}
cout << endl;
cout << sum << endl;
return 0;
}

itsa [C_AR15-易]保齡球計分

1. ‘-’ 則代表 miss 或沒有分數,指全倒後沒有瓶子,和沒打中,即0瓶(分)。
例: 全倒 X,-
沒中 -,6   -,/
2.把'-'換成0,方便運算。

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ',')
s[i] = ' ';
else if (s[i] == '-')
s[i] = '0';
i++;
}
}

int main()
{
int sum = 0, score = 0, x = 0, y = 0;
int arr[10] = { 0 };
char n[21];
string s;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> n[x])
{
x++;
}
for (int i = 0; i < 19; i++)
{
score = 0;
if (i < 16)
{
if (n[i] == 'X')
{
score = 10;
if (n[i + 2] == 'X')
{
score += 10;
if (n[i + 4] == 'X')
score += 10;
else
score += n[i + 4] - '0';
}
else if (n[i + 3] == '/')
score += 10;
else
score += n[i + 2] - '0' + n[i + 3] - '0';
}
else if (n[i + 1] == '/')
{
score = 10;
if (n[i + 2] == 'X')
score += 10;
else
score += n[i + 2] - '0';
}
else
score = n[i] - '0' + n[i + 1] - '0';
i++;
}
else if (i == 16)
{
if (n[i] == 'X')
{
score = 10;
if (n[i + 2] == 'X')
{
score += 10;
if (n[i + 3] == 'X')
score += 10;
else
score += n[i + 3] - '0';
}
else if (n[i + 3] == '/')
score += 10;
else
score += n[i + 2] - '0' + n[i + 3] - '0';
}
else if (n[i + 1] == '/')
{
score = 10;
if (n[i + 2] == 'X')
score += 10;
else
score += n[i + 2] - '0';
}
else
score = n[i] - '0' + n[i + 1] - '0';
i++;
}
else
{
if (n[i] == 'X')
{
score += 10;
if (n[i + 1] == 'X')
{
score += 10;
if (n[i + 2] == 'X')
score += 10;
else
score += n[i + 2] - '0';
}
else if (n[i + 2] == '/')
score += 10;
else
score += n[i + 1] - '0' + n[i + 2] - '0';
}
else if (n[i + 1] == '/')
{
score += 10;
if (n[i + 2] == 'X')
score += 10;
else
score += n[i + 2] - '0';
}
else
score += n[i] - '0' + n[i + 1] - '0';
}
sum += score;
arr[y++] = sum;
}
for (int i = 0; i < 10; i++)
{
if (i > 0)
cout << ",";
cout << arr[i];
}
cout << endl;
return 0;
}

itsa [C_AR14-易]糖果分享

1.拿到一袋糖果的同學必須而且只能把糖果分給他前後左右的同學,包括他自己。

#include<iostream>
using namespace std;

int main()
{
int N, n, m, L, x, y, ans;
int arr[22][22];
int f[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
cin >> N;
for (int i = 0; i < N; i++)
{
ans = 0;
for (int j = 0; j < 22; j++)
for (int k = 0; k < 22; k++)
arr[j][k] = 0;
cin >> n >> m >> L;
for (int j = 0; j < L; j++)
{
cin >> x >> y;
arr[y][x] = 1;
for (int k = 0; k < 4; k++)
arr[y + f[k][0]][x + f[k][1]] = 1;
}
for (int j = 1; j <= m; j++)
{
for (int k = 1; k <= n; k++)
{
if (arr[j][k] == 0)
ans = 1;
}
}
if (ans == 0)
cout << "Y" << endl;
else
cout << "N" << endl;
}
return 0;
}

itsa [C_AR13-易]平面魔方

1.每個整數以5個位置列印,為每個數的個位數在第5個位置。
例:設空白為X
1         XXXX1
10       XXX10
100     XX100
2.每筆測資間,即輸出完需再換行一次,但最後一筆不用。

#include<iostream>
#include<string>
using namespace std;

int main()
{
int M, n, num, a, b;
int arr[10][10], ans[10][10];
string s;
cin >> M;
for (int i = 0; i < M; i++)
{
num = 1;
cin >> n;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++)
{
arr[x][y] = num;
num++;
}
}
getline(cin, s);
getline(cin, s);
for (int j = 0; j < s.length(); j++)
{
if (s[j] == 'R')
{
a = 0;
for (int y = n - 1; y >= 0; y--)
{
b = 0;
for (int x = 0; x < n; x++)
{
ans[x][y] = arr[a][b];
b++;
}
a++;
}
}
else if (s[j] == 'L')
{
b = n - 1;
for (int x = 0; x < n; x++)
{
a = 0;
for (int y = 0; y < n; y++)
{
ans[x][y] = arr[a][b];
a++;
}
b--;
}
}
else
{
a = 0;
for (int x = n - 1; x >= 0; x--)
{
b = 0;
for (int y = 0; y < n; y++)
{
ans[x][y] = arr[a][b];
b++;
}
a++;
}
}
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
arr[x][y] = ans[x][y];
}
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++)
{
if (arr[x][y] < 10)
cout << "    " << ans[x][y];
else if (arr[x][y] < 100)
cout << "   " << ans[x][y];
else
cout << "  " << ans[x][y];
}
cout << endl;
}
if (i < M - 1)
cout << endl;
}
return 0;
}

itsa [C_AR12-易]遊樂園的摩天輪

1.左右跳的時候,超過編號時要跳回0或N
2.非0(有人),才算1個車廂

#include<iostream>
using namespace std;

int main()
{
int N, A, B, C, num, count;
int n[100] = { 0 };
cin >> N;
for (int i = 0; i < N; i++)
{
num = 0;
count = 0;
cin >> A >> B >> C;
for (int j = 0; j < C; j++)
n[j] = j + 1;
for (int j = 0; j < C - 1; j++)
{
count = 0;
if (j % 2 == 0)
{
while (count != B)
{
if (num < C - 1)
num++;
else
num = 0;
if (n[num] != 0)
count++;
}
n[num] = 0;
}
else
{
while (count != A)
{
if (num > 0)
num--;
else
num = C - 1;
if (n[num] != 0)
count++;
}
n[num] = 0;
}
}
for (int k = 0; k < C; k++)
{
if (n[k] != 0)
cout << n[k] << endl;
}
}
return 0;
}

itsa [C_AR11-易]最大權重和

#include<iostream>
using namespace std;

int main()
{
int N, m, n, k, sum, max;
int arr[10][10];
cin >> N;
for (int i = 0; i < N; i++)
{
sum = 0;
max = 0;
cin >> m >> n >> k;
for (int x = 0; x < m; x++)
for (int y = 0; y < n; y++)
cin >> arr[x][y];
if (k >= 2)
{
for (int x = 0; x <= m - k; x++)
{
for (int y = 0; y <= n - k; y++)
{
sum = 0;
for (int j = x; j < x + k; j++)
for (int g = y; g < y + k; g++)
sum += arr[j][g];
if (sum > max)
max = sum;
}
}
}
else
{
for (int x = 0; x < m; x++)
{
for (int y = 0; y < n; y++)
{
if (arr[x][y] > max)
max = arr[x][y];
}
}
}
cout << max << endl;
}
return 0;
}

itsa [C_AR10-中]新通話費率

#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;

void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ',')
s[i] = ' ';
i++;
}
}

int main()
{
int month = 0, second = 0;
double cost = 0;
string s;
getline(cin, s);
replaceD(s);
stringstream ss(s);
ss >> month >> second;
if (month == 186)
{
cost = second * 0.09;
if (cost <= month)
cost = month;
else if (cost <= month * 2)
cost *= 0.9;
else
cost *= 0.8;
}
else if (month == 386)
{
cost = second * 0.08;
if (cost <= month)
cost = month;
else if (cost <= month * 2)
cost *= 0.8;
else
cost *= 0.7;
}
else if (month == 586)
{
cost = second * 0.07;
if (cost <= month)
cost = month;
else if (cost <= month * 2)
cost *= 0.7;
else
cost *= 0.6;
}
else
{
cost = second * 0.06;
if (cost <= month)
cost = month;
else if (cost <= month * 2)
cost *= 0.6;
else
cost *= 0.5;
}
cout << fixed << setprecision(0) << cost << endl;
return 0;
}

itsa [C_AR09-易]兩數差值

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ',')
s[i] = ' ';
i++;
}
}

int main()
{
int x = 0, MAX = 0, MIN = 0, largest = 1, ml = 1, nl = 1;
int n[7] = { 0 };
string s;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> n[x])
{
x++;
largest *= 10;
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i; j < x; j++)
{
if (n[i] > n[j])
{
int tmp = n[i];
n[i] = n[j];
n[j] = tmp;
}
}
}
for (int i = 0; i < x; i++)
{
ml = 1;
nl = largest / 10;
for (int j = 0; j < i; j++)
{
ml *= 10;
nl /= 10;
}
MAX += n[i] * ml;
MIN += n[i] * nl;
}
cout << MAX - MIN << endl;
return 0;
}

itsa [C_AR08-易]找出數字字串中的最大質數

1.長度1需與2以上分開算
2.把字元轉成整數(要注意位數問題)

#include<iostream>
#include<string>
using namespace std;

int main()
{
int num = 0, l = 0, prime = 0, check = 0;
string s;
getline(cin, s);
if (s.length() >= 2)
{
for (int k = s.length(); k >= 2; k--)
{
for (int i = 0; i <= s.length() - k; i++)
{
num = 0;
check = 0;
for (int j = i; j < i + k; j++)
{
l = 1;
for (int x = j - i + 1; x < k; x++)
l *= 10;
num += (s[j] - '0') * l;
}
for (int j = 2; j * j <= num; j++)
{
if (j % 2 == 0 && j > 2)
continue;
if (num % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
if (num > prime)
prime = num;
}
}
}
}
for (int i = 0; i < s.length(); i++)
{
check = 0;
num = s[i] - '0';
for (int j = 2; j < num; j++)
{
if (num % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
if (num > prime)
prime = num;
}
}
if (prime == 0)
cout << "No prime found" << endl;
else
cout << prime << endl;
return 0;
}

itsa [C_AR07-中]有違反數獨的規則嗎

#include<iostream>
#include<string>
using namespace std;

int main()
{
int n = 0, subnum = 0, check = 0, blocknum = 1, ans = 0;
int num[10] = { 0 };
char arr[9][9];
string s;
while (n < 9)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
arr[n][i] = s[i];
n++;
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
subnum = arr[i][j] - '0';
if (subnum != 0)
num[subnum]++;
if (num[subnum] > 1)
{
ans = 1;
cout << "row" << i + 1 << " #" << arr[i][j] << endl;
break;
}
}
for (int j = 0; j < 10; j++)
num[j] = 0;
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
subnum = arr[j][i] - '0';
if (subnum != 0)
num[subnum]++;
if (num[subnum] > 1)
{
ans = 1;
cout << "column" << i + 1 << " #" << arr[j][i] << endl;
break;
}
}
for (int j = 0; j < 10; j++)
num[j] = 0;
}
for (int i = 0; i < 9; i += 3)
{
for (int j = 0; j < 9; j += 3)
{
check = 0;
for (int x = i; x < i + 3; x++)
{
for (int y = j; y < j + 3; y++)
{
subnum = arr[x][y] - '0';
if (subnum != 0)
num[subnum]++;
if (num[subnum] > 1)
{
ans = 1;
check = 1;
cout << "block" << blocknum << " #" << arr[x][y] << endl;
break;
}
}
if (check == 1)
break;
}
blocknum++;
for (int j = 0; j < 10; j++)
num[j] = 0;
}
}
if (ans == 0)
cout << "true" << endl;
return 0;
}

itsa [C_AR06-易]字串在哪裡

#include<iostream>
#include<string>
using namespace std;

int main()
{
int n = 1, ans = 0, x = 0, y = 1;
int num[4] = { 0 };
int f[8][2] = { {0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1} };
string s, s1;
char arr[22][22];
for (int i = 0; i < 22; i++)
for (int j = 0; j < 22; j++)
arr[i][j] = '0';
getline(cin, s);
if (s.length() > 10)
ans = 1;
while (getline(cin, s1))
{
if (s1.length() > 20)
ans = 2;
else
{
for (int i = 0; i < s1.length(); i++)
arr[n][i + 1] = s1[i];
n++;
}
if (n - 1 == s1.length())
break;
}
if (ans >= 1)
cout << "Target Overflow" << endl;
if (ans == 2)
cout << "Array Overflow" << endl;
if (ans == 0)
{
for (int i = 1; i <= s1.length(); i++)
{
for (int j = 1; j <= s1.length(); j++)
{
x = 0;
if (arr[i][j] == s[0])
{
num[0] = i - 1;
num[1] = j - 1;
while (x < 8)
{
for (int d = 1; d < s.length(); d++)
{
if (arr[i + f[x][0] * d][j + f[x][1] * d] != '0' && arr[i + f[x][0] * d][j + f[x][1] * d] == s[y])
{
y++;
if (y == s.length())
{
num[2] = i + f[x][0] * (s.length() - 1) - 1;
num[3] = j + f[x][1] * (s.length() - 1) - 1;
cout << num[0] << ", " << num[1] << " To " << num[2] << ", " << num[3] << endl;
}
}
else
break;
}
y = 1;
x++;
}
}
}
}
}
return 0;
}

itsa [C_AR05-易]最少派車數

#include <iostream>
using namespace std;

int main()
{
int n,s,d,max;
int time[25] = {0};
cin >> n;
max = 0;
for (int i = 0;i < n; i++)
{
cin >> s >> d;
for (int j = s;j < d; j++)
{
time[j]++;
}
for (int k = 0;k < 25; k++)
{
if (time[k] > max)
max = time[k];
}
}
cout << max << endl;
return 0;
}

itsa [C_AR04-易]邊緣偵測

#include<iostream>
using namespace std;

int main()
{
int N,n,m;
int arr[102][102] = {0};
cin >> N;
for (int i = 0;i < N; i++)
{
cin >> n >> m;
for (int a = 1;a <= n; a++)
{
for (int b = 1;b <= m; b++)
{
cin >> arr[a][b];
}
}
if (i > 0)
cout << endl;
for (int a = 1;a <= n; a++)
{
for (int b = 1;b <= m; b++)
{
if (arr[a][b] == 1)
{
if (arr[a - 1][b] == 0 || arr[a + 1][b] == 0 || arr[a][b - 1] == 0 || arr[a][b + 1] == 0)
cout << "0 ";
else
cout << "_ ";
}
else
cout << "_ ";
}
cout << endl;
}
}
return 0;
}

itsa [C_AR03-易]計算陣列中所有元素的立方和

#include<iostream>
using namespace std;

int main()
{
long long sum = 0;
int arr[6];
for (int i = 0;i < 6; i++)
cin >> arr[i];
for (int j = 0;j < 6; j++)
sum += arr[j] * arr[j] * arr[j];
cout << sum << endl;
return 0;
}

itsa [C_AR02-易]一維陣列反轉 II

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
int arr[6] = {0};
int i = 0;
string str;
getline(cin,str);
stringstream ss(str);
while (ss >> arr[i++])
{

}
for (int j = i - 2;j >= 0; j--)
{
if (j < i - 2)
cout << " ";
cout << arr[j];
}
cout << endl;
return 0;
}

itsa [C_AR01-易]一維陣列反轉 I

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
int arr[100] = {0};
int i = 0;
string str;
getline(cin,str);
stringstream ss(str);
while (ss >> arr[i++])
{

}
for (int j = i - 2;j >= 0; j--)
{
if (j < i - 2)
cout << " ";
cout << arr[j];
}
cout << endl;
return 0;
}

C++複雜運算式

【複雜運算式】

成績: 20 / 倒扣: 0.8
問題描述:

複雜運算式可有n個運算子(operator_1..operator_n),及m個運算元(operand_1.. operand_m),1 ≤ n ≤ 30,1 ≤ m ≤ 30,運算元可能是四則運算子(+、-、*、/ )及左右括號,運算元為可以有小數點的正數,例如:(10+ 22.2 )/ 5。請寫一個能夠算出複雜運算式結果的c++程式。

輸入說明:

程式的輸入有k行運算式,1 ≤ k ≤ 30,代表k組測試資料。每一行組輸入一個複雜運算式,運算子與運算元中間不一定有空格隔開。

輸出說明:

輸出運算式的結果到小數點後二位,算式中如果有除以0則輸出NA。
【輸入範例】【輸出範例】
(100 + 20.2) * 10-5
15 / 0 + 3

1197.00
NA

#include<iostream>
#include<string>
#include<sstream>
#include<cstdlib>
#include<iomanip>
#define MAX 80
using namespace std;

void inToPostfix(string*, double&);
int priority(string);
double eval(string*);
double cal(string, double, double);

int main()
{
int j, a;
double sum;
string str, sstr, target = "()+-*/";
unsigned i, pos;
while (getline(cin, str))
{
string infix[MAX] = { "" };
a = 0;
j = 0;
sum = 0;
i = pos = 0;
while ((i = str.find_first_of(target, pos)) != string::npos)
{
str.insert(i + 1, " ");
str.insert(i, " ");
pos += 3;
}
stringstream ss(str);
while (ss >> sstr)
{
infix[j] = sstr;
j++;
}
for (int k = 0; k < j; k++)
{
if (infix[k] == "/" && infix[k + 1] == "0")
a = 1;
}
if (a == 0)
{
inToPostfix(infix, sum);
}
if (a == 0)
cout << fixed << setprecision(2) << sum << endl;
else
cout << "NA" << endl;
}
return 0;
}

void inToPostfix(string *infix, double &sum)
{
string stack[MAX] = { "" }, postfix[MAX] = { "" };
int i, j, top;
i = 0;
j = 0;
top = 0;
while (infix[i] != "")
{
if (infix[i] == "(")
{
stack[++top].clear();
stack[top] = infix[i];
}
else if (infix[i] == "+" || infix[i] == "-" || infix[i] == "*" || infix[i] == "/")
{
while (priority(stack[top]) >= priority(infix[i]))
{
postfix[j].clear();
postfix[j++] = stack[top--];
}
stack[++top].clear();
stack[top] = infix[i];
}
else if (infix[i] == ")")
{
while (stack[top] != "(")
{
postfix[j].clear();
postfix[j++] = stack[top--];
}
top--;
}
else
{
postfix[j].clear();
postfix[j++] = infix[i];
}
i++;
}
while (top > 0)
{
postfix[j].clear();
postfix[j++] = stack[top--];
}
sum = eval(postfix);
}

int priority(string op)
{
if (op == "+" || op == "-")
return 1;
else if (op == "*" || op == "/")
return 2;
else
return 0;
}

double eval(string *postfix)
{
double stack[MAX] = { 0 };
string opnd[2] = { "" };
int top, i;
i = top = 0;
while (postfix[i] != "")
{
if (postfix[i] == "+" || postfix[i] == "-" || postfix[i] == "*" || postfix[i] == "/")
{
stack[top - 1] = cal(postfix[i], stack[top - 1], stack[top]);
top--;
}
else
{
opnd[0].clear();
opnd[0] = postfix[i];
stack[++top] = atof(opnd[0].c_str());
}
i++;
}
return stack[top];
}

double cal(string op, double p1, double p2)
{
if (op == "+")
return p1 + p2;
else if (op == "-")
return p1 - p2;
else if (op == "*")
return p1 * p2;
else if (op == "/")
return p1 / p2;
}

C++簡易運算式

【簡易運算式】

成績: 80 / 倒扣: 0.8
問題描述:

簡易運算式固定有兩個運算子(operator1,operator2),及三個運算元(operand1, operand2, operand3),operator1,operator2可能是+、-、*、/ 等四則運算子,operand1, operand2, operand3為可以有小數點的正數,例如:【10+ 22.2 / 5】。請寫一個能夠算出簡易運算式結果的c++程式。

輸入說明:

程式的輸入包含n+1行,第一行包含一個正整數n,1 ≤ n ≤ 30,代表有n組測試資料。第二行開始有n組輸入,每一組輸入的格式含有兩個運算子及三個運算元,運算子與運算元中間有空格隔開。

輸出說明:

輸出簡易運算式的結果到小數點後二位,算式中如果有除以0則輸出NA。
【輸入範例】【輸出範例】
2
100 + 20.2 * 10
15 / 0 + 3
302.00
NA



#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int n;
double a,c,e,sum;
char b, d;
cin >> n;
for (int i = 0; i < n; i++)
{
sum = 0;
cin >> a >> b >> c >> d >> e;
if ((b == '/' && c == 0) || (d == '/' && e == 0))
cout << "NA" << endl;
else
{
if (b == '+')
{
if (d == '*')
sum = a + c * e;
else if (d == '/')
sum = a + c / e;
else if (d == '+')
sum = a + c + e;
else if (d == '-')
sum = a + c - e;
}
else if (b == '-')
{
if (d == '*')
sum = a - c * e;
else if (d == '/')
sum = a - c / e;
else if (d == '+')
sum = a - c + e;
else if (d == '-')
sum = a - c - e;
}
else if (b == '*')
{
if (d == '*')
sum = a * c * e;
else if (d == '/')
sum = a * c / e;
else if (d == '+')
sum = a * c + e;
else if (d == '-')
sum = a * c - e;
}
else if (b == '/')
{
if (d == '*')
sum = a / c * e;
else if (d == '/')
sum = a / c / e;
else if (d == '+')
sum = a / c + e;
else if (d == '-')
sum = a / c - e;
}
cout << fixed << setprecision(2) << sum << endl;
}
}
return 0;
}