UVa1585 Score

本文介绍了一个简单的算法,用于计算一种特殊格式的客观测试答案的得分。这种格式的测试由'O'和'X'组成,'O'代表正确答案,'X'代表错误答案。文章中的算法通过计算连续正确的答案数量来确定每个问题的分数。

原题:

There is an objective test result such as ``OOXXOXXOOO". An `O' means a correct answer of a problem and an `X' means a wrong answer. The score of each problem of this test is calculated by itself and its just previous consecutive `O's only when the answer is correct. For example, the score of the 10th problem is 3 that is obtained by itself and its two previous consecutive `O's.

Therefore, the score of ``OOXXOXXOOO" is 10 which is calculated by ``1+2+0+0+1+0+0+1+2+3".

You are to write a program calculating the scores of test results.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing a string composed by `O' and `X' and the length of the string is more than 0 and less than 80. There is no spaces between `O' and `X'.

Output

Your program is to write to standard output. Print exactly one line for each test case. The line is to contain the score of the test case.

The following shows sample input and output for five test cases.

Sample Input

5 
OOXXOXXOOO 
OOXXOOXXOO 
OXOXOXOXOXOXOX 
OOOOOOOOOO 
OOOOXOOOOXOOOOX


Sample Output

10 
9 
7 
55 
30

代码:

#include <iostream>
#include <cstring>
#define maxn 80
char input[maxn];
int main() {
    using namespace std;
    int T, score, count;
    if (scanf("%d",&T)==1) {
        //cin.get();
        while (T--) {
            scanf("%s",input);
            score= 0;
            count= 0;
            for (int i= 0; input[i]!='\0'; i++) {
                if(input[i]=='O') count++;
                else count= 0;
                score+= count;
            }
            printf("%d\n",score);
        }
    }
    
    return 0;
}



### 镜像字符的定义 镜像字符是指在特定规则下,每个字符对应一个“镜像”字符。常见的一种规则是字母表反转后对应的位置字母,例如 ‘a’ 的镜像是 ‘z’,‘b’ 的镜像是 ‘y’ 等 [^1]。在一些特定题目(如uva401镜像字符串问题)中,还会定义数字和字母各自对应的镜像字符,像数字 “1” 的镜像是 “1”,字母 “A” 的镜像是 “A” 等 [^5]。 ### 镜像字符的应用 - **数据加密**:可以利用镜像字符的规则对明文进行转换,增加数据的保密性。例如将原字符串中的字符替换为其镜像字符,在解密时再反向操作。 - **字符串校验**:判断一个字符串是否为镜像字符串,可用于数据的完整性检查或特定格式的验证。如在uva401题目中,通过判断输入字符串是否为镜像字符串和回文串来进行分类 [^5]。 - **图形显示**:在图形界面中,可能会利用镜像字符实现一些特殊的显示效果,如将文字进行镜像显示 [^4]。 ### 镜像字符的实现方法 #### Go语言计算字符串镜像分数 以下是使用Go语言计算字符串镜像分数的示例代码: ```go package main import ( "fmt" ) func mirrorScore(s string) int { score := 0 for _, char := range s { if char >= 'a' && char <= 'z' { mirror := 'z' - (char - 'a') score += int(mirror - 'a' + 1) } else if char >= 'A' && char <= 'Z' { mirror := 'Z' - (char - 'A') score += int(mirror - 'A' + 1) } } return score } func main() { s := "abc" fmt.Println(mirrorScore(s)) } ``` #### Python使用`translate`方法创建镜像字符串 在Python中可以使用`translate`方法根据转换表创建镜像字符串: ```python import string # 创建转换表 translation_table = str.maketrans(string.ascii_lowercase, string.ascii_lowercase[::-1]) s = "abc" mirror_string = s.translate(translation_table) print(mirror_string) ``` #### C语言实现字符串镜像 以下是C语言实现字符串镜像的示例代码: ```c #include <stdio.h> #include <string.h> int Reverse(char z[200]) { int i, j, t; i = strlen(z); for (j = 0; j < i / 2; j++) { t = z[j]; z[j] = z[i - j - 1]; z[i - j - 1] = t; } return 0; } int main() { char a[200]; printf("请输入一个字符串"); scanf("%s", a); char b[200]; strcpy(b, a); Reverse(b); strcat(a, b); printf("镜像字符串为%s\n", a); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值