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


知识点: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,打表...

相关文章:

  • 2021-07-29
  • 2021-12-29
  • 2022-12-23
  • 2021-11-24
  • 2022-02-17
  • 2021-08-10
  • 2021-12-31
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2021-11-04
  • 2021-10-28
  • 2021-06-04
  • 2022-01-10
相关资源
相似解决方案