【问题标题】:Ascii String Converter in CC语言中的ASCII字符串转换器
【发布时间】:2015-08-06 11:53:05
【问题描述】:

我是做这类问题的初学者。 下面是一个问题,也是我根据给定问题尝试开发的代码。我看到每当我添加输入字符串时,它都不会继续进行。 问题是:

超级 ASCII 字符串检查器: 在 Byteland 国家,当且仅当字符串中每个字符的计数等于其 ascii 值时,才称字符串“S”为超级 ascii 字符串。 在 Byteland 国家的 ascii 代码中,'a' 是 1,'b' 是 2 ...'z' 是 26。 你的任务是找出给定的字符串是否是超级 ascii 字符串。 输入格式: 第一行包含测试用例的数量 T,然后是 T 行,每行包含一个字符串“S”。 输出格式: 对于每个测试用例,如果字符串“S”是超级ASCII,则打印“是”,否则打印“否” 约束: 1

这就是我所做的:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

void main()
{
    char s[40];
    int i,j,count=1;
    bool p;
    printf("\nEnter the string:");
    scanf("%s",s);
    int n = strlen(s);
    for(i=0; i<n;i++){
        for(j=0;j<n;j++){
            if(s[i]==s[j+1])
                count++;
               }
            int asc = toascii(s[i])-96;
            if(asc == count){
                p = true;
                count=0;
            }
            else
                p=false;
    }
    if(p)
        printf("Yes, the given string is super string");
    else
        printf("No, it ain't a super string");
}

【问题讨论】:

  • 注:void main() --> int main(void)
  • 注2:scanf("%s",s); --> scanf("%39s",s);
  • 而且由于详细输出不符合问题的要求,法官也会使您的回答失败。但你最好加上newline
  • 只有在count == asc 时才重置countyour basic logic: p = true` 中还有一个错误,并且 p = false` 不应该出现在 if/else stalements 中,因为这意味着您只考虑最后一次测试的结果。l 以p = true 开头并设置@ 987654331@ 当其中一个字母计数不匹配时。一旦发生这种情况,总体结果必须是false,并且无法再次将其恢复为true
  • ...而且,法官不会期望在输出中得到"\nEnter the string:" - 将被视为错误答案的一部分。

标签: c string ascii


【解决方案1】:

使用测试字符串是否为超级字符串的函数拆分代码会更简单,因为只要一个字母的计数不等于值,您就知道可以立即返回 false。根据您的逻辑,这意味着您应该拥有:

        else {
            p=false;
            break;
        }

并将愚蠢的count=1初始化替换为count=0

这里是固定版本(无法测试,我的编译器没有stdbool.h)

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isSuperString(const char *s)
{
    int i,j;
    int n = strlen(s);
    for(i=0; i<n;i++){
        int count=0;
        for(j=0;j<n;j++){
            if(s[i]==s[j])
                count++;
            }
            int asc = s[i]-96;
            if(asc != count){
                return false;
            }
    }
    return true;
}

int main()
{
    char s[401];
    printf("\nEnter the string:");
    scanf("%400s",s);
    if(isSuperString(s))
        printf("Yes, the given string is super string");
    else
        printf("No, it ain't a super string");
    return 0;
}

但被问到的主要是:

int main()
{
    int n, i, cr;
    char s[401];                    // max 400 characters in input
    cr = scanf("%d", &n);           // always control input
    if (cr != 1) {
        fprintf(stderr, "Error at first line\n");
        return 1;
    }
    for (i=0; i<n; i++) {
        cr = scanf("%400s",s);      // always limit input strings
        if (cr != 1) {
            fprintf(stderr, "Error at test line %d\n", i+1);
            return 1;
        }
        if(isSuperString(s))
            printf("Yes %s\n", s);
        else
            printf("NO  %s\n", s);
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    由于您的代码与所需逻辑不符,我会给您一些指示。

    您需要计算每种类型的字母数量。这意味着您需要 26 个计数器,每种类型的字母一个。我推荐使用数组,例如int counters[26];

    对于要测试的每个字符串“S”,您需要首先将所有计数器设置为 0。

    然后循环遍历字符串,并为每个字母增加该字母的计数器。

    最后,遍历计数器并检查它们是否等于它们的值。请注意,0 也是可以接受的(该字母根本不出现在字符串中)。 如果任何字母未通过测试,则打印“No\n”,否则打印“Yes\n”。

    继续下一个字符串。

    【讨论】:

    • 这是我第一次喜欢这种关于 SO 的回答方法。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多