Description
给出一个只由大写字母和空格组成字符串,每个字符有对应的权值,权值=字母次序*字母位置,空格权值为0,例如MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 + 11*12 = 650
Input
给出一堆字符串,以#结束输入
Output
对于每个字符串,输出其对应权值
Sample Input
ACM
MID CENTRAL
REGIONAL PROGRAMMING CONTEST
ACN
A C M
ABC
BBC
#
Sample Output
46
650
4690
49
75
14
15
Solution
水题,注意因为字符串中有空格所以用gets()输入
Code
#include<stdio.h>
#include<string.h>
int main()
{
char s[500];
int len,sum,i;
while(gets(s))
{
if(strcmp(s,"#")==0)
break;
sum=0;
len=strlen(s);
for(i=0;i<len;i++)
if(s[i]>='A'&&s[i]<='Z')
sum+=(s[i]-'A'+1)*(i+1);
printf("%d\n",sum);
}
return 0;
}