杭电OJ | 2734 Quicksum 用gets输入

本文介绍了一种计算字符串Quicksum的算法,该算法通过将每个字符转换为对应的数值并乘以其位置权重来计算总和。示例展示了如何使用C语言实现此算法,并提供了输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2734

Problem Description

ACM: 1*1 + 2*3 + 3*13 = 46MID 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             The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 255 characters.

Output           For each packet, output its Quicksum on a separate line in the output.

Sample Input

ACM

MID CENTRAL

#

Sample Output

46

650

要点

gets(input) && input!=NULL && input[0]!='#'  //以#结尾要记得这么写!
#include <stdio.h>
#include <string.h>
int main(){
	char input[255];
	while(gets(input)&&input!=NULL&&input[0]!='#'){
		int i;
		int sum = 0;
		for(i=0;i<strlen(input);i++){
			int nownum;
			if(input[i]==' '){
				nownum = 0;
			}
			else{
				nownum = input[i]-'A'+1;
			}
			sum = sum + nownum*(i+1);
		}
		printf("%d\n",sum);
	}
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值