算法
(模拟) O(1)
可能有些同学对于stdin/stdout
不太熟悉,我们在这里给出所有编程语言的样例程序。
C++ 代码
#include <iostream>
using namespace std;
int main () {
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
C 代码
#include <stdio.h>
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", a + b);
return 0;
}
Java 代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
Scanner cin=new Scanner(System.in);
var a = cin.nextInt();
var b = cin.nextInt();
System.out.println(a + b);
}
}
Python 代码
import sys
for line in sys.stdin:
print sum(map(int, line.split()))
Python3 代码
import sys
for line in sys.stdin:
print(sum(map(int, line.split())))
Javascript 代码
let fs = require('fs');
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
buf.split('\n').forEach(function(line) {
let tokens = line.split(' ').map(function(x) { return parseInt(x); });
if (tokens.length != 2) return;
console.log(tokens.reduce(function(a, b) { return a + b; }));
});
});
Go 代码
package main
import "fmt"
func main() {
var a, b int
for {
n, _ := fmt.Scanf("%d %d", &a, &b)
if n != 2 { break }
fmt.Println(a + b)
}
}
Scanf("%d %d", &a, &b)
Go的风格与C相似
看上去好眼熟啊
orz
#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); //连边 Link connect(A,B); //断边 Cut cut(A,B); //再连边orz Link again connect(A,B); printf("%d\n",query(A,B)); return 0; }
没人写LCT他会哭的QAQ
很棒。
python 代码量最少 但是可读性一般吧
dsfs
c++写多了,感觉其他的都好奇怪
#C++代码:
//-----鸡你太美----- #define KunKun using #define ShiBuShi namespace #define YouBing std #define basketball int #define NiGanMa main #define XiaoHeiZi cin #define Ji cout #define Mei endl #define EiYou atexit #define ZiDongJi auto #define MeiShi void //-----你干嘛----- #include<bits/stdc++.h> KunKun ShiBuShi YouBing; basketball Ni,Tai; ZiDongJi Oh=[]{ XiaoHeiZi>>Ni>>Tai; }; MeiShi NiYouGanMa(){ Ji<<Ni+Tai<<Mei; } basketball NiGanMa(){ Oh(); EiYou(NiYouGanMa); }
define
:宏定义,可以理解成简单替换。#define a 10 cout<<a;//OK,equ cout<<10; a++;//Wrong,equ 10++;
还可以宏定义函数:
#define cir(i,l,r) for(int i=l;i<=r;i++) cir(i,1,10){ cout<<i<<' '; } //OK,output 1 2 3 4 5 6 7 8 9 10
注意多加
(括号)
!#define f(a,b) a*b int c; c=f(1,2+3);//equ 1*2+3,not 1*(2+3) #undef f//undef,撤销宏定义 #define f(a,b) (a)*(b)//OK
19~21行的代码是一个
Lambda
,可以理解为inline函数。[]{ cout<<"Hello Lambda!" }(); //OK,output Hello Lambda! auto l=[]{ cout<<"Hello Lambda!" }; l();//OK,output Hello Lambda!
Lambda
可以有自己的参数:auto l=[](int i){ return i; }; int c=l(10)//OK,c=10
Lambda
可以指定返回值类型。auto l=[]()->double{ return 233; }; //return double(233)
当然,
Lambda
的[]
也有用,但太复杂,就不讲了,大家可以看看资料包。Lambda
资料包atexit
:可以在程序结束时调用一个void
函数。void output(){ cout<<"鸡你太美"; } atexit(output);//output 鸡你太美,程序结束。
再介绍两个函数:
exit
,_Exit
:直接结束程序。最后两个🐥巧:
printf
有返回值,正常情况是非零整数,所以可以:return !printf("1"); exit(!printf("1")); _Exit(!printf("1"));
当
return
有多个参数时,它只用最后一个:return 9,"鸡",7,"Hello World","你",4,"太",2,"asdfjkl","美",0;//equ return 0;
最后问问大家两个问题:
1:你们有什么压行🐥巧?
2:我发的题解只能被自己看见,有什么办法解决这个问题?
#The End~
咯咯哒~(只因叫)
A + B这个怎么用JavaScript去写,为什么我在vscode的写的能运行,在acwing写的运行错误
为什么每个oj第一题都是a+b呢?因为他最基础吗
在洛谷上第一题不是超级玛丽游戏吗
有道理
小明 oj 的第 1 题绝不是 a+b
链接网址:https://www.xmoj.tech/problem.php?id=1001
P1001是第一题,P1000是第零题hh闫老师,Python3代码太长,直接
···
a,b=map(int,input.split())
print(a+b)
```
也可以用int A,B,sum;
https://cdn.acwing.com/static/web/img/icons/emoji/thumbsup.png
大老受我一拜
python3更简单代码:
print(input()+input())
https://www.acwing.com/solution/content/69403/\
好牛
python是不是最简洁?
orz,GO的风格好像
g++Javascript Node版 ps.网站提供的没有v8,语言写Javascirpt(Node)更严谨一些
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.on(‘line’, function(line){
let arr = line.split(‘ ‘).map(Number);
console.log(arr[0]+arr[1]);
})
还是C++😘
Javascript劝退…
人生苦短,我用python