⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1793.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 1793 on 2006-09-12 at 20:26:32 */ 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const char NUM[][10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
							"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", 
							"nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", 
							"hundred", "thousand", "million", "" };
const char NEG[] = "negative";
const int VAL[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 
					60, 70, 80, 90, 100, 1000, 1000000 };
const int SIG[] = { 1000000, 1000, 100 };
const int N = 128;

int getInt(int*, int*);

int main()
{
	int d[N], n;
	
	while(true) {
		bool neg = false;
		char num[N];
		for(n = 0; true; n++) {
			if(scanf("%s", num) == EOF) return 0;
			if(!strcmp(NEG, num)) { neg = true; n--; continue; }
			for(int k = 0; true; k++)
				if(!strcmp(NUM[k], num)) { d[n] = VAL[k]; break; }
			if(getchar() == '\n') break;
		}
		int v = getInt(d, d+n+1);
		if(neg && v != 0) printf("-");
		printf("%d\n", v);
	}
}

int getInt(int* b, int* e)
{
	int r = 0;
	for(int i = 0; i < 3; i++)
		for(int *j = b; j != e; j++)
			if(*j == SIG[i]) 
				{ int v1 = getInt(b, j), v2 = getInt(j+1, e); return v1*SIG[i]+v2; }
	for(int *i = b; i != e; i++) r += *i;
	return r;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -