You are not logged in. Please login at www.codechef.com to post your questions!

×

COAD02 - Editorial

Problem Code: COAD02

Problem link is here

Problem Name: Simple Sum

Contest Name: CodeAdda Practice Contest – 2 (CPC2016)

Author: Yatharth Oza (Username: yatharth100)

Difficulty: Easy

PREREQUISITES: Math

Problem:

Here One String is given as a input in each test case. Each String contains some Float numbers join by one character ‘e’. You just need to extract all floats and do sum of all numbers and print output which is correct up to 4 decimal places.

Explanation:

For this problem we can traverse whole input string by putting one by one character in one temporary string and when ‘e’ comes we will stop and then we can convert string to float and add in ‘Sum’ variable which stores current sum of input. At last we can just return the value of ‘Sum’. After each iteration we make temporary string null and Sum value to 0. If you are doing your code in C then you can make approach like this. But, If you are using JAVA or any object oriented language then it provides directly “split(“e”)” function from which you can make given approach directly.

Solution:

/*Solution is in JAVA and filename is Main.java */
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        float sum=0,temp;
        long t;
        Scanner in=new Scanner(System.in);
        t=in.nextLong();
        while(--t>=0)
        {
        sum=0;
        String str = in.next();
        String[] no = str.split("e");
        for(int i=0;i<no.length;i++)
        {
            temp=Float.parseFloat(no[i]);
            sum=sum+temp;
        }
        System.out.printf("%.4f\n", sum);
        }
    }
}

asked 29 Mar '16, 18:39

yatharth100's gravatar image

2★yatharth100
3
accept rate: 0%

edited 29 Mar '16, 19:22

admin's gravatar image

0★admin ♦♦
19.8k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,631
×3,741
×874
×1

question asked: 29 Mar '16, 18:39

question was seen: 432 times

last updated: 29 Mar '16, 19:22