• 不同编程语言的a+b=c表示方法


    C 语言
    #include <stdio.h>
    int main()
    {
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF)
    printf("%d\n", a + b);
    }

    C++ 语言
    #include <iostream>
    using namespace std;
    int main()
    {
    int a, b;
    while(cin>> a >> b)
    cout << a + b << endl;
    return 0;
    }

    C# 语言
    using System;
    class Program {
    public static void Main() {
    string line;
    string []p;
    int a,b;
    while((line=Console.ReadLine())!=null){
    p=line.Split(' ');
    a=int.Parse(p[0]);b=int.Parse(p[1]);
    Console.WriteLine(a+b);
    }
    }
    }

    Java 语言
    //package main
    //注意不要添加包名称,否则会报错。

    import java.io.*;
    import java.util.*;
    class Test {
    }
    public class Main
    {
    public static void main(String args[])
    {
    Scanner cin = new Scanner(System.in);
    int a, b;
    while(cin.hasNextInt())
    {
    a = cin.nextInt();
    b = cin.nextInt();
    System.out.println(a + b);
    }
    }
    }

    Ruby 语言
    a=gets
    while a != nil && a != "" && a != "\r" && a != "\n" do
    arr = a.split(" ")
    sum = 0
    arr.each_with_index do |value, index|
    sum = sum + value.to_i
    end
    puts sum.to_s
    a=gets
    end

    Bash 语言
    #!/bin/bash

    read -a arr
    #echo ${#arr[@]}
    while [ ${#arr[@]} -eq 2 ]
    do
    sum=$((${arr[0]}+${arr[1]}))
    echo "$sum"
    read -a arr
    done

    Obj-C 语言
    #import <Foundation/Foundation.h>
    int primeNumber(long int m)
    {
    int i;
    for (i=2; i*i<=m; i++) {
    if (m%i==0)
    return 0;
    }
    return 1;
    }
    int main(int argc,char * argv[])
    {
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    int n,flag;
    long int m;
    int a,b;
    while(scanf("%d %d",&a, &b) != EOF)
    printf("%d\n",a+b);
    return 0;
    [pool drain];
    return 0;
    }

    NodeJS 语言
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');

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

    //这里不灵活,需要全部读取数据后再处理,容易超内存。
    process.stdin.on('data', function (data) {
    input += data;
    });

    var solveMeFirst = (a, b) => a+b;

    process.stdin.on('end', function () {
    let arr = input.split("\n");
    for (var i=0; i<arr.length; i++) {
    if (arr[i] != '') {
    input_array = arr[i].split(" ");

    let inline = 0;
    let res;
    let _a = parseInt(input_array[inline].trim(), 10);
    inline += 1;

    let _b = parseInt(input_array[inline].trim(), 10);
    inline += 1;

    res = solveMeFirst(_a, _b);
    process.stdout.write("" + res + "\n");
    }
    }
    });

    PHP 语言
    <?php
    function solveMeFirst($a,$b){
    return $a + $b;
    }
    $handle = fopen ("php://stdin","r");
    $s = fgets($handle);
    while ($s != "") {
    $a = explode(" ", $s);
    $sum = solveMeFirst((int)$a[0],(int)$a[1]);
    print ($sum);
    print ("\n");
    $s = fgets($handle);
    }
    fclose($handle);
    ?>

    Python语言
    Python2:

    #!/usr/bin/env python
    # coding=utf-8
    # Python使用的是2.7.6,缩进可以使用tab、4个空格或2个空格,但是只能任选其中一种,不能多种混用
    while 1:
    a=[]
    s = raw_input()
    # raw_input()里面不要有任何提示信息
    if s != "":
    for x in s.split():
    a.append(int(x))

    print sum(a)
    else:
    break

    Python3:

    #!/usr/bin/env python
    # coding=utf-8
    # Python使用的是3.4.3,缩进可以使用tab、4个空格或2个空格,但是只能任选其中一种,不能多种混用
    while 1:
    a=[]
    s = input()

    if s != "":
    for x in s.split():
    a.append(int(x))

    print(sum(a))
    else:
    break

    Go 语言
    package main
    import "fmt"
    import "io"
    func main() {
    a:=0
    b:=0
    for {
    _, err := fmt.Scanf("%d%d",&a,&b)
    if err == io.EOF {
    break
    } else {
    fmt.Printf("%d\n",a+b)
    }
    }
    }


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

  • 相关阅读:
    jquery 监听input的value值改变
    Win10家庭版共享打印机启用Guest账户
    js数组操作大全(pop,push,unshift,splice,shift方法)
    EF Core索引
    工作经验(C++篇)
    FFmpeg编译i386 OSX 脚本
    xcrun -sdk 选择
    Unity GL 画圆
    OpenGL ES无法获取贴图数据原因
    Unity在Android和iOS中如何调用Native API
  • 原文地址:https://www.cnblogs.com/GKLBB/p/16703349.html
Copyright © 2020-2023  润新知