POJ3094 快速和
本题每行输入一行以大写字母开头的,只包含大写字母与空格的字符串,字符数<=255个。要求转化为一串数字和。转化算法为:对于S中的每一位字符,该字符的值乘以其位置然后加到总和中去。但是空格的值为0,字母的值为1-26.A=1,B=2,C=3,D=4,,,Z=26。
如ACF E = 1*1+2*3+3*6+4*0+5*5=50.
输入:每行为不超过255个字符的字符串,以单个#表示结束。
输出:每行字符串表示的和。
代码:
#include <cstring>
#include <cstdio>
using namespace std;
char s[300];
int main()
{
while(gets(s)&&s[0]!='#')
{
int sum=0;
for(int i=0;i<strlen(s);i++)
{
if(s[i]>='A'&&s[i]<='Z')
sum +=(s[i]-'A'+1)*(i+1);
}
printf("%d\n",sum);
}
return 0;
}