牛客网考研机试题集合:A+B

考点:字符串的分割

方法一:使用strtok(,)函数

#include<bits/stdc++.h>
using namespace std;
const int MAXSIZE=10000;

int s2i(char *p);
int add(char s[]);

int main() {
	char s[100];
	while(gets(s)) {
		printf("%d\n",add(s));
	}
	return 0;
}
int s2i(char p[]) {
	int r;
	if(strcmp(p,"one")==0) {
		r=1;
	} else if(strcmp(p,"two")==0) {
		r=2;
	} else if(strcmp(p,"three")==0) {
		r=3;
	} else if(strcmp(p,"four")==0) {
		r=4;
	} else if(strcmp(p,"five")==0) {
		r=5;
	} else if(strcmp(p,"six")==0) {
		r=6;
	} else if(strcmp(p,"seven")==0) {
		r=7;
	} else if(strcmp(p,"eight")==0) {
		r=8;
	} else if(strcmp(p,"nine")==0) {
		r=9;
	} else {
		r=0;
	}
	return r;
}
int add(char s[]) {
	int ret,a=0,b=0,p1;
	vector<char *> tmp;
	char *p;
	p=strtok(s," ");
	while(p) {
		if(*p=='+') {
			p1=tmp.size();
		}
		tmp.push_back(p);
		p=strtok(NULL," ");
	}
	int len=tmp.size();
	for(int i=0; i<len; i++) {
		if(i<p1) {
			//printf("%s,%d\n",tmp[i],s2i(tmp[i]));
			a=a*10+s2i(tmp[i]);
		} else if(i<len-1&&i>p1) {
			//printf("%s,%d\n",tmp[i],s2i(tmp[i]));
			b=b*10+s2i(tmp[i]);
		}

	}
//	printf("a=%d,b=%d\n",a,b);
	ret=a+b;
	return ret;
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值