溶液模拟器描述小谢虽然有很多溶液,但是还是没有办法配成想要的溶液,因为万一倒错了就没有办法挽回了。因此,小谢到网上下载了一个溶液配置模拟器。模拟器在计算机中构造一种虚拟溶液,然后可以虚拟地向当前虚拟溶液中加入一定浓度、一定体积的这种溶液,模拟器会快速地算出倒入后虚拟溶液的浓度和体积。当然,如果倒错了可以撤销。模拟器的使用步骤如下:1)为模拟器设置一个初始体积和浓度V0、C0%。2)进行一系列操作,模拟器支持两种操作:P(v,c)操作:表示向当前的虚拟溶液中加入体积为v浓度为c的溶液;Z操作:撤销上一步的P操作。(P,Z均为大写字母)输入第一行两个整数,表示V0和C0,0≤C0≤100;第二行一个整数n,表示操作数,n≤10000;接下来n行,每行一条操作,格式为:P_v_c或Z。其中_代表一个空格,当只剩初始溶液的时候,再撤销就没有用了。任意时刻质量不会超过2^31 -1。输出n行,每行两个数Vi,Ci,其中Vi为整数,Ci为实数(保留5位小数)。其中,第i行表示第i次操作以后的溶液体积和浓度。输入样例 1 100 1002P 100 0Z输出样例 1200 50.00000100 100.00000来源
代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
double v[10002],c[10002];
string str;
cin >> v[0] >> c[0]; //初始值
int n;
cin >> n;
int k = 0;
for(int i=1;i<=n;i++)
{
double c1,v1;
cin >> str;
if(str[0]=='P')
{
cin >> v1 >> c1;
v[k+1] = v1 + v[k];
c[k+1] = (v1*c1 + v[k]*c[k])/v[k+1];
cout << fixed << setprecision(5) << v[k+1] <<" " << c[k+1] << endl;
k++;
}else
{
if(k != 0)
{
k--;
}
cout << fixed << setprecision(5) << v[k] <<" "<< c[k] << endl;
}
}
return 0;
}
【以下回答由 GPT 生成】
我会使用Python来解决该问题。下面是解决该问题的代码实现:
def simulate_solution(V0, C0, operations):
solution = [(V0, C0)] # 存储溶液的体积和浓度
for op in operations:
if op[0] == 'P':
v, c = op[1], op[2] # 获取溶液的体积和浓度
last_volume, last_concentration = solution[-1] # 获取上一次操作后的体积和浓度
new_volume = last_volume + v # 计算新的体积
new_concentration = (last_volume * last_concentration + v * c) / new_volume # 计算新的浓度
solution.append((new_volume, new_concentration)) # 将新的体积和浓度添加到溶液列表中
elif op[0] == 'Z':
if len(solution) > 1:
solution.pop() # 撤销上一次的操作
return solution
# 获取输入
V0, C0 = map(int, input().split()) # 获取初始体积和浓度
n = int(input()) # 获取操作数
operations = []
for _ in range(n):
operation = input().split() # 获取操作
if operation[0] == 'P': # 如果是P操作
v, c = int(operation[1]), float(operation[2]) # 获取溶液的体积和浓度
operations.append(('P', v, c))
elif operation[0] == 'Z': # 如果是Z操作
operations.append(('Z',))
# 使用模拟器计算溶液配置
results = simulate_solution(V0, C0, operations)
# 输出结果
for volume, concentration in results:
print(f"{volume} {concentration:.5f}")
请注意,由于我无法运行下载的溶液配置模拟器,因此我无法验证上述代码的正确性。该代码根据输入的操作序列模拟生成溶液的体积和浓度,并输出每次操作后的结果。
【相关推荐】