【问题标题】:the code here works perfectly in codeblocks but in codechef IDE it gives SIGTSTP (runtime error and shows a lot of 0s in the output)这里的代码在代码块中完美运行,但在 codechef IDE 中它给出了 SIGTSTP(运行时错误并在输出中显示很多 0)
【发布时间】:2020-11-14 08:01:55
【问题描述】:

问题链接:https://www.codechef.com/problems/LUCKFOUR

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

int main() {

    int T,ans;
    scanf("%d",&T);

    while(T) {
        int num,count = 0;
        scanf("%d",&num);

        while(num) {
            ans = num % 10;
            num = num/10;
            if( ans == 4) {
                count++;
            }
        }
        printf("%d\n",count);
        T--;
    }
    return 0;
}

对于代码块正常运行:它首先需要 T,然后从用户那里获取输入并打印编号。该输入中的 4 秒,并重复输入并打印出编号的过程。 4s T 次

【问题讨论】:

  • 可能是 TLE。并且codechef在线判断正在发出停止信号杀死进程。
  • 如果您得到 TLE,那么不要让计算机浪费时间将输入的数字转换为数字,然后再将数字转换回数字!输入一个字符串。
  • @silentPlanet 是什么让您认为报告为“SIGTSTP 运行时错误”的内容是 TLE?
  • 你的算法效率不高。只需在 string 读取中搜索'4's。
  • 对我来说,你在问什么很不清楚

标签: c loops scanf


【解决方案1】:

我对您的解决方案进行了一些修改,并且通过了。请注意约束,因为有时int 会导致溢出。看看:

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

int main() {
  long long int T;
  scanf("%lld", &T);

  while(T--) {
    long long int num, count=0;
    int ans;
    scanf("%lld",&num);

    while(num) {
        ans = num % 10;
        num = num/10;
        if( ans == 4) {
            count++;
        }
    }
    printf("%d\n",count);
  }
  return 0;
}

Submission image

【讨论】:

  • long long int 有什么不同,我的问题得到了正确的答案,但在 codechef ide 中运行时出错
  • 知道了。我认为您的解决方案因此不被接受。当我解决像 10⁹ 这样的约束问题时,我使用long long int 来避免溢出问题。
猜你喜欢
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多