【问题标题】:Time limit exceeded Error codechef超出时间限制 错误 codechef
【发布时间】:2016-05-21 06:34:02
【问题描述】:

这是问题 如果给出一个整数 N 。编写一个程序来获取这个数字的第一位和最后一位数字的总和。

输入

第一行包含一个整数T,测试用例的总数。然后跟随 T 行,每行包含一个整数 N。 输出

显示N的第一位和最后一位的和。 约束

1 ≤ T ≤ 1000 1≤N≤1000000

例子

输入 3 1234 124894 242323

输出 5 5 5

这是我的代码,当我提交代码时,它显示 TLE 错误。我认为我的代码的复杂性是 O(1),因为只要输入数字,就会计算总和。我想我正在计算复杂性错误。对此有一些帮助会很棒。干杯!

#include<iostream>
#include<vector>
#include<math.h>
#include<stdio.h>
using namespace std;


int main()
{
    char first,chr,chr_prv,temp;
    int test;
    cin>>test;
    temp=getchar(); // just to ignore the /n after cin>>test
    for(int i=0;i<test;i++)
    {

    int count=0;
    do
    {

        chr_prv=chr;
        chr=getchar();
        if(count==0)
            first=chr;
        count++;
    }while(chr!='\n');
    cout<<(int(first)+int(chr_prv)-96)<<endl;

    }
  return 0;
}

【问题讨论】:

    标签: time-complexity


    【解决方案1】:

    您正在逐个字符地输入,这比较慢。我建议将整数作为单个整数输入,然后简单地添加 number%10 和 number/str(number).length()。一定要带着结果回来:)(考虑到这是一个codechef问题,我特意给出了一个提示,而不是整个代码)

    【讨论】:

    • 是的,我知道这种方法之前我只是觉得这会更快☺无论如何都比建议的建议
    • 你得到了什么结果?顺便说一句,如果您的问题得到了回答,请将其标记为已回答,以便其他 SO 成员轻松使用
    • 结果为?我得到了一个 ac:)
    • 很酷,所以问题确实在于输入。干得好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多