zoukankan      html  css  js  c++  java
  • Quicksum

    时空限制

    Time Limit:1000ms

    Resident Memory Limit:1024KB

    Output Limit:1024B

    题目内容

    A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting transmission errors, validating document contents, and in many other situations where it is necessary to detect undesirable changes in data.

    For this problem, you will implement a checksum algorithm called Quicksum. A Quicksum packet allows only uppercase letters and spaces. It always begins and ends with an uppercase letter. Otherwise, spaces and letters can occur in any combination, including consecutive spaces.

    A Quicksum is the sum of the products of each character’s position in the packet times the character’s value. A space has a value of zero,while letters have a value equal to their position in the alphabet. So, A=1, B=2, etc., through Z=26. Here are example Quicksum calculations for the packets “ACM” and “MID CENTRAL”:

    ACM:1*1+2*3+3*13=46

    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

    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.

    题目来源

    Mid-Central USA 2006

    解题思路

    本题要求计算一个数据包(及一行字符串)的Quicksum。所谓Quicksum,就是指一行字符串(数据包)中每个字符的位置与该字符的值的乘积相加的结果。一个数据包占一行,仅由大写字母和空格组成;位置由1开始计数,空格也占一个位置;A~Z的值对应为1~26,空格的值为0.

    本题的难点就是数据的读入。可采用cin.getline()一行一行读入数据;也可以采用cin.get()一个一个读入字符,但需要注意的是,cin.get()不会忽略任何字符,对于回车符需要单独处理。

    流程分析

    (1)定义一个字符型变量作为从键盘读入的字符;定义一个整形变量作为字符的位置,初始化为1;定义一个整形变量作为Quicksum,初始化为0

    (2)对每个从键盘读入的字符,首先判断其是否为“#”,若为“#”则表示读完全部输入,用break;语句跳出读入循环。然后判断其是否为回车’ ’,若不是则没读完一行,判断其是否为空格字符’ ‘,若非空格字符则计算当前的Quicksum=之前的Quicksum+字符位置*字符值,其中字符值为字符型变量-64,然后将字符位置加一。若为回车’ ’,则输出当前的Quicksum,然后将Quicksum重置为0,字符位置重置为1

    参考代码

    #include <fstream>
    #include <iostream>
    using namespace std;
    int main(int argc,char * argv[])
    {
        char ch;
        int i=1;
        int sum=0;
        while(cin.get(ch))
        {
            if(ch=='#') break;
            if(ch!='
    ')
            {
                if(ch!=' ') sum=sum+i*(ch-64);
                i++;
            }
            if(ch=='
    ')
            {
                cout<<sum<<endl;
                sum=0;
                i=1;
            }
        }
        system("pause");
        return 0;
    }
    

    运行结果

  • 相关阅读:
    MVC3 Razor模板引擎
    Razor引擎学习:RenderBody,RenderPage和RenderSection
    Lambda表达式详解
    MVC的重定向页面的跳转
    dataSet==>Ilist<>的函数封装
    shell 判断目录还是文件
    大写金额转小写(千万以下)
    python将有序列表转乱序,模拟音乐播放器随机播放列表
    ssh登录远程linux服务器的错误
    ubuntu Unable to locate package错误解决办法
  • 原文地址:https://www.cnblogs.com/cysolo/p/3381515.html
Copyright © 2011-2022 走看看