Reputation: 335
I am trying to solve this question. I tried many different ways, I get the correct output but the testcases are not passed(I dont know what are those testcases). I cant figure what is wrong.
Question:
Given a set of integers (separated by space), write a program to print the sum of their cubes.
Sample Input:
1 2 3 4 5
Sample Output:
225
Sample Input:
1 2
Sample Output:
9
Solution 2:
import java.io.*;
import java.util.*;
class Test{
public static void main(String []args)
{
int num,sum=0;
Scanner sc=new Scanner(System.in);
String data=sc.nextLine();
Scanner sc2=new Scanner(data);
while(sc2.hasNextInt())
{
num=sc2.nextInt();
sum=sum+num*num*num;
}
System.out.print(sum);
}
}
Upvotes: 0
Views: 3620
Reputation: 335
Used long instead of int. Thanks to Andreas and Matsev for the BigInteger
idea.
import java.util.*;
class Test{
public static void main(String []args)
{
long num,sum=0;
Scanner sc=new Scanner(System.in);
String data=sc.nextLine();
Scanner sc2=new Scanner(data);
while(sc2.hasNextInt())
{
num=sc2.nextInt();
sum=sum+num*num*num;
}
System.out.println(sum);
}
}
Upvotes: -1
Reputation: 1
public static void main(String args[])
{
long num,count=0;
Scanner in=new Scanner(System.in);
while(in.hasNextInt())
{
num=in.nextInt();
count+=Math.pow(num,3);
}
System.out.println(count);
}
or
public Static vid main(String args[])
{
long count;
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String[] t=s.split(" ");
for(i=0;i<t.length;i++)
{
count+=Math.pow(Integer.parseInt(t[i]),3);
}
System.out.println(count);
}
Upvotes: -1
Reputation: 1
import java.util.*;
class csum
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
long csum=0;
while(in.hasNextInt())
{
csum=csum+(long)Math.pow(in.nextInt(),3);
}
System.out.println(csum);
}
}
Upvotes: 0
Reputation: 33779
Update (by Andreas): Added boilerplate imports since @Ash seems unable to figure that out.
If you would like an implementation in more functional style (using Java 8+ streams), here is another suggestion:
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
String[] numbers = line.split("\\s+");
BigInteger sum = Arrays.asList(numbers)
.stream()
.map(BigInteger::new)
.map(bigInteger -> bigInteger.pow(3))
.reduce(BigInteger.ZERO, BigInteger::add);
System.out.println(sum);
}
}
Update: After @Andreas comments, the implementation can be even shorter:
import java.math.BigInteger;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
BigInteger sum = Pattern.compile("\\s+")
.splitAsStream(line)
.map(s -> new BigInteger(s).pow(3))
.reduce(BigInteger::add)
.orElse(BigInteger.ZERO);
System.out.println(sum);
}
}
Upvotes: 1
Reputation: 432
I modified my answer according to your needs. Now it gives sum of cubes of numbers in given input which are separated by spaces. Check this :-
import java.math.BigInteger;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.List;
import java.util.Scanner;
public class SumOfCubes {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String integers="";
Integer num=0;
BigInteger cube;
BigInteger sum=new BigInteger("0");
System.out.println("Enter Integers seperated by space");
integers=in.nextLine();
List<Integer> list = new ArrayList<Integer>();
for (String s : integers.split("\\s")) {
list.add(Integer.parseInt(s));
}
list.toArray();
for(int i=0;i<list.size();i++){
num=list.get(i);
cube=BigInteger.valueOf(num*num*num);
sum=sum.add(cube);
}
System.out.println("Sum of Cubes of numbers in given input = "+sum);
}
}
Hope it helps.
Upvotes: 1
Reputation: 2652
I have changed your Solution 2 a little bit. Could you please confirm whether this passes the test or not.
import java.math.BigInteger;
import java.util.Scanner;
public class Test{
public static void main(String []args)
{
BigInteger sum=new BigInteger.ZERO;
Scanner sc=new Scanner(System.in);
String data=sc.nextLine();
Scanner sc2=new Scanner(data);
while(sc2.hasNextInt())
{
BigInteger num=new BigInteger(String.valueOf(sc2.nextInt()));
sum= sum.add(num.multiply(num).multiply(num));
}
System.out.print(sum);
}
}
Upvotes: 0
Reputation: 11319
Your test cases most likely contain large integers as input. Use BigInteger to hold their cubes.
Upvotes: 2