166 条题解

  • 129
    @ 2021-10-5 13:19:34

    尽管 A + B Problem 是十分经典的入门题目,也希望大家能够在题解区友善交流。 HydroOJ 致力于提供一个友善的交流学习平台,请不要发布误导性题解,谢谢! 您可以给您喜欢的题解投票,让更多的人看到它们。同时,题解的评分也会影响你在排名中的贡献一栏分数。

    我们认为优质的题解需要:代码高亮,简洁而有效的注解,思路清晰,便于理解,无冗长板子,不定义非常用宏。

    • 37
      @ 2021-10-13 17:00:49

      简单的入门测试题

      本题各种语言的程序写法:(不知道Hydro有没有这些语言

      C

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

      C++

      #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.
      

      Python2

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

      Python3

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

      Java

      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);
      }
      

      Go

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

      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 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)
      

      Scala

      object Main extends App {
          println(scala.io.StdIn.readLine().split(" ").map(_.toInt).sum)
      }
      

      Perl

      my $in = <STDIN>;
      chomp $in;
      $in = [split /[\s,]+/, $in];
      my $c = $in->[0] + $in->[1];
      print "$c\n";
      

      文言

      施「require('fs').readFileSync」於「「/dev/stdin」」。名之曰「數據」。
      施「(buf => buf.toString().trim())」於「數據」。昔之「數據」者。今其是矣。
      施「(s => s.split(' '))」於「數據」。昔之「數據」者。今其是矣。
      注曰。「「文言尚菜,無對象之操作,故需 JavaScript 之语法」」。
      
      夫「數據」之一。取一以施「parseInt」。名之曰「甲」。
      夫「數據」之二。取一以施「parseInt」。名之曰「乙」。
      
      加「甲」以「乙」。書之。
      
      • @ 2024-1-25 17:08:05

        我的Python3

        print(int(input()) + int(input())
        

        得了(一行代码)

      • @ 2024-2-3 16:55:12

        只要你想就有

      • @ 2024-7-23 20:03:54

        @ 你这样不行,要加split

      • @ 2024-7-29 15:26:31

        文言……

      • @ 2024-10-28 16:22:19

        python代码是不是得在split的括号里加个空格啊 【感谢点拨】

      • @ 2024-11-27 22:07:19

        @ 这个写法不满足题目输入要求,要求输入一行,两个数之间用空格隔开。

        你程序写法需要在每输入第一个数后,按下回车键。

    • 25
      @ 2021-8-24 13:40:55

      这题很简单,输入 aabb,然后输出他们的和即可。

      #include<iostream> //头文件
      using namespace std; //命名空间
      int main(){ //主函数,程序从这里开始
          int a,b; //定义变量
          cin>>a>>b; //输入
          cout<<a+b<<endl; //输出他们的和
          return 0; //主函数需要返回0
      }
      

      管理大大求过

      • @ 2023-12-31 14:00:33

        这道题应该用long long类型的变量进行存储因为数值范围是10610^6,int存不下,建议改一下

      • @ 2024-1-30 13:44:38

        @。 在大多数编程环境中,int 类型的大小通常为 4 字节(32 位),其范围为 -2,147,483,648 到 2,147,483,647(常见的采用补码表示法)。这是根据 C99 标准中的 INT_MIN(最小值)和 INT_MAX(最大值)来定义的。

    • 11
      @ 2021-3-7 18:45:04
      #include<bits/stdc++.h>//万能头文件
      using namespace std;//使用标准命名空间
      int main()//主函数
      {
      	int a,b;
      	cin>>a>>b;
      	cout<<a+b;
      	return 0;//好习惯
      }
      
      • 5
        @ 2021-11-1 16:48:34

        请大家不要发恶搞题解来误导新手,谢谢!

        Python 题解,输入 aabb,输出 a+ba+b
        提醒一句,Python 需要用 split() 切片,而且要转成 int 型。

        a=input().split()
        print(int(a[0])+int(a[1]))
        
        • @ 2024-1-18 21:02:38

          你可以在定义时就转成int

          a=int(input())
          
        • @ 2024-12-13 21:42:21

          @ 错啦,是map(int, input().split())

      • 2
        @ 2023-6-25 17:53:41

        c++:

        # include <bits/stdc++.h>
        using namespace std;
        int main() {
            int a, b;
            scanf("%d%d", &a, &b);
            printf("%d", a + b);
            return 0;
        }
        

        c++极限两行:

        # include <bits/stdc++.h>
        using namespace std;int main() {int a, b;scanf("%d%d", &a, &b);printf("%d", a + b);return 0;}
        

        python:

        # 加;是个人习惯
        a, b = map(int, input().split());
        print(a + b, end='');
        
        • @ 2024-7-23 20:08:20

          C++还可以更短

          #import<iostream>
          main(){int a,b;std::cin>>a>>b;cout<<a+b;}
          
        • @ 2024-7-29 15:32:02

          @ 你试过能编译了么?你cout前面的std::呢?

        • @ 2024-12-6 18:24:23

          C++还可以更短:

          main(){int a,b;__builtin_scanf("%d%d",&a,&b);__builtin_printf("%d",a+b);return 0;}
          
      • 1
        @ 2024-8-7 19:35:21

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

        这是一道适合入门级选手练手的题目,本篇题解为大家介绍 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

          饿的方法方法反反复复方法方法发普通3也非他与i个人原因人与人国语歌坛要通过如果任由规范哥哥发一个

          • 0
            @ 2025-2-13 10:35:01

            这题只要输出两数之和就行了,代码:

            #include<bits/stdc++.h>
            using namespace std;
            signed main(){
            	int a, b;
            	cin >> a >> b;
            	cout << a + b;
            	return 0;
            }
            
            
            • 0
              @ 2024-12-29 14:17:40

              c++

              #include<bits/stdc++.h>
              typedef long long ll;
              using namespace std;
              ll a,b;
              int main(){
              	ios::sync_with_stdio(false);
              	cin.tie(0),cout.tie(0);
              	cin>>a>>b;
              	cout<<a+b<<endl;
              	return 0;
              }
              • 0
                @ 2024-12-13 18:03:57

                这真的很简单!!!

                只需要先输入a和b,然后输出a+b的和就行了

                代码如下 C++

                #include<bits/stdc++.h>
                using namespace std;
                
                int main(){
                    int a,b;
                    cin >> a >> b;
                    cout << a+b;//输出a+b的和
                    return 0;
                }
                
                • 0
                  @ 2024-12-5 14:58:48

                  这是一个新手题

                  C++

                  #include<iostream> //头文件
                  using namespace std; //存储空间
                  int main()  //主函数
                  {
                      int a,b; //定义
                      cin >> a >> b; //输入
                      cout << a + b; //输出
                      return 0; //结束
                  }
                  

                  这个题很适合新手学习C++

                  • 0
                    @ 2024-11-26 20:59:28

                    空间和时间都省到极致了各位!

                    AC Code:

                    #include<iostream>
                    int main(){
                        int a,b;
                        std::cin>>a>>b;//输入a和b
                        std::cout<<a+b<<std::endl;//输出a+b的结果
                        return 0;
                    }
                    

                    原创不易,切勿抄袭

                    不会这还有人抄吧

                    • 0
                      @ 2024-11-16 0:31:10
                      #include<iostream>
                      #define 声明32位整型 int
                      #define 左圆括号 (
                      #define 右圆括号 )
                      #define 左花括号 {
                      #define 右花括号 }
                      #define 使用 using
                      #define 名字空间 namespace
                      #define 输入 cin>>
                      #define 输出 cout<<
                      #define 和 >>
                      #define 以及 <<
                      #define 声明64位整型 long long
                      #define 返回 return
                      #define 加 +
                      使用 名字空间 std;
                      声明32位整型 main 左圆括号 右圆括号
                      左花括号
                        声明64位整型 a,b;
                        输入 a 和 b;
                        输出 a 加 b;
                        返回 0;
                      右花括号
                      
                      • 0
                        @ 2024-7-28 19:47:33

                        弱爆了 输出cout << a + b;

                        • 0
                          @ 2023-10-15 17:46:10
                          #include<iostream>//头文件
                          using namespace std;//命名空间
                          int main()
                          {
                               int a, b;//定义
                               cin>> a>> b;//输入
                               cout<< a + b;//输出
                               return 0;
                          }
                          
                          • 0
                            @ 2023-10-10 22:50:59
                            /*****************
                            备注:
                            *****************/
                            #include <iostream>
                            using namespace std;
                            #define LL long long
                            #define MAXM 3010
                            #define MAXN 3010
                            const int N =1e5+10;
                            const int INF =0x3f3f3f3f;
                            int main ()
                            {
                                LL a,b;
                                cin>>a>>b;
                                cout<<a+b;
                               return 0;
                            }
                            
                            
                            • 0
                              @ 2023-10-2 13:53:11
                              #include<bits/stdc++.h> //引入头文件
                              using namespace std; //设置namespace
                              
                              int main(){ //主函数
                                  int a,b; //定义变量
                                  cin >>a >> b; //输入
                                  cout << a+b; //输出
                                  return 0; //结束代码
                              }
                              
                              • 0
                                @ 2023-9-24 11:48:26

                                A+B Problem

                                题解+思路

                                题解

                                #include <iostream>
                                using namespace std;
                                
                                struct Node
                                {
                                    int data;
                                    Node *prev;
                                    Node *next;
                                    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
                                };
                                
                                Node *createList(int num)
                                {
                                    Node *head = nullptr;
                                    Node *tail = nullptr;
                                    while (num > 0)
                                    {
                                        int digit = num % 10;
                                        Node *newNode = new Node(digit);
                                        if (head == nullptr)
                                        {
                                            head = newNode;
                                            tail = newNode;
                                        }
                                        else
                                        {
                                            newNode->next = head;
                                            head->prev = newNode;
                                            head = newNode;
                                        }
                                        num /= 10;
                                    }
                                    return head;
                                }
                                
                                Node *addTwoNumbers(Node *num1, Node *num2)
                                {
                                    Node *result = nullptr;
                                    Node *current = nullptr;
                                    int carry = 0;
                                
                                    while (num1 != nullptr || num2 != nullptr || carry != 0)
                                    {
                                        int sum = carry;
                                
                                        if (num1 != nullptr)
                                        {
                                            sum += num1->data;
                                            num1 = num1->next;
                                        }
                                
                                        if (num2 != nullptr)
                                        {
                                            sum += num2->data;
                                            num2 = num2->next;
                                        }
                                
                                        carry = sum / 10;
                                        sum %= 10;
                                
                                        Node *newNode = new Node(sum);
                                
                                        if (result == nullptr)
                                        {
                                            result = newNode;
                                            current = newNode;
                                        }
                                        else
                                        {
                                            current->prev = newNode;
                                            newNode->next = current;
                                            current = newNode;
                                        }
                                    }
                                
                                    return result;
                                }
                                
                                void printList(Node *head)
                                {
                                    if (head == nullptr)
                                    {
                                        cout << "Empty list" << endl;
                                        return;
                                    }
                                
                                    while (head != nullptr)
                                    {
                                        cout << head->data;
                                        head = head->next;
                                    }
                                    cout << endl;
                                }
                                
                                void deleteList(Node *head)
                                {
                                    while (head != nullptr)
                                    {
                                        Node *temp = head;
                                        head = head->next;
                                        delete temp;
                                    }
                                }
                                
                                int main()
                                {
                                    int num1 = 12345;
                                    int num2 = 6789;
                                
                                    Node *list1 = createList(num1);
                                    Node *list2 = createList(num2);
                                
                                    cout << "Number 1: ";
                                    printList(list1);
                                
                                    cout << "Number 2: ";
                                    printList(list2);
                                
                                    Node *sumList = addTwoNumbers(list1, list2);
                                
                                    cout << "Sum: ";
                                    printList(sumList);
                                
                                    deleteList(list1);
                                    deleteList(list2);
                                    deleteList(sumList);
                                
                                    return 0;
                                }
                                

                                思路

                                1. 首先,定义一个双向链表的节点结构体,包含一个整数值和两个指针,分别指向前一个节点和后一个节点。
                                2. 创建两个双向链表,分别表示要相加的两个数字。可以通过遍历输入的整数,从个位开始,逐个创建节点并将其插入链表中。
                                3. 定义一个变量来表示进位,初始值为0。
                                4. 从链表的最后一个节点开始,逐位相加,并将结果存储在新的链表中。具体步骤如下: - 从两个链表的最后一个节点开始,分别取出对应的值,并加上进位。 - 将相加的结果对10取模得到当前位的值,同时更新进位为相加结果除以10的商。 - 创建一个新的节点,将当前位的值存储在该节点中,并将该节点插入到结果链表的头部。 - 分别将两个链表的指针指向上一个节点,继续下一位的相加操作。 - 如果其中一个链表已经遍历完,但另一个链表还有剩余位数,则将剩余位数与进位相加,并将结果插入到结果链表中
                                5. 最后,得到的结果链表即为相加后的结果。
                                • 0
                                  @ 2023-8-12 15:01:05

                                  Python 压行技巧,详细讲一讲:

                                  1. split 函数,可以将字符串按一定分隔符分割,放在一个 list 中。例如,s'abc.abcd',那么 s.split ('.') 就是 ['abc', 'abcd'],如果没有参数,默认为 ' '
                                  2. map 函数,可以将一个序列依次进行某个操作的最终序列。例如,a[1, 1, 4, 5, 1, 4]func 函数定义如下:
                                    def func (int x):
                                        return x + 1
                                    
                                    那么 map (func, a) 就是 [2, 2, 5, 6, 2, 5]
                                  3. sum 函数,可以求一个序列的和。例如,按照上面的 a,那么 sum (a) 就是 16

                                  最终代码(注释参考样例一,不含注释一行):

                                   print(sum(map(int,input().split())))
                                  #print(sum(map(int, '1 2' .split())))
                                  #print(sum(map(int,   ['1', '2']  )))
                                  #print(sum(         [1, 2]        )))
                                  #print(             2               )
                                  

                                  信息

                                  ID
                                  56
                                  时间
                                  1000ms
                                  内存
                                  1024MiB
                                  难度
                                  1
                                  标签
                                  递交数
                                  9734
                                  已通过
                                  4392
                                  上传者