字符距离类似题

有道笔试题,是要求输入3个字符,要求在a,b,c,d,e,f,g之间,然后

a,b,c,d,e,f,g等于:

1,2,3,4,5,6,7

规则:(1)输入的三个字符中,中间的大,输出为两边之和;否则为3个之和。

例如:输入是a,b,c;输出是a+b+c即:1+2+3=6;c,f,b=c+b=3+2=5

也就是输入的某个字符char代表的数字为:char-'a'+1

// Note:Your choice is C++ IDE
#include <iostream>
#include <vector>
using namespace std;
int main()
{
char m;
vector<char>vec;
while(cin>>m)//输入的所有都压进去
{
vec.push_back(m);
}
if(vec.size()!=3)判断输入的个数是否等于3
{
cout<<"number count not equal 3"<<endl;
return 0;
}
else//满足3个数
{
for(int i=0;i<=2;++i)
{
if(vec[i]<'a' || vec[i] >'g')//范围超了,断掉
{
cout<<"the number is out of a to g"<<endl;
return 0;
}
}

if(vec[1]>vec[0] && vec[1]>vec[2])//中间大
{ cout <<int(vec[0]-'a'+vec[2]-'a')+2<<endl; return 0;}
else
{ cout <<int(vec[0]-'a'+vec[1]-'a'+vec[2]-'a')+3<<endl; return 0;}
}

return 0;
}

原文地址:https://www.cnblogs.com/beihaidao/p/8579356.html