zoukankan      html  css  js  c++  java
  • sicily 1388. Quicksum

    Description

    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.
     
    输出是每个字母的号码(A~Z分别为1~26,空格为0)* 该字母位置之和。利用char类型的存储特性很容易写出来。
    需要注意的是数组的长度要比需要的大一点,不然会报RF(虽然很奇怪,不过提示说可能和RTE是一样的),第一次设成255的时候就报了,再改成256就AC,应该是数组溢出?
     
    发现自己养成了一个习惯,标记控制的部分用while,计数器控制的部分用for:
    前者不需要步长,用for的话头会出现空语句不好看;后者把控制条件全写在头里,比while更加直观。
    else里的语句其实可以是空的,不过写出来可能也比较好看一点,但是懒得再乘i+1了(咦) 给自己看就好
     
    View Code
     1 #include<stdio.h>
     2 int main()
     3 {
     4     int i, total = 0;
     5     char letter[256];
     6     
     7     while ( scanf("%c", &letter[0] )&& letter[0] != '#' )
     8     {
     9         total = total + letter[0] - 64;
    10         
    11         for ( i = 1; scanf("%c", &letter[i]) && letter[i] != '\n'; i++ )
    12         {
    13             if ( letter[i] != ' ' )
    14             {
    15                 total = total + ( letter[i] - 64 ) * ( i + 1 );
    16             }
    17             else
    18             {
    19                 total = total + 0;
    20             }
    21         }
    22         
    23         printf("%d\n", total);
    24         
    25         total = 0;
    26         
    27     }
    28     
    29     return 0;
    30 }
  • 相关阅读:
    pytorch-卷积基本网络结构-提取网络参数-初始化网络参数
    pytorch-mnist神经网络训练
    python 面对对象 类(继承, 多态)
    注意机制CBAM
    python sqlalchemy 进行 mysql 数据库操作
    python pymysql 连接 mysql数据库进行操作
    mysql数据库基础
    python正则表达式解析(re)
    python 装饰器 (test = submit(test))
    对opencv读取的图片进行像素调整(1080, 1920) 1.cv2.VideoCapture(构造图片读取) 2.cv2.nameWindow(构建视频显示的窗口) 3.cv2.setWindowProperty(设置图片窗口的像素) 4.video_capture(对图片像素进行设置)
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2764963.html
Copyright © 2011-2022 走看看