【发布时间】: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