题意及思路

题意:输入一行字符串,只含有空格或者大写英文字母(空格不会出现在首尾处),要你求这一串字符串的sum。给定 空格=0,A=1 . . . Z=26。

思路:rs += (i+1)*(cts[i]-64); 每次循环执行。(空格:rs+=0;)

踩坑点:scanf读取%s时,遇到空格就会跳过,读不到题意所要求的的形式,所以我们需要使用gets()来读取。 另外,strlen()是求字符串的实际长度的。 A的ASCII码为65。


代码

#include<stdio.h> #include<string.h> int Quicksum(char cts[],int len); int main(){ char cts[256]; int rs; while(gets(cts)){ if(cts[0]=='#') break;
        rs = Quicksum(cts,strlen(cts));
        printf("%d\n",rs);
    } return 0;
} int Quicksum(char cts[],int len){ int rs=0; for(int i=0;i<len;i++){ if(cts[i]==' ') {
            rs+=0; continue;
        }
        rs += (i+1)*(cts[i]-64);
    } return rs;
}