C 中的 Ascii 字符串转换器
Ascii String Converter in C
我是做这类题的初学者。
下面是一个问题,也是我试图根据给定问题开发的代码。我看到每当我添加输入字符串时,它都不会继续进行。
问题是:
超级 ASCII 字符串检查器:
在 Byteland 国家/地区,当且仅当字符串中每个字符的计数等于其 ascii 值时,字符串 "S" 才被称为超级 ascii 字符串。
在 Byteland 国家的 ascii 码中 'a' 是 1,'b' 是 2 ...'z' 是 26。
你的任务是找出给定的字符串是否是一个超级 ascii 字符串。
输入格式:
第一行包含 T 个测试用例,后面是 T 行,每行包含一个字符串 "S"。
输出格式:
对于每个测试用例,如果字符串 "S" 是超级 ascii,则打印 "Yes",否则打印 "No"
约束:
1<=T<=100
1<=|S|<=400,S 将只包含小写字母 ('a'-'z').
这是我所做的:
#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");
}
由于您的代码不符合要求的逻辑,我会给您一些指导。
您需要计算每种类型的字母数。这意味着您需要 26 个计数器,每种类型的字母一个。我建议使用数组,例如 int counters[26];
对于每个要测试的字符串 "S",您需要先将所有计数器设置为 0。
然后遍历字符串,并为每个字母增加该字母的计数器。
最后,遍历计数器并检查它们是否等于它们的值。请注意,0 也是可以接受的(该字母根本不出现在字符串中)。
如果任何字母未通过测试,则打印 "No\n" 否则打印 "Yes\n".
继续下一个字符串。
用一个函数来拆分你的代码会更简单,这个函数会测试一个字符串是一个超级字符串,因为一旦一个字母的计数不等于它的值,你就知道你可以立即 return 错误。根据您的逻辑,这意味着您应该拥有:
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;
}
我是做这类题的初学者。 下面是一个问题,也是我试图根据给定问题开发的代码。我看到每当我添加输入字符串时,它都不会继续进行。 问题是:
超级 ASCII 字符串检查器: 在 Byteland 国家/地区,当且仅当字符串中每个字符的计数等于其 ascii 值时,字符串 "S" 才被称为超级 ascii 字符串。 在 Byteland 国家的 ascii 码中 'a' 是 1,'b' 是 2 ...'z' 是 26。 你的任务是找出给定的字符串是否是一个超级 ascii 字符串。 输入格式: 第一行包含 T 个测试用例,后面是 T 行,每行包含一个字符串 "S"。 输出格式: 对于每个测试用例,如果字符串 "S" 是超级 ascii,则打印 "Yes",否则打印 "No" 约束: 1<=T<=100 1<=|S|<=400,S 将只包含小写字母 ('a'-'z').
这是我所做的:
#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");
}
由于您的代码不符合要求的逻辑,我会给您一些指导。
您需要计算每种类型的字母数。这意味着您需要 26 个计数器,每种类型的字母一个。我建议使用数组,例如 int counters[26];
对于每个要测试的字符串 "S",您需要先将所有计数器设置为 0。
然后遍历字符串,并为每个字母增加该字母的计数器。
最后,遍历计数器并检查它们是否等于它们的值。请注意,0 也是可以接受的(该字母根本不出现在字符串中)。 如果任何字母未通过测试,则打印 "No\n" 否则打印 "Yes\n".
继续下一个字符串。
用一个函数来拆分你的代码会更简单,这个函数会测试一个字符串是一个超级字符串,因为一旦一个字母的计数不等于它的值,你就知道你可以立即 return 错误。根据您的逻辑,这意味着您应该拥有:
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;
}