175 条题解

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

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

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

    • 35
      @ 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

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

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

      • @ 2025-3-15 19:23:14

        文言!hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha

    • 26
      @ 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())

      • 1
        @ 2025-3-31 21:40:36

        声明:后几种方法一般不用

        C++题解

        这里采用多种方法求解。

        方法一

        读入a和b,输出a+b。

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

        如果你看不懂上面的代码,那你可以看看下面的。

        #include <bits/stdc++.h>
        using namespace std;
        
        #define 开始啦 main()
        #define 开头 {
        #define 结尾 }
        #define 和 , 
        #define 没了 return 0
        #define 整数32位 int
        #define 输入 cin
        #define 输出 cout 
        #define 一个 >> 
        #define 这个 <<
        #define 加上 +
        #define 换行 '\n' 
        
        整数32位 开始啦
        开头
        	整数32位 a 和 b;
        	输入 一个 a 一个 b;
        	输出 这个 a 加上 b 这个 换行; 
        	没了;
        结尾
        

        方法二

        数手指都会吧,我们就用“数手指”的方法做这一题。

        #include <bits/stdc++.h>
        using namespace std;
        
        int a, b, ans = 0;
        
        int main() {
        	scanf("%d%d", &a, &b);
        	for (int i = 1; i <= a; i++)
        		ans++;
        	for (int j = 1; j <= b; j++)
        		ans++;
        	printf("%d\n", ans);
        	return 0;
        }
        

        方法三

        算法数据结构的角度来思考一下这一题……

        我们可以设计一个这样的算法:先将a和b插入到一个数据结构(栈、队列或堆)中,非空时,将头上的元素取出,如果大于1则拆成它减1和1(根据自己的喜好)再插入,否则统计答案。

        写法

        #include <bits/stdc++.h>
        using namespace std;
        
        int a, b, ans = 0;
        stack<int> s;
        
        int main() {
        	scanf("%d%d", &a, &b);
        	s.push(a);
        	s.push(b);
        	while (!s.empty()) {
        		int x = s.top();
        		s.pop();
        		if (x > 1) {
        			s.push(x - 1);
        			s.push(1);
        		}
        		else
        			ans += x; 
        	}
        	printf("%d\n", ans);
        	return 0;
        }
        

        队列写法

        #include <bits/stdc++.h>
        using namespace std;
        
        int a, b, ans = 0;
        queue<int> q;
        
        int main() {
        	scanf("%d%d", &a, &b);
        	q.push(a);
        	q.push(b);
        	while (!q.empty()) {
        		int x = q.front();
        		q.pop();
        		if (x > 1) {
        			q.push(x - 1);
        			q.push(1);
        		}
        		else
        			ans += x; 
        	}
        	printf("%d\n", ans);
        	return 0;
        }
        

        写法

        #include <bits/stdc++.h>
        using namespace std;
        
        int a, b, ans = 0;
        priority_queue<int> h;
        
        int main() {
        	scanf("%d%d", &a, &b);
        	h.push(a);
        	h.push(b);
        	while (!h.empty()) {
        		int x = h.top();
        		h.pop();
        		if (x > 1) {
        			h.push(x - 1);
        			h.push(1);
        		}
        		else
        			ans += x; 
        	}
        	printf("%d\n", ans);
        	return 0;
        }
        

        方法四

        我们可以使用高精度加法。

        #include <bits/stdc++.h>
        using namespace std; 
        
        char s1[100], s2[100];
        int a[100], b[100], c[100];
        
        int main() {
            scanf("%s%s", s1 + 1, s2 + 1);
            int la = strlen(s1 + 1), lb = strlen(s2 + 1), lc;
            lc = max(la, lb);
        	for (int i = 1; i <= la; i++)
        		a[i] = s1[la - i + 1] - '0';
        	for (int i = 1; i <= lb; i++)
        		b[i] = s2[lb - i + 1] - '0';
        	memset(c, 0, sizeof(c));
        	for (int i = 1; i <= lc; i++) {
        		c[i] += a[i] + b[i];
        		c[i + 1] = c[i] / 10;
        		c[i] %= 10;
        	}
        	if (c[lc + 1] > 0)
        		lc++;
        	for (int i = lc; i >= 1; i--)
        		printf("%d", c[i]);
        	printf("\n");
        	return 0;
        }
        

        方法五

        方法三方法四结合。

        #include <bits/stdc++.h>
        using namespace std;
        
        int a, b, c[100], lc = 1;
        priority_queue<int> h;
        
        int main() {
        	scanf("%d%d", &a, &b);
        	h.push(a);
        	h.push(b);
        	memset(c, 0, sizeof(c));
        	while (!h.empty()) {
        		int x = h.top();
        		h.pop();
        		if (x > 1) {
        			h.push(x - 1);
        			h.push(1);
        		}
        		else {
        			c[1] += x;
        			for (int i = 1; i <= lc; i++) {
        				c[i + 1] += c[i] / 10;
        				c[i] %= 10;
        			}
        			if (c[lc + 1] > 0)
        				lc++;
        		}
        	}
        	for (int i = lc; i >= 1; i--)
        		printf("%d", c[i]);
        	printf("\n");
        	return 0;
        }
        

        你竟然认真的看完了!

        恭喜你,把简单的问题复杂化了!要是你真的看懂了,就说明你太没事闲的了

        • 1
          @ 2023-11-3 21:22:55

          LCT解法

          #include <iostream>
          #include <cstring>
          #include <cstdio>
          #include <cstring>
          using namespace std;
          struct node 
          {
              int data,rev,sum;
              node *son[2],*pre;
              bool judge();
              bool isroot();
              void pushdown();
              void update();
              void setson(node *child,int lr);
          }lct[233];
          int top,a,b;
          node *getnew(int x)
          {
              node *now=lct+ ++top;
              now->data=x;
              now->pre=now->son[1]=now->son[0]=lct;
              now->sum=0;
              now->rev=0;
              return now;
          }
          bool node::judge(){return pre->son[1]==this;}
          bool node::isroot()
          {
              if(pre==lct)return true;
              return !(pre->son[1]==this||pre->son[0]==this);
          }
          void node::pushdown()
          {
              if(this==lct||!rev)return;
              swap(son[0],son[1]);
              son[0]->rev^=1;
              son[1]->rev^=1;
              rev=0;
          }
          void node::update(){sum=son[1]->sum+son[0]->sum+data;}
          void node::setson(node *child,int lr)
          {
              this->pushdown();
              child->pre=this;
              son[lr]=child;
              this->update();
          }
          void rotate(node *now)
          {
              node *father=now->pre,*grandfa=father->pre;
              if(!father->isroot()) grandfa->pushdown();
              father->pushdown();now->pushdown();
              int lr=now->judge();
              father->setson(now->son[lr^1],lr);
              if(father->isroot()) now->pre=grandfa;
              else grandfa->setson(now,father->judge());
              now->setson(father,lr^1);
              father->update();now->update();
              if(grandfa!=lct) grandfa->update();
          }
          void splay(node *now)
          {
              if(now->isroot())return;
              for(;!now->isroot();rotate(now))
              if(!now->pre->isroot())
              now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
          }
          node *access(node *now)
          {
              node *last=lct;
              for(;now!=lct;last=now,now=now->pre)
              {
                  splay(now);
                  now->setson(last,1);
              }
              return last;
          }
          void changeroot(node *now)
          {
              access(now)->rev^=1;
              splay(now);
          }
          void connect(node *x,node *y)
          {
              changeroot(x);
              x->pre=y;
              access(x);
          }
          void cut(node *x,node *y)
          {
              changeroot(x);
              access(y);
              splay(x);
              x->pushdown();
              x->son[1]=y->pre=lct;
              x->update();
          }
          int query(node *x,node *y)
          {
              changeroot(x);
              node *now=access(y);
              return now->sum;
          }
          int main()
          {
              scanf("%d%d",&a,&b);
              node *A=getnew(a);
              node *B=getnew(b);
                  connect(A,B);
                  cut(A,B);
                  connect(A,B);
              printf("%d\n",query(A,B)); 
              return 0;
          }
          
          • 1
            @ 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;}
              
          • 0
            @ 2025-4-6 9:51:46
            #include<bits/stdc++.h>
            #define _rep(i,a,b) for(int i=(a);i<=(b);i++)
            #define _antirep(i,a,b) for(int i=(a);i>=(b);i--)
            using namespace std;
            typedef long long lxl; 
            string a,b,ans;
            int x;
            int main()
            { 
            	cin>>a>>b;
            	int len=max(a.size(),b.size()); 
            	while(a.size()<len) a='0'+a;//补零
            	while(b.size()<len) b='0'+b;//补零
            	_antirep(i,len-1,0)
            	{
            		int c=a[i]-'0',d=b[i]-'0';
            		ans=(char)((c+d+x)%10+'0')+ans;
            		x=(c+d+x)/10;
            	}
            	if(x!=0) ans=(char)(x+'0')+ans;
            	cout<<ans;
              return 0;
            }
            
            
            • 0
              @ 2025-4-1 19:28:29
              #include <algorithm>
              #include <bitset>
              #include <cctype>
              #include <cerrno>
              #include <clocale>
              #include <cmath>
              #include <complex>
              #include <cstdio>
              #include <cstdlib>
              #include <cstring>
              #include <ctime>
              #include <deque>
              #include <exception>
              #include <fstream>
              #include <functional>
              #include <limits>
              #include <list>
              #include <map>
              #include <iomanip>
              #include <ios>
              #include <iosfwd>
              #include <iostream>
              #include <istream>
              #include <ostream>
              #include <queue>
              #include <set>
              #include <sstream>
              #include <stack>
              #include <stdexcept>
              #include <streambuf>
              #include <string>
              #include <utility>
              #include <vector>
              #include <cwchar>
              #include <cwctype>
              using namespace std;
              int a,b;
              int main()
              {
                  cin>>a>>b;
              	cout<<a+b;
                  return 0;
              }
              

              用上了所有知道的头文件

              • 0
                @ 2025-3-31 21:40:11

                声明:后几种方法一般不用

                C++题解

                这里采用多种方法求解。

                方法一

                读入a和b,输出a+b。

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

                如果你看不懂上面的代码,那你可以看看下面的。

                #include <bits/stdc++.h>
                using namespace std;
                
                #define 开始啦 main()
                #define 开头 {
                #define 结尾 }
                #define 和 , 
                #define 没了 return 0
                #define 整数32位 int
                #define 输入 cin
                #define 输出 cout 
                #define 一个 >> 
                #define 这个 <<
                #define 加上 +
                #define 换行 '\n' 
                
                整数32位 开始啦
                开头
                	整数32位 a 和 b;
                	输入 一个 a 一个 b;
                	输出 这个 a 加上 b 这个 换行; 
                	没了;
                结尾
                

                方法二

                数手指都会吧,我们就用“数手指”的方法做这一题。

                #include <bits/stdc++.h>
                using namespace std;
                
                int a, b, ans = 0;
                
                int main() {
                	scanf("%d%d", &a, &b);
                	for (int i = 1; i <= a; i++)
                		ans++;
                	for (int j = 1; j <= b; j++)
                		ans++;
                	printf("%d\n", ans);
                	return 0;
                }
                

                方法三

                算法数据结构的角度来思考一下这一题……

                我们可以设计一个这样的算法:先将a和b插入到一个数据结构(栈、队列或堆)中,非空时,将头上的元素取出,如果大于1则拆成它减1和1(根据自己的喜好)再插入,否则统计答案。

                写法

                #include <bits/stdc++.h>
                using namespace std;
                
                int a, b, ans = 0;
                stack<int> s;
                
                int main() {
                	scanf("%d%d", &a, &b);
                	s.push(a);
                	s.push(b);
                	while (!s.empty()) {
                		int x = s.top();
                		s.pop();
                		if (x > 1) {
                			s.push(x - 1);
                			s.push(1);
                		}
                		else
                			ans += x; 
                	}
                	printf("%d\n", ans);
                	return 0;
                }
                

                队列写法

                #include <bits/stdc++.h>
                using namespace std;
                
                int a, b, ans = 0;
                queue<int> q;
                
                int main() {
                	scanf("%d%d", &a, &b);
                	q.push(a);
                	q.push(b);
                	while (!q.empty()) {
                		int x = q.front();
                		q.pop();
                		if (x > 1) {
                			q.push(x - 1);
                			q.push(1);
                		}
                		else
                			ans += x; 
                	}
                	printf("%d\n", ans);
                	return 0;
                }
                

                写法

                #include <bits/stdc++.h>
                using namespace std;
                
                int a, b, ans = 0;
                priority_queue<int> h;
                
                int main() {
                	scanf("%d%d", &a, &b);
                	h.push(a);
                	h.push(b);
                	while (!h.empty()) {
                		int x = h.top();
                		h.pop();
                		if (x > 1) {
                			h.push(x - 1);
                			h.push(1);
                		}
                		else
                			ans += x; 
                	}
                	printf("%d\n", ans);
                	return 0;
                }
                

                方法四

                我们可以使用高精度加法。

                #include <bits/stdc++.h>
                using namespace std; 
                
                char s1[100], s2[100];
                int a[100], b[100], c[100];
                
                int main() {
                    scanf("%s%s", s1 + 1, s2 + 1);
                    int la = strlen(s1 + 1), lb = strlen(s2 + 1), lc;
                    lc = max(la, lb);
                	for (int i = 1; i <= la; i++)
                		a[i] = s1[la - i + 1] - '0';
                	for (int i = 1; i <= lb; i++)
                		b[i] = s2[lb - i + 1] - '0';
                	memset(c, 0, sizeof(c));
                	for (int i = 1; i <= lc; i++) {
                		c[i] += a[i] + b[i];
                		c[i + 1] = c[i] / 10;
                		c[i] %= 10;
                	}
                	if (c[lc + 1] > 0)
                		lc++;
                	for (int i = lc; i >= 1; i--)
                		printf("%d", c[i]);
                	printf("\n");
                	return 0;
                }
                

                方法五

                方法三方法四结合。

                #include <bits/stdc++.h>
                using namespace std;
                
                int a, b, c[100], lc = 1;
                priority_queue<int> h;
                
                int main() {
                	scanf("%d%d", &a, &b);
                	h.push(a);
                	h.push(b);
                	memset(c, 0, sizeof(c));
                	while (!h.empty()) {
                		int x = h.top();
                		h.pop();
                		if (x > 1) {
                			h.push(x - 1);
                			h.push(1);
                		}
                		else {
                			c[1] += x;
                			for (int i = 1; i <= lc; i++) {
                				c[i + 1] += c[i] / 10;
                				c[i] %= 10;
                			}
                			if (c[lc + 1] > 0)
                				lc++;
                		}
                	}
                	for (int i = lc; i >= 1; i--)
                		printf("%d", c[i]);
                	printf("\n");
                	return 0;
                }
                

                你竟然认真的看完了!

                恭喜你,把简单的问题复杂化了!要是你真的看懂了,就说明你太没事闲的了

                • 0
                  @ 2025-3-30 15:10:58

                  #include <bits/stdc++.h> using namespace std;

                  int main() { long long a, b;//定义。 cin >> a >> b;//输入。 cout << a + b;//输出。 return 0; }

                  • 0
                    @ 2025-3-27 6:01:40

                    此题求两数之和用 + 号,但我们可以用 sum 函数直接求和。 用Python写最简题解如下:

                    print(sum(list(map(int,input().split()))))
                    
                    • 0
                      @ 2025-3-12 12:54:38

                      一道简单的入门测试题。

                      头文件 <iostream> <bits/stdc++.h>均可。

                      #include<iostream>
                      using namespace std;
                      int main()
                      {
                      	int a,b;
                      	cin>>a>>b;
                      	cout<<a+b;
                      	
                      	return 0;
                      }
                      
                      #include<bits/stdc++.h>
                      using namespace std;
                      int main()
                      {
                      	int a,b;
                          scanf("%d%d",&a,&b);
                      	printf("%d\n",a+b);
                      	
                      	return 0;
                      }
                      
                      • 0
                        @ 2025-3-10 20:59:21

                        这道题非常的简单,但是我们可以使用不同的方法做,在完成题目的同时,也可以发散我们的思维。

                        首先第一步:打头文件

                        你既可以选择万能头文件#include<bits/stdc++.h>

                        也可以选择一般头文件#include<iostream>

                        第二步:补充

                        using namespace std;

                        第三步:写主函数

                        int main()
                        {
                            return 0;
                        }
                        

                        第四步:定义变量

                        int a,b;.

                        第五步:思考算法

                        从这一步开始,你就要开始思考解决这道问题有哪些不同的算法,并开始付诸实践,请看下文。

                        法一:直接输入A+BA+B,直接输出A+BA+B

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

                        法二:利用循环解决

                        #include<bits/stdc++.h>
                        #define int long long
                        using namespace std;
                        const int maxn=100010;
                        int a,b,n=1,c[maxn];
                        signed main(){
                        cin>>a>>b;
                        for(int i=1;i<=n;i++){
                        c[i]+=a+b;
                        }
                        cout<<c[1];
                        }
                        

                        法三:利用栈解决 思路很明确,将AABB入栈,在计算的时候,弹出栈顶,计算。

                        #include<bits/stdc++.h>
                        #define int long long
                        using namespace std;
                        const int maxn=100010;
                        stack<int> s;
                        int a,b,n;
                        signed main(){
                        cin>>a>>b;
                        s.push(a);
                        s.push(b);
                        while(!s.empty()){
                        n+=s.top();
                        s.pop();
                        }
                        cout<<n;
                        }
                        

                        法四:利用队列解决 思路很简单,利用队列,输入的时候把AA入队BB,在使用的时候,弹出队头再计算。

                        #include<bits/stdc++.h>
                        #define int long long
                        using namespace std;
                        const int maxn=100010;
                        queue<int> s;
                        int a,b,n;
                        signed main(){
                        cin>>a>>b;
                        s.push(a);
                        s.push(b);
                        while(!s.empty()){
                        n+=s.front();
                        s.pop();
                        }
                        cout<<n;
                        }
                        

                        法五:利用双端队列解决 思路很清晰,将AABB一个加入队头,一个加入队尾,使用的时候,将两端分别弹出即可。

                        #include<bits/stdc++.h>
                        #define int long long
                        using namespace std;
                        const int maxn=100010;
                        deque<int> s;
                        int a,b,n;
                        signed main(){
                        cin>>a>>b;
                        s.push_front(a);
                        s.push_back(b);
                        cout<<s.front()+s.back();
                        }
                        

                        运用不同的方法,解决同一道题目,不仅是收获了快乐,更重要的是发散了思维,熟练了算法。

                        • 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-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 
                                  }
                                  

                                  信息

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