Skip to content

Instantly share code, notes, and snippets.

@oyekanmiayo
Last active December 13, 2022 20:59
Show Gist options
  • Save oyekanmiayo/0f39366acdfb65be1cfac5674fb4928e to your computer and use it in GitHub Desktop.
Save oyekanmiayo/0f39366acdfb65be1cfac5674fb4928e to your computer and use it in GitHub Desktop.
How to read from and write to standard input and ouput.
#include <stdlib.h>
int main(){
// Read integer from line
int t;
scanf("%d",&t);
// Read integer from line times
for(int a0 = 0; a0 < t; a0++){
int n;
scanf("%d",&n);
}
return 0;
}
int main(){
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
long n;
cin >> n;
}
return 0;
}
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
// Call readLine(reader) as many times as you need
// checkError(err)
// Get result
// %d assumes an integer output, find the right format for your output
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
import java.io.*;
import java.util.*;
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
// Read line from input into a string
bufferedReader.readLine().replaceAll("\\s+$", "");
// Read line from input and split by space into an array
bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
// Read line from input, replace all white spaces, split the values on the line by space, convert them to int
// and add them to alit
try {
List<Integer> intInput = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
// Get result
// Write result to output
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
// Using Scanner
import java.util.*;
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
System.out.println(userName.contains("\\r\\n\\r\\n"));
}
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
// Call readLine() as many times as you need.
// get answer
ws.write(answer + '\n');
ws.end();
}
import sys
# Read t, then read t integers from the input
t = int(input().strip())
for a0 in range(t):
n = int(input().strip())
# Read t, read 2 * t lines. In each iteration, split the first line by space and read the second line
t = int(input().strip())
for a0 in range(t):
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
num = input().strip()
# To send value to stdout, use print()
print(val)
@Shopiley
Copy link

Please can you provide one for python?

@oyekanmiayo
Copy link
Author

Done @Shopiley

@Shopiley
Copy link

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment