1. Solve Me First
<?php
function solveMeFirst($a,$b){
// Hint: Type return $a + $b; below
}
$handle = fopen ("php://stdin","r");
$_a = fgets($handle);
$_b = fgets($handle);
$sum = (int)$_a+(int)$_b;
print ($sum);
fclose($handle);
?>
2. Simple Array Sum (PHP)
<?php
/*
* Complete the simpleArraySum function below.
*/
function simpleArraySum($ar) {
/*
* Write your code here.
*/
$len=count($ar);
$sum=0;
for($i=0;$i<$len;$i++){
$sum+=$ar[$i]; // sum=sum+ar[i];
}
return $sum;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $ar_count);
fscanf($stdin, "%[^\n]", $ar_temp);
$ar = array_map('intval', preg_split('/ /', $ar_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = simpleArraySum($ar);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);
3. Compare the Triplets (java 7)
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the compareTriplets function below.
static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
int aliceTotalScore = 0, bobTotalScore = 0;
for(int i=0; i<3; i++){
int aliceScore = a.get(i);
int bobScore = b.get(i);
if(aliceScore != bobScore){
int temp = aliceScore > bobScore ? aliceTotalScore++ : bobTotalScore++;
}
}
List<Integer> result = new ArrayList<>();
result.add(aliceTotalScore);
result.add(bobTotalScore);
return result;
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] aItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> a = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int aItem = Integer.parseInt(aItems[i]);
a.add(aItem);
}
String[] bItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> b = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int bItem = Integer.parseInt(bItems[i]);
b.add(bItem);
}
List<Integer> result = compareTriplets(a, b);
for (int i = 0; i < result.size(); i++) {
bufferedWriter.write(String.valueOf(result.get(i)));
if (i != result.size() - 1) {
bufferedWriter.write(" ");
}
}
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
4. A Very Big Sum (PHP)
<?php
// Complete the aVeryBigSum function below.
function aVeryBigSum($ar) {
$len=count($ar);
$sum=0;
for($i=0;$i<$len;$i++){
$sum+=$ar[$i]; // sum=sum+ar[i];
}
return $sum;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $ar_count);
fscanf($stdin, "%[^\n]", $ar_temp);
$ar = array_map('intval', preg_split('/ /', $ar_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = aVeryBigSum($ar);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);
No comments:
Post a Comment
" Terimakasih telah mengunjungi blog kami ,, silahkan tinggalkan komentar untuk menghargai jerit payah penulis ^_^ "