gpt4 book ai didi

c - 艾达和蜡笔 - CodeChef

转载 作者:行者123 更新时间:2023-11-30 16:43:12 26 4
gpt4 key购买 nike

The Problem

我的代码有什么问题?它在我的 TurboC 编译器上运行完全正常,但在 CodeChef 中给出运行时错误。

Ada 有 N 支蜡笔数组,一些蜡笔朝上,一些朝下。艾达认为,如果所有蜡笔都指向同一个方向,那么一排蜡笔就很漂亮。

一步即可翻转连续蜡笔的任意一段。翻转一段后,所有朝下的蜡笔都会朝上,反之亦然

最少需要多少步骤才能使蜡笔阵列变得漂亮?

输入

输入的第一行包含 T 测试用例的数量。每个测试用例都用包含 N 个字符的字符串 S 的一行进行描述,如果第 i 个蜡笔朝上,则第 i 个字符为“U”,如果第 i 个蜡笔朝下,则第 i 个字符为“D”。

输出

对于每个测试用例,输出一行,其中包含使所有蜡笔指向同一方向所需的最少翻转次数。

示例

输入:1呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜

输出:1

#include<stdio.h>

int main()
{
clrscr();
int t,i,j,p,c,k,ucount=0,dcount=0;
char last='0';
char cases[50][3000];

scanf("%d",&t);

for(i=0;i<t;i++)
scanf("%s",cases[i]);

for(i=0;i<t;i++)
{
for(j=0;cases[i][j]!='\0';j++)
{
if(cases[i][j]=='U' && last!='U')
{
ucount++;
last='U';
}
else if(cases[i][j]=='D' && last!='D')
{
dcount++;
last='D';
}
}

if(i==(t-1))
{
if(dcount<ucount)
printf("%d",dcount);
else
printf("%d",ucount);
}
else
{
if(dcount<ucount)
printf("%d\n",dcount);
else
printf("%d\n",ucount);
}

ucount=0;
dcount=0;
last='0';
}

return 0;
}

最佳答案

我严重怀疑 CodeChef 支持您似乎使用过的非标准 clrscr() 函数。您应该确保您使用的是本地符合标准的功能,而不是使用标准库中找不到的平台和工具特定功能的功能。

Davids-Mac-Pro:~ dhoelzer$ gcc test.c 
test.c:4:1: warning: implicit declaration of function 'clrscr' is invalid in C99
[-Wimplicit-function-declaration]
clrscr();
^
1 warning generated.
Undefined symbols for architecture x86_64:
"_clrscr", referenced from:
_main in test-171c5e.o
ld: symbol(s) not found for architecture x86_64

您可能会找到使用 conio.h 的引用,但这也不是标准的一部分。

关于c - 艾达和蜡笔 - CodeChef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45442213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com