ACM 10038 Jolly跳线

ACM 10038 Jolly跳线,c,C,我已经编写了(ACM 10038 uva)的解决方案代码。 我的代码如下: #include<stdio.h> #include<stdlib.h> int main(){ int count=0; int Number[3000]={0}; int Absolute[3000]={0}; bool flag=true; while(scanf("%d",&count)){ for(int i=0;i<count;++i){

我已经编写了(ACM 10038 uva)的解决方案代码。 我的代码如下:

#include<stdio.h>
#include<stdlib.h>

int main(){
  int count=0;
  int Number[3000]={0};
  int Absolute[3000]={0};
  bool flag=true;
  while(scanf("%d",&count)){
   for(int i=0;i<count;++i){
     scanf("%d",&Number[i]);
     Absolute[i]=0;
   }
   for(int j=0;j<count-1;++j){
     int diff=Number[j]-Number[j+1];
     if(diff<0)diff*=-1;
     Absolute[diff]=1;
   }
   flag=true;
   for(int x=1;x<count;++x){
     if(Absolute[x]!=1){
       flag=false;
       break;
     }
   }
   if(flag)printf("Jolly\n");
   else printf("Not Jolly\n");
 }
 return 0;
}
#包括
#包括
int main(){
整数计数=0;
整数[3000]={0};
int绝对[3000]={0};
布尔标志=真;
while(scanf(“%d”、&count)){

对于(inti=0;i我以前已经回答过了

我所做的是把每两个元素的差值加到一个向量中,最后对它进行排序


现在你需要做的只是检查这个向量的每个元素是否比前一个元素多1个。另外,检查这个排序向量的第一个元素是1,最后一个元素是n-1。

我以前已经回答过了

我所做的是把每两个元素的差值加到一个向量中,最后对它进行排序


现在,您需要做的只是检查此向量的每个元素是否比前一个元素正好多1个。此外,检查此排序向量的第一个元素为1,最后一个元素为n-1。

您的程序可能超出了时间限制,因为它从未完成。if/when
scanf()
返回
EOF
,以下内容将永远不会停止循环:

while(scanf("%d",&count)){
    // whatever...
}

在这些在线编程问题中,通常最好至少根据问题中提供的示例数据运行您提出的解决方案,看看您是否获得了预期的输出。如果您的程序没有产生预期的输出,那么您就知道您有一个问题要解决(您还有一些具体的问题要调试).

您的程序可能超出了时间限制,因为它从未完成。如果/当
scanf()
返回
EOF
,以下内容将永远不会停止循环:

while(scanf("%d",&count)){
    // whatever...
}

在这些在线编程问题中,通常最好至少根据问题中提供的示例数据运行您提出的解决方案,看看您是否获得了预期的输出。如果您的程序没有产生预期的输出,那么您就知道您有一个问题要解决(您还有一些具体的问题要调试).

无限循环!只需将
while(scanf(“%d”,&count))
替换为
while(scanf(“%d”,&count)!=EOF)
即可

man scanf

返回值

   These  functions  return  the  number  of  input items successfully
   matched and assigned, which can be fewer than provided for, or even
   zero in the event of an early matching failure.

   The  value  EOF  is  returned if the end of input is reached before
   either the  first  successful  conversion  or  a  matching  failure
   occurs.  EOF is also returned if a read error occurs, in which case
   the error indicator for the stream  (see  ferror(3))  is  set,  and
   errno is set indicate the error.
我也是一个竞争对手。我可以给你一个提示,就是始终创建一个输入文件(in.txt)和一个输出文件(out.txt),将输入重定向到程序,并使用
diff
比较输出:

$ cat in.txt
4 1 4 2 3
5 1 4 2 -1 6

$ cat out.txt
Jolly
Not jolly

$ ./jolly_jumpers < in.txt > my_out.txt

$ diff -s out.txt my_out.txt
Files out.txt and my_out.txt are identical
$cat in.txt
4 1 4 2 3
5 1 4 2 -1 6
$cat out.txt
愉快的
不高兴
$./jolly\u跳线my\u out.txt
$diff-s out.txt my_out.txt
文件out.txt和my_out.txt是相同的

无限循环!只需将
while(scanf(“%d”,&count))
替换为
while(scanf(“%d”,&count)!=EOF)
即可

man scanf

返回值

   These  functions  return  the  number  of  input items successfully
   matched and assigned, which can be fewer than provided for, or even
   zero in the event of an early matching failure.

   The  value  EOF  is  returned if the end of input is reached before
   either the  first  successful  conversion  or  a  matching  failure
   occurs.  EOF is also returned if a read error occurs, in which case
   the error indicator for the stream  (see  ferror(3))  is  set,  and
   errno is set indicate the error.
我也是一个竞争对手。我可以给你一个提示,就是始终创建一个输入文件(in.txt)和一个输出文件(out.txt),将输入重定向到程序,并使用
diff
比较输出:

$ cat in.txt
4 1 4 2 3
5 1 4 2 -1 6

$ cat out.txt
Jolly
Not jolly

$ ./jolly_jumpers < in.txt > my_out.txt

$ diff -s out.txt my_out.txt
Files out.txt and my_out.txt are identical
$cat in.txt
4 1 4 2 3
5 1 4 2 -1 6
$cat out.txt
愉快的
不高兴
$./jolly\u跳线my\u out.txt
$diff-s out.txt my_out.txt
文件out.txt和my_out.txt是相同的