互联网笔试各种主流语言在OJ上的的标准输入输出

1.求a+b的和

c++

#include 
using namespace std;
int main() {
    int a,b;
    while(cin >> a >> b)//注意while处理多个case
        cout << a+b << endl;
}

java

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {//注意while处理多个case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}

python 2.7

#coding=utf-8
import sys 
for line in sys.stdin:
    a = line.split()
    print int(a[0]) + int(a[1])

python 3.5

#coding=utf-8
import sys 
for line in sys.stdin:
    a = line.split()
    print(int(a[0]) + int(a[1]))

c#

#coding=utf-8
import sys 
for line in sys.stdin:
    a = line.split()
    print(int(a[0]) + int(a[1]))

php

js v8.6

while(line=readline()){
    var lines = line.split(" ");
    var a = parseInt(lines[0]);
    var b = parseInt(lines[1]);
    print(a+b);
}

js Node 6.11

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input = "";
var input_array = "";

process.stdin.on('data', function (data) {
    input += data;
});

process.stdin.on('end', function () {
    input_array = input.split("\n");
    var nLine = 0;

    while(nLine < input_array.length){
        var line = input_array[nLine++].trim();
        if(line === ''){
            continue;
        }
        var input_arrays = line.split(' ');
        var a = +input_arrays[0];
        var b = +input_arrays[1];
        console.log(a+b);
    }
});

R

lines=readLines("stdin")
for(l in lines){
    if(l == ""){
        break;
    }
    ll = strsplit(l, " ")[[1]]
    a=ll[1];
    b=ll[2];
    cat(as.numeric(a)+as.numeric(b));
    cat("\n");
}

Go

package main

import (
    "fmt"
)
func main() {
    a:=0
    b:=0
    for {
        n, _ := fmt.Scan(&a,&b)
        if n == 0 {
            break
        } else {
            fmt.Printf("%d\n",a+b)
        }
    }
}

Ruby

a, b = gets.split(" ").map {|x| x.to_i}
puts (a + b)

2.给出N阶方阵所有的数,求方阵中所有数的和

Input:
3
1 2 3
2 1 3
3 2 1
Output:
18

c/c++

#include 
#include 

using namespace std;

int main(){
    //freopen("1.in","r",stdin);
    int n,ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x; scanf("%d",&x);
            ans += x;
        }
    }
    cout << ans << endl;
    return 0;
}

java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0, x;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                x = sc.nextInt();
                ans += x;
            }
        } 
        System.out.println(ans);
    }
}

python 2.7

#coding=utf-8
import sys

if __name__ == "__main__":
    # 读取第一行的n
    n = int(sys.stdin.readline().strip())
    ans = 0
    for i in range(n):
        # 读取每一行
        line = sys.stdin.readline().strip()
        # 把每一行的数字分隔后转化成int列表
        values = map(int, line.split())
        for v in values:
            ans += v
    print ans

python 3.5

#coding=utf-8
import sys
if __name__ == "__main__":
    # 读取第一行的n
    n = int(sys.stdin.readline().strip())
    ans = 0
    for i in range(n):
        # 读取每一行
        line = sys.stdin.readline().strip()
        # 把每一行的数字分隔后转化成int列表
        values = list(map(int, line.split()))
        for v in values:
            ans += v
    print(ans)

c#

using System;
public class Program {
    public static void Main() {
        int n = int.Parse(Console.ReadLine().Trim());
        int ans = 0;
        for(int i = 0; i < n; i++){
            string[] inputs = Console.ReadLine().Trim().Split(' ');
            for(int j = 0; j < n; j++){
                ans += int.Parse(inputs[j]);
            }
        }
        Console.WriteLine(ans);
    }
}

PHP

js v8.6

var n = parseInt(readline());
var ans = 0;
for(var i = 0;i < n; i++){
    lines = readline().split(" ")
    for(var j = 0;j < lines.length; j++){
        ans += parseInt(lines[j]);
    }
}
print(ans);

js Node

var readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal:false
});

var n = -1;// 初始状态为负数,表示还没开始读取
var ans = 0;
var cur_line = 0;
rl.on('line', function(line){ // javascript每行数据的回调接口
   if (n < 0) { // 测试用例第一行读取n
       n = parseInt(line.trim())
   } else {
       // 矩阵数据读取
          var tokens = line.split(' ');
       for (var i = 0; i < tokens.length; ++i) {
           // 题目逻辑求和,边读取边计算
           ans += parseInt(tokens[i]);
       }
       // 记录当前读取的行数
       cur_line += 1;
   }
    
   // 读取行数结束,如果确定只有一行额外的数据输入,也可以通过cur_line === 1来判断
   if (n === cur_line) {
       // 输出结果
       console.log(ans);
       // 重新初始化相关变量
       n = -1;
       ans = 0;
       cur_line = 0;
   }
});

R

lines=readLines("stdin")
sum=0
line_count=0
line_index=1
for(l in lines){
    if(l == ""){
        break;
    }
    if(line_count == 0){
        line_count = as.numeric(l);
        next
    }

    if(line_index > line_count){
        break
    }
    line_index = line_index + 1

    ll = strsplit(l, " ")[[1]]
    for(num in ll){
        sum = sum + as.numeric(num);
    }
}
cat(sum);
cat("\n");

Go

package main

import (
    "fmt"
)
func main() {
    n:=0
    ans:=0

    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            x:=0
            fmt.Scan(&x)
            ans = ans + x
        }
    }
    fmt.Printf("%d\n",ans)
}

Ruby

num = gets.to_i
i = 0
sum = 0
while i < num  do
    line = gets
    line.split().each do |token|
      sum += token.to_i
    end
    i +=1
end
puts(sum)

转载请注明出处:
CSDN:楼上小宇_home:http://blog.csdn.net/sty945
:楼上小宇:http://www.jianshu.com/u/1621b29625df

你可能感兴趣的:(互联网笔试各种主流语言在OJ上的的标准输入输出)