骰子代数(HZNUOJ 2024程序设计基础综合作业 Hard )
2024程序设计基础综合作业 Hard 2:骰子代数(模拟)
[题目链接]( HZNUOJ–2024程序设计基础综合作业 Hard (dup4.cn) )
题目分析:
模拟
这道题是Hard中最简单的没有之一的题。
可能代码量有点,反正注重细节就是,大家慢慢写,哈哈哈
慢慢写,多想,多练
能写到这的你相信这道题不是你的对手
接下来附上多个语言的代码,也仅是提供思路,供参考使用。
代码实现:
c
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int T, i, j, len, k, g, n, m;
char a[200000];
scanf("%d", &T);
getchar();
while (T--)
{
gets(a);
len = strlen(a);
for (i = 0; i < len; i++)
{
if (a[i] == '(' || a[i] == ')')
{
printf("%c", a[i]);
}
else if (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/' && a[i + 1] != 't')
{
printf(" %c ", a[i]);
}
else if (a[i] >= 48 && a[i] <= 58)
{
for (j = i; j < len; j++)
{
if (a[j] >= 48 && a[j] <= 58)
{
}
else
{
break;
}
}
g = 0;
if (a[j] == 'd')
{
if (j - i > 1 || j - i == 1 && a[i] > 49)
{
printf("(");
}
m = 0;
char b[100] = {0};
for (n = j + 1; n < len; n++)
{
if (a[n] <= 58 && a[n] >= 48)
{
b[m] = a[n];
m++;
}
else
{
break;
}
}
for (k = i; k < j; k++)
{
g = a[k] - 48 + g * 10;
}
for (k = 0; k < g; k++)
{
printf("[d%s]", b);
if (k != g - 1)
{
printf(" + ");
}
}
if (j - i > 1 || j - i == 1 && a[i] > 49)
{
printf(")");
}
i = n - 1;
}
else
{
for (k = i; k < j; k++)
{
printf("%c", a[k]);
}
i = j - 1;
}
}
else if (a[i] == 'd')
{
m = 0;
char b[100] = {0};
for (j = i + 1; j < len; j++)
{
if (a[j] <= 58 && a[j] >= 48)
{
b[m] = a[j];
m++;
}
else
{
break;
}
}
printf("[d%s]", b);
i = j - 1;
}
}
printf(" = [Result]\n");
}
}
python
num_cases = int(input())
for _ in range(num_cases):
input_str = input()
input_str += ')'
tokens = []
is_dice = False
current_token = ''
num = 0
result_str = ''
# Process the input string and tokenize
for i in range(len(input_str)):
if input_str[i] == '\t' or input_str[i] == ' ':
continue
else:
current_token += input_str[i]
# Analyze the tokens
for num in range(len(current_token)):
if current_token[num] == '(' or current_token[num] == ')':
if result_str == '':
tokens.append([current_token[num], 'Bracket'])
else:
if is_dice:
tokens.append([result_str, '<dice>'])
else:
tokens.append([result_str, 'Constant'])
result_str = ''
is_dice = False
tokens.append([current_token[num], 'Bracket'])
if current_token[num] == '+' or current_token[num] == '-' or current_token[num] == '*' or current_token[num] == '/':
if result_str == '':
tokens.append([current_token[num], 'Operator'])
else:
if is_dice:
tokens.append([result_str, '<dice>'])
else:
tokens.append([result_str, 'Constant'])
result_str = ''
is_dice = False
tokens.append([current_token[num], 'Operator'])
if '0' <= current_token[num] <= '9':
result_str += current_token[num]
if current_token[num] == 'd':
result_str += current_token[num]
is_dice = True
# Build the final string
final_str = ''
for i in range(len(tokens) - 1):
if tokens[i][1] == '<dice>':
temp_str = ''
for j in range(len(tokens[i][0])):
if tokens[i][0][j] == 'd':
if temp_str == '':
final_str = final_str + '[' + tokens[i][0] + ']'
elif temp_str == '1':
final_str = final_str + '[' + tokens[i][0][1:] + ']'
else:
final_str += '('
for x in range(int(temp_str) - 1):
final_str = final_str + '[' + tokens[i][0][j:] + '] + '
final_str = final_str + '[' + tokens[i][0][j:] + ']' + ')'
break
else:
temp_str += tokens[i][0][j]
elif tokens[i][1] == 'Constant' or tokens[i][1] == 'Bracket':
final_str += tokens[i][0]
elif tokens[i][1] == 'Operator':
final_str = final_str + ' ' + tokens[i][0] + ' '
final_str = final_str + ' = [Result]'
print(final_str)
我是BoBoowen,希望能在acm实验室遇见你!