7 条题解

  • 4
    @ 2024-12-22 11:13:05

    算法想必大家都懂,下面展示不同语言的写法:

    C (C 14 with O2

    #include <stdio.h>
    
    int main()
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n", a+b);
        return 0;
    }
    

    C++(C++ 14 with O2

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

    Pascal

    var a, b: longint;
    begin
        readln(a,b);
        writeln(a+b);
    end.
    

    Python(Python 3

    s = input().split()
    print(int(s[0]) + int(s[1]))
    

    Java(Java 21 LTS

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

    JavaScript(Node.js

    const fs = require('fs')
    const data = fs.readFileSync('/dev/stdin')
    const result = data.toString('ascii').trim().split(' ').map(x => parseInt(x)).reduce((a, b) => a + b, 0)
    console.log(result)
    process.exit() // 请注意必须在出口点处加入此行
    

    Ruby

    a, b = gets.split.map(&:to_i)
    print a+b
    

    PHP

    <?php
    $input = trim(file_get_contents("php://stdin"));
    list($a, $b) = explode(' ', $input);
    echo $a + $b;
    

    Rust

    use std::io;
    
    fn main(){
        let mut input=String::new();
        io::stdin().read_line(&mut input).unwrap();
        let mut s=input.trim().split(' ');
    
        let a:i32=s.next().unwrap()
                   .parse().unwrap();
        let b:i32=s.next().unwrap()
                   .parse().unwrap();
        println!("{}",a+b);
    }
    

    Golang

    package main
    
    import "fmt"
    
    func main() {
        var a, b int
        fmt.Scanf("%d%d", &a, &b)
        fmt.Println(a+b)
    }
    

    C#(C# Mono

    using System;
    
    public class APlusB{
        private static void Main(){
            string[] input = Console.ReadLine().Split(' ');
            Console.WriteLine(int.Parse(input[0]) + int.Parse(input[1]));
        }
    }
    

    Visual Basic(Visual Basic Mono

    Imports System
    
    Module APlusB
        Sub Main()
            Dim ins As String() = Console.ReadLine().Split(New Char(){" "c})
            Console.WriteLine(Int(ins(0))+Int(ins(1)))
        End Sub
    End Module
    

    Kotlin

    fun main(args: Array<String>) {
        val (a, b) = readLine()!!.split(' ').map(String::toInt)
        println(a + b)
    }
    

    Haskell

    main = do
        [a, b] <- (map read . words) `fmap` getLine
        print (a+b)
    
  • 3
    @ 2025-5-11 10:39:40

    萌新不要查看此题解!

    (此题解仅为娱乐,请勿模仿)

    看到大佬们用10行以下代码解,本蒟蒻用50行解

    有请老朋友dijkstra出场(XD

    
    
    #include<bits/stdc++.h>
    #define N 100005
    using namespace std;
    vector<pair<int,int> >a[N];//邻接表
    int t[N];
    bool st[N];
    void dijkstra(int s)//dijkstra求a+b,此处用的是优先队列优化解法
    {
    	memset(t,0x3f,sizeof(t));//初始化st数组
    	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;//定义一个优先队列
    	q.push({0,s});//插入当前点
    	t[s]=0;
    	while (!q.empty())//广搜
    	{
    		auto x=q.top();
    		q.pop();
    		int ver=x.second;
    		if (st[ver]==1)//如果遍历过了就跳出循环
    		{
    			continue;
    		}
    		st[ver]=1;
    		for (auto &i:a[ver])//遍历下面节点
    		{
    			int j=i.first;
    			int w=i.second;
    			if (t[j]>t[ver]+w)//替换大值,取小值
    			{
    				t[j]=t[ver]+w;
    				q.push({t[j],j});//进入队列
    			}
    		}
    	}
    }
    int main()
    {
    	int x,y;
    	cin>>x>>y;
    	int i;
    	a[1].push_back({2,y});//插入邻接表
        a[2].push_back({1,x});
        int sum=0;
        dijkstra(1);//dijkstra1和2分别遍历2次,以取sum值
        sum+=t[2];
        memset(st,0,sizeof(st));
        dijkstra(2);
        sum+=t[1];
    	cout<<sum;//输出总和
        return 0;
    }
    
    

    100pts拿下a+b!

    • @ 2025-5-11 10:43:50

      30s的代码给你变成30min了是吧

  • 3
    @ 2024-12-22 10:54:42

    emm,hehe

    #include<bits/stdc++.h>//头文件
    using namespace std;//开拓空间
    long long a,b;//变量
    int main(){//主函数
    	cin>>a>>b;//输入
    	cout<<a+b;//输出
    	return 0;//完结撒花···
    }
    
    
    • 3
      @ 2024-12-22 10:46:13

      做出这道题,那么恭喜你踏上了万里征程的第一步!

      这是一道适合入门级选手练手的题目,本篇题解为大家介绍 cin 和 cout 。

      cin :标准输入流。

      输入:cin>>变量名;
      多个输入:cin>>变量名1>>变量名2……;

      cout :标准输出流。

      输出变量:cout<<变量名;
      输出单个字符:cout<<'字符';
      输出一串字符:cout<<"一串字符";
      输出换行:cout<<endl;

      本题代码及解析

      #include<iostream>//头文件,可调用cin、cout
      using namespace std;//标准命名空间 
      int main(){//主函数 
      	int a,b;//定义变量 
      	cin>>a>>b;//输入a,b 
      	cout<<a+b;//输出a,b之和 
      	return 0;//返回0 
      }
      
      • 0
        @ 2025-8-1 11:16:40

        `

        #include<bits/stdc++.h>
        using namespace std;
        int main(){
        	int a,b;
        	cin>>a>>b;
        	int a1,b1;
        	a1=a;
        	b1=b;
        	a1*=b1;//a*b
        	b1*=a1;//a*b*b
        	a1=b1;//a*b*b
        	b1/=a1;//1
        	b1*=a;//a
        	a1/=b1;//b*b
        	int t=b1;//a
        	b1=a1;//b*b
        	b1/=b;//b
        	a1=t;//a
        	cout<<a1+b1;//(自欺欺人)
        	return 0; 
        }
        

        `

        • 0
          @ 2024-12-22 11:08:09

          第一步:改变语言

          初始语言是Bash,改变它为C++

          第二步:写代码(如下)

          #include<bits/stdc++.h>
          using namespace std;
          
          int a,b;//定义a、b两个变量 
          
          int main(){
          	cin >> a >> b;//输入
          	cout << a+b ;//输出a+b的值
          	return 0;//以防万一 
          }
          
          • -4
            @ 2024-12-22 10:47:15

            祝各位 AK IOI。

            #include<bits/stdc++.h>
            #define int long long
            #define INF 0x3f3f3f
            using namespace std;
            int a,b;
            signed main(){
            	cin>>a>>b;//读入
            	cout<<a+b;//输出
            	return 0;
            }
            

            upd on 2025.1.1:本题有多种不同的解法,比如高精度,甚至还有树状数组、线段树、二叉搜索树等。(

            • 1

            信息

            ID
            1
            时间
            1000ms
            内存
            64MiB
            难度
            4
            标签
            递交数
            81
            已通过
            35
            上传者