oh hoh. got it. thanks
Why doesn’t my code work. It runs on different IDE and also went okay with another similar problem in toph. Here is my code:
a = int(input())
b = list(map(int, input().split()))
for i in range(a):
c = sum(b[:i+1])/(i+1)
print( c )
Your code is running 100% ok on Toph.
Toph uses the same Python that you use.
So, there is nothing to go wrong.
You are getting CPU Limit Exceeded which means your code is taking way too much time to run.
if your code takes more time than the timelimit than you will get this verdict.
for this problem, the time limit is 1s.
see this: https://toph.co/s/322078
Try to write a more efficient code.
I get CLE most of the time. ( ;_; )
@Abdullah_1234
I am also a Python solver and I guarantee you that this problem can be solved using Python3 without getting CLE.
If you get CLE than it is your problem. Try to think about how you can solve the problem more efficiently.
see this
There is a reason why there are two problems on Toph with almost the exact same statement and even very similar names.
Where is the difference in between these two problem.
The first line of the input will contain N (N < 100).
vs.
The first line of the input will contain N (N ≤ 100000).
thanks for pointing it out
#include <stdio.h>
float main() { int N,x,y,a,b,n;
N=4,x=N-2,y=N+3;
float c;
a=N,b=(N+x)/2,c=(N+x+y)/3;
printf(“%d %d %.10f”, a,b,c);}
What went wrong??? Why is it showing 4.00000000000 instead of 4.3333333333???
N = 4
x = N - 2 = 2
y = N + 3 = 7
a = N = 4
b = ( N + x ) / 2
= ( 4 + 2 ) / 2
= 6 / 2
= 3
So,
c = ( N + x + y ) / 3
= ( 4 + 2 + 7 ) / 3
= 13 / 3
= 4.3333........ (should be)
But Computer does not work like this.
float
variables has 32 bits and among those 8 is used for storing the decimical place of a numbers or a real number.
So, float
can store number with fractions.
But all of the bits in an integer
variable is used for storing the non fractional part of a number (except a bit that stores the sign of the number which is positive
or negative
).
Thus, integer
type variables cannot store fractional numbers.
So,
int / int = int // and,
float / float = float
and,
(float) 13 / (float) 4 = (float) 4.3333333... // and,
(int) 13 / (int) 4 = (int) 4 // integers cannot store fractional parts of a number.
// you can even see things like
(int) 3 / (int) 4 = (int) 0 // What?
Or, you can simply say that when you divide an integer
with an integer
, the fractional part is vanished.
To know how data inside integer
and float
variables is stored, see this.
However, if one of the arguments of the /
operator is float
, the other will be typecasted to float
and the result will be a float
type data.
(float) 13 / (int) 4 = (float) 4.3333333... // float 13 was typecasted.
(int) 3 / (float) 4 = (int) 0.3333333... // int 3 was typecasted.
check this out yourself. ideone link: TIlMp2 - Online C Compiler & Debugging Tool - Ideone.com
printf("%f\n", (float)(13 / 4));
printf("%f\n", (float)(13.0 / 4));
printf("%f\n", (float)(13 / 4.0));
printf("%f\n", (float)(13.0 / 4.0));
Luck it is a matter of, solution of previous problem might be what you need here!
(I’m Just Trynna Be YODA! XD)
can anyone suggest me how can i write my code more efficiently?
here is my code
Scanner input=new Scanner(System.in);
int N=0;
if(N<=100000){
N=input.nextInt();
double sum=0,average;
double number[]=new double[N];
for (int i = 0; i <number.length; i++) {
number[i]=input.nextDouble();
sum+=number[i];
average=sum/(i+1);
System.out.println(""+average);
this is showing cpu time limit exceded
You don’t need to use an array to store all numbers. Think again and you can do it yourself. If i want to say something then I will want to say that don’t put unnecessary things like these. It is a little intriguing.
if(N <= 100000) {
. . . .
. . . .
}
And I please try to markdown your codes properly. See this. It might help.
in java, what am i doing wrong?
import java.util.Scanner;
public class solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int digits=100001;
while(digits>100000)
digits = scanner.nextInt();
double[] intArray = new double[digits];
double sum1=0,output;
for(int i=0; i<digits; i++){
intArray[i]=scanner.nextDouble();
if(intArray[i]>1000 || intArray[i]<1){
i–;
}
}
for(int i=0; i<digits; i++){
for(int j=0; j<=i; j++){
sum1+=intArray[j];
}
output = sum1/(i+1);
System.out.println(output);
sum1=0;
}
}
}
What’s wrong with this code??
test = int(input())
x = [int(i) for i in input().split()]
res = 0
for i in x:
res += (sum(x[:(x.index(i)+1)]))/(len(x[:(x.index(i)+1)]))
print("%.4f" %res)
res *= 0
//This is easiest way to solve with java
import java.util.Scanner;
public class sol {
static double pi= 3.141592653589793;
public static void main(String[] args) {
int x,y;
Scanner ob=new Scanner (System.in);
x=ob.nextInt();
int a[]=new int [x];
for(int i=0;i<a.length;i++)
{
a[i]=ob.nextInt();
}
int sum=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<=i;j++)
{
sum=sum+a[j];
}
double k= (sum/(double)(i+1));
System.out.println(k);
sum=0;
}
}
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
short n = in.nextShort();
float num, avrg = 0.0f;
for(int i = 1; i<=n; i++){
num = in.nextFloat();
avrg = avrg+num /i;
System.out.println(avrg);
}
}
}
Getting Runtime error … Any sugestion?
#include<stdio.h>
int main()
{
int n,a[n],count=1.0,i;
double sum=0.0;
scanf("%d",&n);
if(n<=100000)
{
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
if(a[i]>1000)
{
return 0;
}
else
{
sum += a[i];
printf("%lf ", sum/count);
count++;
}
}
}
return 0;
}
What is the problem in my code?
bro, it can be solved by python. When I solved this problem by python, I also get an error . Then I try a different way and find a solution (using python).