Bookmark and Share

free counters
Back to the Training Home

Lesson: 33
Problem: 2520
Title: Quicksum
Difficulty Level: .5
Date: MAR 02, 2009
Site: http://acm.tju.edu.cn/toj/showp2520.html

Site: http://acm.tju.edu.cn/toj/showp2520.html

char str[] = "ABC";
int sum = 0;
for(int i = 0; str[i]; i++) {
   sum += str[i] - 'A';
}

sum = ?, try yourself.

#include<stdio.h>
#include<string.h>
#include<ctype.h>


char input[300];

int getResult() {
	int i, sum = 0;
	for(i = 0; input[i]; i++) {
		if(isalpha(input[i])) {
			sum += (i+1)* (input[i] - 'A' + 1);
		}
	}
	printf("%d\n",sum);
	return 0;
}
int main() {
	while(gets(input)) {
		if(!strcmp(input,"#")) break;
		getResult();
	}
	return 0;
}
Back to the Training Home