Skip to content

11359: 【原1359】帮琪露诺加密

题目

题目描述

author: SSY 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1359

Description

大家都知道,琪露诺不擅长算术。可是大小姐丁姐硬要她加密一串字符串。加密的方法倒也很简单,假设A=1,B=2,以此类推;a=27,b=28以此类推;空格=0。对于输入的字符串,加密的结果就是字符串每一位对应的数值乘以这一位下标之和,下标从1开始。比如ACEe加密的过程如下:

ACEeA加密为:1×1 + 2×3 + 3×5 + 4×31 + 5×1= 151

可是就算这么简单,琪露诺还是无所适从,请你帮帮她。

Input Format

一行字符串,保证只包含大小写字母和空格。字符串长度不超过1000,并且首位和末位不是空格。

Output Format

一个数字,表示加密结果。

Sample Input

Welcome to SJTU

Sample Output

2748

BugenZhao's solution

//
// Created by BugenZhao on 2019/3/16.
//
// 编程练习

#include <iostream>
#include <cctype>

using namespace std;

int main() {
    string str;
    getline(cin, str);
    long long ans = 0;
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] == ' ') continue;
        if (isupper(str[i]))
            ans += (str[i] - 'A' + 1) * (i + 1);
        else if (islower(str[i]))
            ans += (str[i] - 'a' + 27) * (i + 1);
    }
    cout << ans << endl;
}

FineArtz's solution

/* 帮琪露诺加密 */
#include <iostream>
using namespace std;

int main(){
    char ch;
    int ans = 0, i = 0;
    while (cin.get(ch)){
        ++i;
        int p = 0;
        if ((ch >= 'A') && (ch <= 'Z')) p = ch - 'A' + 1;
        if ((ch >= 'a') && (ch <= 'z')) p = ch - 'a' + 27;
        ans += p * i;
    }
    cout << ans << endl;
    return 0;
}

q4x3's solution

/**
 * 模拟
 * 处理空格
 **/
#include <iostream>
using namespace std;
int main()
{
    string s;
    int ans = 0;
    getline(cin, s);
    for (int i = 0;i < s.length();++ i) {
        int tmp = s[i] - 'A' + 1;
        if (tmp > 26) tmp = tmp - 6;
        if (tmp < 0) tmp = 0;
        ans += tmp * (i + 1);
    }
    cout << ans << endl;
}

skyzh's solution

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int enc[256] = { 0 };
    char msg[1000];

    for (int i = 0; i < 26; i++) {
        enc['A' + i] = i + 1;
    }
    for (int i = 0; i < 26; i++) {
        enc['a' + i] = i + 27;
    }

    cin.getline(msg, 1000);
    int l = strlen(msg);
    int result = 0;

    for (int i = 0; i < l; i++) {
        result += (i + 1) * enc[msg[i]];
    }

    cout << result << endl;

    return 0;
}

victrid's solution

#include <cstring>
#include <iostream>

using namespace std;

int pharse(char chz) {
    if (chz == ' ')
        return 0;
    return chz - 'A' >= 26 ? chz - 'a' + 27 : chz - 'A' + 1;
}
int main() {
    string chs;
    int ans = 0;
    getline(cin, chs);
    for (int i = 0; chs[i] != '\0'; i++) {
        ans += (i + 1) * pharse(chs[i]);
    }
    cout << ans;
    return 0;
}

yyong119's solution

#include <cstdio>
using namespace std;
char ch;
int ans, cnt;
int main() {
    ch = getchar();
    while (ch != '\n') {
        ++cnt;
        if (ch >= 'A' && ch <= 'Z')
            ans += cnt * (ch - 'A' + 1);
        else if (ch >= 'a' && ch <= 'z')
            ans += cnt * (ch - 'a' + 27);
        ch = getchar();
    }
    printf("%d", ans);
    return 0;
}