题解 P1765 【手机_NOI导刊2010普及(10)】

说实话,打表真的很累!
所以小金羊又开始暴力出奇迹了!
这个题解适合初学者使用。


知识点:string里面的str.find()函数:
可以查找字符串和字符,有就返回位置(开头是0),
没有就返回string::npos(unsigned int npos=-1)。
所以就可以开始微型打表微型暴力了:
Code:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string str[10]={"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"," "};
int main()
{
	int count=0,i;
	char inp;
	while (scanf("%c",&inp)!=EOF)
	{//据说会吞回车
		for(i=1;i<=9;i++)
		{//从九个预设字符串里找吧
			if (str[i].find(inp,0)!=string::npos)
			{//如果在这个预设字符串内,
				count=count+str[i].find(inp,0)+1;
                break;
			}//位置+1就是需要按的次数。
		}
	}
	printf("%d",count);
	return 0;
}

效果明显,秒杀if,case,打表...

posted @ 2019-02-15 21:06  小金羊  阅读(228)  评论(0)    收藏  举报