【发布时间】: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时才重置count。your basic logic:p = true` 中还有一个错误,并且 p = false` 不应该出现在 if/else stalements 中,因为这意味着您只考虑最后一次测试的结果。l 以p = true开头并设置@ 987654331@ 当其中一个字母计数不匹配时。一旦发生这种情况,总体结果必须是false,并且无法再次将其恢复为true。 -
...而且,法官不会期望在输出中得到
"\nEnter the string:"- 将被视为错误答案的一部分。