首页 > 其他 > 详细

URAL 1823. Ideal Gas 数学,分类

时间:2015-03-15 19:49:13      阅读:274      评论:0      收藏:0      [点我收藏+]


1823. Ideal Gas

Time limit: 0.5 second
Memory limit: 64 MB
Many of you know the universal method of solving simple physics problems: you have to find in a textbook an identity in which you know the values of all the quantities except for one, substitute the numbers into this identity, and calculate the unknown quantity.
This problem is even easier. You know right away that the identity needed for its solution is the Clapeyron–Mendeleev equation for the state of an ideal gas. This equation relates the pressure of an ideal gas p, the amount of substance n, the volume occupied by the gas V, and the temperature T. Given three of these quantities, you have to find the fourth quantity. Note that the temperature of a gas and the volume occupied by it must always be positive.

Input

Each of the three input lines has the form “X = value”, where X is the symbol for a physical quantity and value is a nonnegative integer not exceeding 1000. The three lines specify the values of three different quantities. Pressure is specified in pascals, amount of substance in moles, volume in cubic meters, and temperature in kelvins. It is guaranteed that the temperature and volume are positive. The universal gas constant R should be taken equal to 8.314 J / (mol · K).

Output

If the input data are inconsistent, output the only line “error”. If the value of X can be determined uniquely, output it in the format “X = value” with an accuracy of 10?3. If it is impossible to uniquely determine the value of X, output the only line “undefined”.

Sample

input output
p = 1
n = 1
V = 1
T = 0.120279

Notes

Recall that Pa = N / m2 and J = N · m.


高中学的:pV=nRT,即理想气体状态方程

题意:知道三个变量求剩下一个变量。其中R是已知常量=8.314。其中V,T必须要大于0。如果出现矛盾就输出error。如果出现不确定的情况输出undefined。


分类:以求V为例子。如果p和n 有只一个为0 那T和V中也必须有个是等于0的。所以和题意矛盾。输出error。  如果p,n同时为0,那么就无法求出V是多少。输出undefined。  其他情况直接计算即可。




#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; 
#include <vector> 

int main()
{ 
	int data[5];
	char p[3];
	string tem;
	int flag=1;
	memset(data,-1,sizeof data);
	for(int i=0;i<3;i++)
	{
		int num;
		cin>>p[i];
		cin>>tem;
		cin>>num;
		if(p[i]=='p')
			data[0]=num;
		if(p[i]=='V')
			data[1]=num;
		if(p[i]=='n')
			data[2]=num;
		if(p[i]=='T')
			data[3]=num;
	} 

	if(data[1]==0||data[3]==0)
	{
		puts("error");
		return 0;
	}  

	int wei;
	for(int i=0;i<4;i++)//找出未知数
	{
		if(data[i]==-1)
			wei=i;
	} 
	double r=8.314;
	if(wei==0)//计算p   data[1]不会等于0 所以不用多做考虑
	{
		printf("p = %lf\n",r*(data[2]*data[3])/data[1]);
	}
	else if(wei==1)//计算V
	{
		if(data[0]==0&&data[2]==0)//如果等式上下都是0 那么就是无法确定, 
			puts("undefined"); 
		else if(data[0]==0||data[2]==0)//如果p等于0 n不等于0  那T必须是0  矛盾。反之,V不能是0 也矛盾
			puts("error"); 
		else
			printf("V = %lf\n",r*(data[2]*data[3])/data[0]);
	}
	else if(wei==2)//计算n
	{
		printf("n = %lf\n",1.0*(data[0]*data[1])/data[3]/r);
	}
	else if(wei==3)//计算T
	{ 
		if(data[0]==0&&data[2]==0) //如wei==1
			puts("undefined");  
		else if(data[0]==0||data[2]==0)
			puts("error"); 
		else
			printf("T = %lf\n",1.0*(data[0]*data[1])/data[2]/r);
	}
	return 0;
}
/*
T = 1
n = 0
p = 0
*/


URAL 1823. Ideal Gas 数学,分类

原文:http://blog.csdn.net/u013532224/article/details/44279639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!