Algorithm
Problem Name: beecrowd | 1156
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1156
S Sequence II
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Write an algorithm to calculate and write the value of S, S being given by:
S = 1 + 3/2 + 5/4 + 7/8 + ... + 39/?
Input
There is no input in this problem.
Output
The output contains a value corresponding to the value of S.
The value should be printed with two digits after the decimal point.
Input Sample | Output Sample |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)
using namespace std;
int main(){
float s = 1;
int x = 2;
for (int i = 3; i < 40 ; i += 2){
s += (((1.0) * i) / x);
x *= 2;
}
cout << FIXED_FLOAT(s) << endl;
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with Java Programming
Code -
Java Programming
public class Main{
public static void main(String[] args){
float s = 1;
int x = 2;
for (int i = 3; i < 40 ; i += 2){
s += (((1.0) * i) / x);
x *= 2;
}
System.out.printf("%.2f\n", s);
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var s = 1.0;
var x = 2;
for (var i = 3; i < 40 ; i += 2){
s += (((1.0) * i) / x);
x *= 2;
}
console.log(s.toFixed(2));
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
Python Programming
s = 1.0
x = 2
for i in range(3, 40, 2):
s += (i / x)
x *= 2
print("{:.2f}".format(s))
Copy The Code &
Try With Live Editor