首页 > 学院 > 开发设计 > 正文

UVa1586 - Molar mass还是画流程图思路清晰

2019-11-10 19:59:57
字体:
来源:转载
供稿:网友

上流程图:

有了流程图,写代码就可以一个分支一个分支地写了:

#define NDEBUG//这个宏的意思是不要debug,交上去的时候要有这一句。//#define LOCAL//定义了这个宏,就是输出到本地,否则是输出到屏幕#include <stdio.h>#include <iomanip>#include <iostream>#include <cmath>#include <ctime>#include <cassert>#include <string>#include <vector>const double C = 12.01, H = 1.008, O = 16.00, N = 14.01;using namespace std;int main(int argc, char* argv[]){#ifdef LOCAL	FILE* newFile;	freopen_s(&newFile, "out.txt", "w", stdout);#endif	cout << setiosflags(ios_base::fixed) << setPRecision(3);	int n; cin >> n;	while (n--)	{		string Word; cin >> word;		double sum = 0;		for (int i = 0; i < word.size(); ++i)		{			int howmany = 0;			if (i + 1 == word.size() || isalpha(word[i + 1]))			{				howmany = 1;				switch (word[i])				{				case 'C':					sum += C * howmany;					break;				case 'H':					sum += H * howmany;					break;					//氧原子				case 'O':					sum += O * howmany;					break;				case 'N':					sum += N * howmany;					break;				}				if (i + 1 == word.size())				{					cout << sum << endl;				}				continue;			}			//得记住读取数字之前的i值啊……			int j = i;			while (i < word.size() && isdigit(word[i + 1]))			{				howmany *= 10;				howmany += word[i + 1] - '0';				++i;			}			switch (word[j])			{			case 'C':				sum += C * howmany;				break;			case 'H':				sum += H * howmany;				break;				//氧原子			case 'O':				sum += O * howmany;				break;			case 'N':				sum += N * howmany;				break;			}			if (i + 1 == word.size())			{				cout << sum << endl;			}			continue;		}	}	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表