177 条题解
-
42
这题教大家如何写出高级感,十分简单(doge)
#include <bits/stdc++.h> using namespace std; struct Plus { int a,b;//定义一个结构体 }; stack<int> sta;//定义一个栈 int main() { Plus c;//结构体变量 cin >> c.a >> c.b; sta.push(c.a);//入栈 sta.push(c.b); int s; s += sta.top();//输出栈顶元素 sta.pop();//出栈 s += sta.top(); cout << s;//输出总和 return 0; }
这题特别
难,以上为最简单的代码哦~ 不就是个STL嘛,太简单了,来个高难度的。 来个弱爆了的DFS#include <bits/stdc++.h> using namespace std; int n = 2, a[5], s; int dfs(int x, int sum) { if (x > n) return sum; int i = dfs(x + 1, sum); int j = dfs(x + 1, sum + a[x]); if (i == s) return i; if (j == s) return j; return -1; } int main() { for (int i = 1;i <= n; i++) scanf("%d", &a[i]), s += a[i]; cout << dfs(1, 0) << endl; return 0; }
三岁小孩
都会什么?不够刺激? 来个最短路的图论Floyd算法呗~#include <bits/stdc++.h> using namespace std; int d[5][5], n = 3; int main() { int a, b; scanf("%d%d", &a, &b); memset(d, 0x3f, sizeof d); d[1][2] = a; d[2][3] = b; for (int k = 1;k <= n; k++) for (int i = 1;i <= n; i++) for (int j = 1;j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); printf("%d\n", d[1][3]); return 0; }
啥?还是不够? 好,成全你! 来个网络流总行吧
#include<bits/stdc++.h> using namespace std; #define set(x) Set(x) #define REP(i,j,k) for (int i=(j),_end_=(k);i<=_end_;++i) #define DREP(i,j,k) for (int i=(j),_start_=(k);i>=_start_;--i) #define mp make_pair #define x first #define y second #define pb push_back template<typename T> inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0; } template<typename T> inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0; } typedef long long LL; typedef pair<int,int> node; const int dmax = 1010, oo = 0x3f3f3f3f; int n, m; int a[dmax][dmax] , ans; int d[dmax], e[dmax]; priority_queue <node> q; inline bool operator >(node a,node b){ return a.y>b.y; } bool p[dmax]; void Set(int x){ p[x] = 1; } void unset(int x){ p[x] = 0; } bool check(int x){ return x != 1 && x != n && !p[x] && e[x] > 0; } void preflow(){ e[1] = oo; d[1] = n - 1; q.push(mp(1, n - 1)); set(1); while (!q.empty()) { bool flag = 1; int k = q.top().x; q.pop(), unset(k); DREP(i, n, 1) if ((d[k] == d[i] + 1 || k == 1) && a[k][i] > 0){ flag = 0; int t = min(a[k][i], e[k]); e[k] -= t; a[k][i] -= t; e[i] += t; a[i][k] += t; if (check(i)) { q.push(mp(i, d[i])); set(i); } if (e[k] == 0) break; } if (flag) { d[k] = oo; REP(i, 1, n) if (a[k][i] > 0) chkmin(d[k], d[i] + 1); } if (check(k)) { q.push(mp(k, d[k])); set(k); } } ans = e[n]; } int main() { n = 2, m = 2; int x, y; scanf("%d%d", &x, &y); a[1][2] += x + y; preflow(); printf("%d\n", ans); return 0; }
这么说,还是没有震撼到你? 木有关系,来点别滴~ 那就来一个名字
直观清晰的istreamistream_iteratoriterator算法吧~#include <iostream> #include <cstring> #include <algorithm> #include <numeric> #include<iterator> using namespace std; int main() { istream_iterator<int> in(cin), eof; cout << accumulate(in, eof ,0) << endl; return 0; }
什么?有人要优化? 开个O3不过分吧
#pragma GCC diagnostic error "-std=c++11" #pragma GCC target("avx") #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-ftree-tail-merge") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("-fhoist-adjacent-loads") #pragma GCC optimize("-frerun-cse-after-loop") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") #include <bits/stdc++.h> using namespace std; int main() { int a, b; scanf("%d%d", &a, &b); printf("%d", a + b); return 0; }
要不要来点刺激的?来个超时吧。
不多,也就3176 ms而已#include <algorithm> //STL 通用算法 #include <bitset> //STL 位集容器 #include <cctype> //字符处理 #include <cerrno> //定义错误码 #include <cfloat> //浮点数处理 #include <ciso646> //对应各种运算符的宏 #include <climits> //定义各种数据类型最值的常量 #include <clocale> //定义本地化函数 #include <cmath> //定义数学函数 #include <complex> //复数类 #include <csignal> //信号机制支持 #include <csetjmp> //异常处理支持 #include <cstdarg> //不定参数列表支持 #include <cstddef> //常用常量 #include <cstdio> //定义输入/输出函数 #include <cstdlib> //定义杂项函数及内存分配函数 #include <cstring> //字符串处理 #include <ctime> //定义关于时间的函数 #include <cwchar> //宽字符处理及输入/输出 #include <cwctype> //宽字符分类 #include <deque> //STL 双端队列容器 #include <exception> //异常处理类 #include <fstream> //文件输入/输出 #include <functional> //STL 定义运算函数(代替运算符) #include <limits> //定义各种数据类型最值常量 #include <list> //STL 线性列表容器 #include <locale> //本地化特定信息 #include <map> //STL 映射容器 #include <memory> //STL通过分配器进行的内存分配 #include <new> //动态内存分配 #include <numeric> //STL常用的数字操作 #include <iomanip> //参数化输入/输出 #include <ios> //基本输入/输出支持 #include <iosfwd> //输入/输出系统使用的前置声明 #include <iostream> //数据流输入/输出 #include <istream> //基本输入流 #include <iterator> //STL迭代器 #include <ostream> //基本输出流 #include <queue> //STL 队列容器 #include <set> //STL 集合容器 #include <sstream> //基于字符串的流 #include <stack> //STL 堆栈容器 #include <stdexcept> //标准异常类 #include <streambuf> //底层输入/输出支持 #include <string> //字符串类 #include <typeinfo> //运行期间类型信息 #include <utility> //STL 通用模板类 #include <valarray> //对包含值的数组的操作 #include <vector> //STL 动态数组容器 //头文件拖延编译时间(虽然不能拖延运行时间,但能拖一点编译时间也很不错了hh) using namespace std; int main(){ int a; int b; //不用int a, b;,拖延运行时间 cin >> a >> b; //cin拖延运行时间 int ans = 1 * 10000 / 10 / 10 / 10 / 10 * 5 * 2 / 10 - 1; //ans表达式拖延编译和运行时间 for (int i = 1;i <= a; i++) ans += 5, ans -= 4; //拖延时间 for (int i = 1;i <= b; i++) ans += 5, ans -= 4; //拖延时间 ans = ans - ans + ans + ans - ans; //表达式拖延时间 cout << ans << endl; //cout和多输出回车拖延时间 return 0; }
再来一个
小小的模拟#include <bits/stdc++.h> using namespace std; int a, b; int main() { scanf("%d%d", &a, &b); //人眼看见数据 if (a == 0) {printf("%d", b); return 0;} //大脑瞬间“打表”被老师发现了,血量减少50…… if (b == 0) {printf("%d", a); return 0;} //大脑瞬间“打表”被老师发现了,血量减少50…… int f1 = 0, f2 = 0; //大脑申请了两个空间……(还好没炸掉) if (a < 0) f1 = 1; //大脑正在判断,请勿打扰…… if (b < 0) f2 = 1; //大脑正在判断,请勿打扰…… a = abs(a); b = abs(b); //哇!大脑使用了去掉负号的大法!!! int ans = 0; //大脑申请了一个空间 if (f1) ans -= a; //大脑正在艰难地判断着…… //大脑指挥手拿起笔在草稿纸上划来划去…… //大脑感到烦躁 //眼睛看到老师转了一下身子,立刻反馈给大脑 //大脑指挥手在计算器键盘上写下了算式…… //眼睛看到答案,反馈给大脑,大脑立刻指挥手关掉了计算器…… //眼睛看到老师转回来了 else ans += a; //大脑正在艰难地判断着…… if (f2) ans -= b;//大脑正在艰难地判断着…… //大脑指挥手拿起笔在草稿纸上划来划去…… //大脑感到烦躁 //眼睛看到老师转了一下身子,立刻反馈给大脑 //大脑指挥手在计算器键盘上写下了算式…… //眼睛看到答案,反馈给大脑,大脑立刻指挥手关掉了计算器…… //眼睛看到老师转回来了 else ans += b;//大脑正在艰难地判断着…… //眼睛观察到老师正在身后冷冷地看着…… //大脑感到紧张 //大脑让身体不由自主地颤抖起来 //大脑差点死机 //大脑复活 //立刻写下答案 printf("%d", ans); //大脑又死机了…… //耳朵听到老师在叫自己起来 //大脑指挥身体起来了 //开始下一题……(下一个数据) return 0; }
背包表示:那我走? 喂喂喂~ 先来一个多重背包
#include <bits/stdc++.h> using namespace std; int n, m; int f[20010], g[20010], q[20010]; int main() { n = m = 2; for (int i = 1; i <= n; i ++ ) { int v = 1, w, s = 1; scanf("%d", &w); memcpy(g, f, sizeof f); for (int j = 0; j < v; j ++ ) { int hh = 0, tt = -1; for (int k = j; k <= m; k += v) { if (hh <= tt && q[hh] < k - s * v) hh ++ ; //剔除超出长度元素 if (hh <= tt) f[k] = max(f[k], g[q[hh]] + (k - q[hh]) / v * w); //更新当前答案 while (hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt -- ; //维持单调性 //这里也可以这样写,更易理解 //while (hh <= tt && g[q[tt]] <= g[k] - (k - q[tt]) / v * w) tt -- ; q[++tt] = k; } } } printf("%d", f[m]); return 0; }
于是,至此,我们就完美的、
高效的解决了本题~更多资讯请关注ylkj.mysxl.cn 我的工作室网站,欢迎各位有意者发邮至ylkj_email@163.com
特此声明,以上代码有一部分来自洛谷网站与CSDN,转载请附加。
-
24
前方高能!史上最长AC代码即将来袭!
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int pow(int a,int b){//简单较慢的快速幂,但是很简单 if(b==0)//递归边界之一:a^0=1(暂且不考虑a=0) return 1; if(b==1)//递归边界之二:a^1=a return a; if(b==2)//递归边界之三:a^2=a*a return a*a; if(b%2==0)//快速幂的核心递归部分:当2|b时,a^b=(a^(b/2))^2 return pow(pow(a,b/2),2); else//递归部分:当b%2=1时,a^b=a^(b-1)*a return pow(a,b-1)*a; } struct integer { short i[126];//四位一压,所以共有126*4=504位,不会爆 bool pn;//我还考虑了符号...... integer( ){//构造函数初始化 int j; pn=false; for(j=0;j<126;j++) i[j]=0; }//这个应该都看得懂吧...... void read( ){//成员函数负责读入 int j,k,m=0,n=0; char ii[504]={0}; bool zero=true; scanf("%s",ii);//读入 if(ii[0]=='-')//正负处理 pn=true; else pn=false; for(j=503;j>-1;j--)//处理绝对值 if(ii[j]>0){//找到个位 for(k=j;k>-1;k--) if(ii[k]>='0' && ii[k]<='9') { n++; i[m]+=pow(10,n-1)*(ii[k]-'0');//这一步最难懂,慢慢琢磨,想一想怎么往高插入 if(n==4){//四位一压,别过界 n=0; m++; } } break; } for(j=0;j<126;j++) if(i[j]>0) zero=false; if(zero) pn=false; return; } void write( ){//成员函数负责输出 int j,k,a,b,c,d; bool zero=true; for(j=0;j<126;j++)//分清楚0和-0是一码事 if(i[j]>0) zero=false; if(zero) pn=false; if(pn)//负号 putchar('-'); for(j=125;j>-1;j--)//找到最高位 if(i[j]>0) { cout<<i[j];//省略前导0 for(k=j-1;k>-1;k--){//一定注意这里不能省略前导0!!!这也是为什么四位一压麻烦的原因之一 a=i[k]/1000; b=i[k]/100%10; c=i[k]/10%10; d=i[k]%10; putchar(a+'0'); putchar(b+'0'); putchar(c+'0'); putchar(d+'0'); } return; } putchar('0');//如果自始至终都是0(不进循环,即本数为0),输出0 return; } integer operator=(const int a){//把int型转化成integer自定义型 int b,j,k; bool zero=true; b=a;//注意a是const类型,不可改变! if(a<0){//对负数的处理 b=-b; pn=true; } else pn=false; for(j=0;j<126;j++){//想一想,为什么是10000不是10 i[j]=b%10000; b/=10000; if(b==0){ for(k=j+1;k<126;k++) i[k]=0; break; } } for(j=0;j<126;j++)//还是那句话,分清楚0和-0是一码事 if(i[j]>0) zero=false; if(zero) pn=false; return *this; } integer(int a){//重载运算符里最难懂的部分,注意一定要有这个(只要你有对赋值语句的重载最好都加上) *this=a; } integer operator+(const integer &a){//对加号的重载 integer b; int j; bool zero=true; if(*this==0)//0加任何数,仍得这个数 return a; else if(a==0) return *this; if(pn==a.pn){//同号两数相加,取相同的符号,并把绝对值相加 b.pn=pn; for(j=0;j<125;j++){ b.i[j]+=i[j]+a.i[j]; b.i[j+1]+=b.i[j]/10000;//还是那个问题,想想为什么是10000不是10 b.i[j]=b.i[j]%10000; } b.i[125]+=i[125]+a.i[125];//专防数组越界(即访问无效内存) b.i[j]=b.i[j]%10000; for(j=0;j<126;j++)//不多说了,你懂的 if(b.i[j]>0) zero=false; if(zero) b.pn=false; return b; } else{//异号两数相加,取绝对值较大数的符号,并把绝对值相减 int tw=0; integer x,y; x.pn=false; y.pn=false; for(j=0;j<126;j++){ x.i[j]=i[j]; y.i[j]=a.i[j]; } if(x>y){//判断哪个数的绝对值大 b.pn=pn; for(j=0;j<126;j++){ b.i[j]=i[j]-a.i[j]-tw; tw=0; if(b.i[j]<0){ b.i[j]+=10000; tw++; } } for(j=0;j<126;j++) if(b.i[j]>0) zero=false; if(zero) b.pn=false; return b; } else if(x<y){ b.pn=a.pn; for(j=0;j<126;j++){ b.i[j]=a.i[j]-i[j]-tw; tw=0; if(b.i[j]<0){ b.i[j]+=10000; tw++; } } for(j=0;j<126;j++) if(b.i[j]>0) zero=false; if(zero) b.pn=false; return b; } else{//互为相反数的两个数和为0 b=0; for(j=0;j<126;j++) if(i[j]>0) zero=false; if(zero) pn=false; return b; } } } bool operator>(const integer &a)const{//判断大小 int j; if(pn && (!a.pn))//负数小于正数 return false; else if((!pn) && a.pn)//正数大于负数 return true; else if(pn){//两个负数比较,绝对值大的反而小 for(j=125;j>-1;j--){ if(i[j]<a.i[j]) return true; else if(i[j]>a.i[j]) return false; } return false; } else{//两个正数比较,绝对值大的就大 for(j=125;j>-1;j--){ if(i[j]>a.i[j]) return true; else if(i[j]<a.i[j]) return false; } return false;//相等返回false } } bool operator<(const integer &a)const{//小于即大于的完全相反 return a>(*this); } bool operator==(const integer &a)const{//等于即既不大于又不小于 return !(*this>a || *this<a); } bool operator>=(const integer &a)const{//大于等于即不小于 return !(*this<a); } bool operator<=(const integer &a)const{//小于等于即不大于 return !(*this>a); } bool operator!=(const integer &a)const{//不等于就是不等于嘛 return !(*this==a); } }; int main( ) { integer a,b,c;//剩下的简单易懂 a.read( ); b.read( ); c=a+b; c.write( ); return 0; }
-
11
别人都太麻烦了吧,那么简单......
本题各种语言的程序范例:
C
#include <stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n", a+b); return 0; }
各种C++
#include <iostream> 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";
编写题解不易,求点赞。
-
4
题目说了a b都在1到100之间,所以:
*超长警告
a,b = tuple(map(int, input().split(" "))) if a == 1: if b == 1: print(2) if b == 2: print(3) if b == 3: print(4) if b == 4: print(5) if b == 5: print(6) if b == 6: print(7) if b == 7: print(8) if b == 8: print(9) if b == 9: print(10) if b == 10: print(11) if b == 11: print(12) if b == 12: print(13) if b == 13: print(14) if b == 14: print(15) if b == 15: print(16) if b == 16: print(17) if b == 17: print(18) if b == 18: print(19) if b == 19: print(20) if b == 20: print(21) if b == 21: print(22) if b == 22: print(23) if b == 23: print(24) if b == 24: print(25) if b == 25: print(26) if b == 26: print(27) if b == 27: print(28) if b == 28: print(29) if b == 29: print(30) if b == 30: print(31) if b == 31: print(32) if b == 32: print(33) if b == 33: print(34) if b == 34: print(35) if b == 35: print(36) if b == 36: print(37) if b == 37: print(38) if b == 38: print(39) if b == 39: print(40) if b == 40: print(41) if b == 41: print(42) if b == 42: print(43) if b == 43: print(44) if b == 44: print(45) if b == 45: print(46) if b == 46: print(47) if b == 47: print(48) if b == 48: print(49) if b == 49: print(50) if b == 50: print(51) if b == 51: print(52) if b == 52: print(53) if b == 53: print(54) if b == 54: print(55) if b == 55: print(56) if b == 56: print(57) if b == 57: print(58) if b == 58: print(59) if b == 59: print(60) if b == 60: print(61) if b == 61: print(62) if b == 62: print(63) if b == 63: print(64) if b == 64: print(65) if b == 65: print(66) if b == 66: print(67) if b == 67: print(68) if b == 68: print(69) if b == 69: print(70) if b == 70: print(71) if b == 71: print(72) if b == 72: print(73) if b == 73: print(74) if b == 74: print(75) if b == 75: print(76) if b == 76: print(77) if b == 77: print(78) if b == 78: print(79) if b == 79: print(80) if b == 80: print(81) if b == 81: print(82) if b == 82: print(83) if b == 83: print(84) if b == 84: print(85) if b == 85: print(86) if b == 86: print(87) if b == 87: print(88) if b == 88: print(89) if b == 89: print(90) if b == 90: print(91) if b == 91: print(92) if b == 92: print(93) if b == 93: print(94) if b == 94: print(95) if b == 95: print(96) if b == 96: print(97) if b == 97: print(98) if b == 98: print(99) if b == 99: print(100) if b == 100: print(101) elif a == 2: if b == 1: print(3) if b == 2: print(4) if b == 3: print(5) if b == 4: print(6) if b == 5: print(7) if b == 6: print(8) if b == 7: print(9) if b == 8: print(10) if b == 9: print(11) if b == 10: print(12) if b == 11: print(13) if b == 12: print(14) if b == 13: print(15) if b == 14: print(16) if b == 15: print(17) if b == 16: print(18) if b == 17: print(19) if b == 18: print(20) if b == 19: print(21) if b == 20: print(22) if b == 21: print(23) if b == 22: print(24) if b == 23: print(25) if b == 24: print(26) if b == 25: print(27) if b == 26: print(28) if b == 27: print(29) if b == 28: print(30) if b == 29: print(31) if b == 30: print(32) if b == 31: print(33) if b == 32: print(34) if b == 33: print(35) if b == 34: print(36) if b == 35: print(37) if b == 36: print(38) if b == 37: print(39) if b == 38: print(40) if b == 39: print(41) if b == 40: print(42) if b == 41: print(43) if b == 42: print(44) if b == 43: print(45) if b == 44: print(46) if b == 45: print(47) if b == 46: print(48) if b == 47: print(49) if b == 48: print(50) if b == 49: print(51) if b == 50: print(52) if b == 51: print(53) if b == 52: print(54) if b == 53: print(55) if b == 54: print(56) if b == 55: print(57) if b == 56: print(58) if b == 57: print(59) if b == 58: print(60) if b == 59: print(61) if b == 60: print(62) if b == 61: print(63) if b == 62: print(64) if b == 63: print(65) if b == 64: print(66) if b == 65: print(67) if b == 66: print(68) if b == 67: print(69) if b == 68: print(70) if b == 69: print(71) if b == 70: print(72) if b == 71: print(73) if b == 72: print(74) if b == 73: print(75) if b == 74: print(76) if b == 75: print(77) if b == 76: print(78) if b == 77: print(79) if b == 78: print(80) if b == 79: print(81) if b == 80: print(82) if b == 81: print(83) if b == 82: print(84) if b == 83: print(85) if b == 84: print(86) if b == 85: print(87) if b == 86: print(88) if b == 87: print(89) if b == 88: print(90) if b == 89: print(91) if b == 90: print(92) if b == 91: print(93) if b == 92: print(94) if b == 93: print(95) if b == 94: print(96) if b == 95: print(97) if b == 96: print(98) if b == 97: print(99) if b == 98: print(100) if b == 99: print(101) if b == 100: print(102) else: print(a+b) #写不动了(哭)
-
4
额,这道题非常简单,但我们可以用不同语言来解答
C++:
#include<bits/stdc++.h> using namespace std; int main(){ //定义两个变量 int a,b; cin>>a>>b; //计算它们的和,并输出 cout<<a+b; return 0; }
C语言:
#include<stdio.h> int main(){ int a,b; int c; scanf("%d%d",&a,&b); c=a+b; printf("%d",c); return 0; }
Python:
a,b=map(int,input().split()) c=a+b print(c)
Java:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1 + num2); } }
大杂烩:
#include<bits/stdc++.h> using namespace std; int main(){ int x,y; scanf("%d%d",&x,&y); int z=x+y; printf("%d",z); return 0; }
点个赞吧,都是自己写的
-
4
算法一、DFS一号
#include <bits/stdc++.h> using namespace std; int n = 2, a[5], s; int dfs(int x, int sum) { if (x > n) return sum; int i = dfs(x + 1, sum); int j = dfs(x + 1, sum + a[x]); if (i == s) return i; if (j == s) return j; return -1; } int main() { for (int i = 1;i <= n; i++) scanf("%d", &a[i]), s += a[i]; cout << dfs(1, 0) << endl; return 0; }
算法二、DFS二号
#include <bits/stdc++.h> using namespace std; int a, b; int dfs(int x) { if (x <= 5) return x; return dfs(x / 2) + dfs(x - x / 2); } int main() { scanf("%d%d", &a, &b); printf("%d\n", dfs(a) + dfs(b)); return 0; }
算法三、BFS
#include <bits/stdc++.h> using namespace std; int n = 2, a[5], s; queue<int> q; void bfs() { q.push(0); int c = 0; while (q.size()) { c++; int f = q.front(); q.pop(); if (f == s) {printf("%d\n", f); exit(0);} q.push(f + a[c]); q.push(f); } } int main() { for (int i = 1;i <= n; i++) scanf("%d", &a[i]), s += a[i]; bfs(); return 0; }
算法四、直接算
#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; int a, b; int main() { scanf("%d%d", &a, &b); int l = 0, r = 200000000; while (l < r) { int mid = l + r >> 1; if (mid == a + b) {printf("%d\n", mid); return 0;} if (mid < a + b) l = mid + 1; if (mid > a + b) r = mid - 1; } cout << l << endl; return 0; }
算法六、稍微有点暴力的枚举
#include <bits/stdc++.h> using namespace std; int a, b; int main() { scanf("%d%d", &a, &b); for (int i = 0;i <= 200000000; i++) if (a + b == i) {printf("%d\n", i); break;} return 0; }
算法七、最短路
思路:定义节点1到节点2路径长度为a,节点2到节点3路径长度为b 则答案为节点1到节点3的最短路(也就是a+ba+b)
#include <bits/stdc++.h> using namespace std; int w[5][5], d[5], v[5]; int n = 3; void dijkstra() { memset(d, 0x3f, sizeof d); memset(v, 0, sizeof v); d[1] = 0; for (int i = 1;i < n; i++) { int x = 0; for (int j = 1;j <= n; j++) if (!v[j] && (x == 0 || d[j] < d[x])) x = j; v[x] = 1; for (int y = 1;y <= n; y++) d[y] = min(d[y], d[x] + w[x][y]); } } int main() { int a, b; scanf("%d%d", &a, &b); memset(w, 0x3f, sizeof w); w[1][2] = a; w[2][3] = b; dijkstra(); printf("%d\n", d[3]); return 0; }
算法八、最短路之SPFA
思路同上
#include <bits/stdc++.h> using namespace std; int a, b, n = 3; int w[5][5], d[5], v[5]; queue<int> q; void spfa() { memset(d, 0x3f, sizeof d); memset(v, 0, sizeof v); d[1] = 0, v[1] = 1; q.push(1); while (q.size()) { int x = q.front(); q.pop(); v[x] = 0; for (int i = 1;i <= n; i++) { // if (w[x][i] == 0x3f) continue; if (d[i] > d[x] + w[x][i]) { d[i] = d[x] + w[x][i]; if (!v[i]) q.push(i), v[i] = 1; } } } } int main() { scanf("%d%d", &a, &b); memset(w, 0x3f, sizeof w); w[1][2] = a; w[2][3] = b; spfa(); printf("%d\n", d[3]); return 0; }
算法九、最短路之Floyd
思路同上
#include <bits/stdc++.h> using namespace std; int d[5][5], n = 3; int main() { int a, b; scanf("%d%d", &a, &b); memset(d, 0x3f, sizeof d); d[1][2] = a; d[2][3] = b; for (int k = 1;k <= n; k++) for (int i = 1;i <= n; i++) for (int j = 1;j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); printf("%d\n", d[1][3]); return 0; }
算法十、高精
#include<bits/stdc++.h> using namespace std; string a0, b0; int a[1005], b[1005]; int main(){ cin >> a0 >> b0; int l1 = a0.size(), l2 = b0.size(); for (int i = 0;i < l1; i++) a[l1 - i] = a0[i] - 48; for (int i = 0;i < l2; i++) b[l2 - i] = b0[i] - 48; l1 = max(l1, l2); for (int i = 1;i <= l1; i++) { a[i] += b[i]; if (a[i] > 9) a[i + 1] += 1, a[i] %= 10; } if (a[max(l1, l2) + 1] > 0) l1++; for (int i = l1;i >= 1; i--) printf("%d", a[i]); return 0; }
算法十一、最小生成树之kruskal
思路其实和最短路的一样,只是改成用最小生成树的方法求罢了
#include <bits/stdc++.h> using namespace std; struct rec { int x, y, z; } edge[5]; int fa[5], m = 2, ans = 0; int get(int x) { if (x == fa[x]) return x; return fa[x] = get(fa[x]); } int cmp(rec a, rec b) { return a.z < b.z; } int main() { int a, b; scanf("%d%d", &a, &b); edge[1] = (rec){1, 2, a}; edge[2] = (rec){2, 3, b}; for (int i = 1;i <= m + 1; i++) fa[i] = i; sort(edge + 1, edge + 1 + m, cmp); for (int i = 1;i <= m; i++) { int x = get(edge[i].x); int y = get(edge[i].y); if (x == y) continue; fa[x] = y; ans += edge[i].z; } printf("%d\n", ans); return 0; }
算法十二、最小生成树之prim
思路同上
#include <bits/stdc++.h> using namespace std; int w[5][5], d[5], n = 3, ans, v[5]; void prim() { memset(d, 0x3f, sizeof d); memset(v, 0, sizeof v); d[1] = 0; for (int i = 1;i < n; i++) { int x = 0; for (int j = 1;j <= n; j++) if (!v[j] && (x == 0 || d[j] < d[x])) x = j; v[x] = 1; for (int y = 1;y <= n; y++) if (!v[y]) d[y] = min(d[y], w[x][y]); } } int main() { int a, b; scanf("%d%d", &a, &b); memset(w, 0x3f, sizeof w); w[1][2] = a; w[2][3] = b; prim(); int ans = 0; for (int i = 2;i <= n; i++) ans += d[i]; printf("%d\n", ans); return 0; }
算法十三、前缀和
#include <bits/stdc++.h> using namespace std; int a[5], s[5]; int main() { for (int i = 1;i <= 2; i++) scanf("%d", &a[i]), s[i] += a[i] + s[i - 1]; printf("%d\n", s[2]); return 0; }
算法十四、后缀和
#include <bits/stdc++.h> using namespace std; int a[5], s[5]; int main() { for (int i = 2;i >= 1; i--) scanf("%d", &a[i]), s[i] += a[i] + s[i + 1]; printf("%d\n", s[1]); return 0; }
算法十五、位运算
#include <bits/stdc++.h> using namespace std; int add(int a, int b) { if (b == 0) return a; return add(a ^ b, (a & b) << 1); } int main() { int a, b; scanf("%d%d", &a, &b); printf("%d\n", add(a, b)); return 0; }
网络流(不要问我为啥不标号了,太麻烦了)
#include<bits/stdc++.h> using namespace std; #define set(x) Set(x) #define REP(i,j,k) for (int i=(j),_end_=(k);i<=_end_;++i) #define DREP(i,j,k) for (int i=(j),_start_=(k);i>=_start_;--i) #define mp make_pair #define x first #define y second #define pb push_back template<typename T> inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0; } template<typename T> inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0; } typedef long long LL; typedef pair<int,int> node; const int dmax = 1010, oo = 0x3f3f3f3f; int n, m; int a[dmax][dmax] , ans; int d[dmax], e[dmax]; priority_queue <node> q; inline bool operator >(node a,node b){ return a.y>b.y; } bool p[dmax]; void Set(int x){ p[x] = 1; } void unset(int x){ p[x] = 0; } bool check(int x){ return x != 1 && x != n && !p[x] && e[x] > 0; } void preflow(){ e[1] = oo; d[1] = n - 1; q.push(mp(1, n - 1)); set(1); while (!q.empty()) { bool flag = 1; int k = q.top().x; q.pop(), unset(k); DREP(i, n, 1) if ((d[k] == d[i] + 1 || k == 1) && a[k][i] > 0){ flag = 0; int t = min(a[k][i], e[k]); e[k] -= t; a[k][i] -= t; e[i] += t; a[i][k] += t; if (check(i)) { q.push(mp(i, d[i])); set(i); } if (e[k] == 0) break; } if (flag) { d[k] = oo; REP(i, 1, n) if (a[k][i] > 0) chkmin(d[k], d[i] + 1); } if (check(k)) { q.push(mp(k, d[k])); set(k); } } ans = e[n]; } int main() { n = 2, m = 2; int x, y; scanf("%d%d", &x, &y); a[1][2] += x + y; preflow(); printf("%d\n", ans); return 0; }
线段树
转化为区间求和问题
#include <bits/stdc++.h> #define l(x) tree[x].l #define r(x) tree[x].r #define sum(x) tree[x].sum #define add(x) tree[x].add using namespace std; struct SegmentTree { int l, r; //区间左右端点 long long sum, add; //sum 区间和 add 延迟标记 } tree[400010]; int a[100010], n = 1, m = 2; void build (int p, int l, int r) { l(p) = l, r(p) = r; if(l == r) {sum(p) = a[l]; return;} int mid = l + r >> 1; build(p * 2, l, mid); build(p * 2 + 1, mid + 1, r); sum(p) = sum(p * 2) + sum(p * 2 + 1); } void spread(int p) { if(add(p)) { //节点p有标记 sum(p * 2) += add(p) * (r(p * 2) - l(p * 2) + 1); //更新左子节点信息 sum(p * 2 + 1) += add(p) * (r(p * 2 + 1) - l(p * 2 + 1) + 1); //更新右子节点 add(p * 2) += add(p); //给左子节点打延迟标记 add(p * 2 + 1) += add(p); //给右子节点打延迟标记 add(p) = 0; //清除p的标记 } } void change(int p, int l, int r, int d) { if(l <= l(p) && r >= r(p)) { //完全覆盖 sum(p) += (long long) d * (r(p) - l(p) + 1); //更新节点信息 add(p) += d; //给节点打延迟标记 return; } spread(p); //下传延迟标记 int mid = l(p) + r(p) >> 1; if(l <= mid) change(p * 2, l, r, d); if(r > mid) change(p * 2 + 1, l, r, d); sum(p) = sum(p * 2) + sum(p * 2 + 1); } long long ask(int p, int l, int r) { if(l <= l(p) && r >= r(p)) return sum(p); spread(p); int mid = l(p) + r(p) >> 1; long long val = 0; if(l <= mid) val += ask(p * 2, l, r); if(r > mid) val += ask(p * 2 + 1, l, r); return val; } int main() { a[1] = 0; build(1, 1, n); while(m--) { int d = 0; scanf("%d", &d); change(1, 1, 1, d); } printf("%lld\n", ask(1, 1, 1)); return 0; }
树状数组
思路一样,区间求和
#include<bits/stdc++.h> using namespace std; const int SIZE = 100010; int a[SIZE], n = 1, m = 2; long long c[2][SIZE], sum[SIZE]; long long ask(int k, int x) { long long ans = 0; for(; x ; x -= x & -x) ans += c[k][x]; return ans; } void add(int k,int x,int y) { for(; x <= n; x += x & -x) c[k][x] += y; } int main() { a[1] = 0; while(m--) { int d = 0; scanf("%d", &d); add(0, 1, d); add(0, 2, -d); add(1, 1, d); add(1, 2, -2 * d); } long long ans = sum[1] + 2 * ask(0, 1) - ask(1, 1); ans -= sum[0] + 1 * ask(0, 0) - ask(1, 0); printf("%lld\n", ans); return 0; }
分块
思路一样,区间求和
#include<bits/stdc++.h> using namespace std; long long a[50000010], sum[50000010], add[50000010]; int L[50000010], R[50000010]; int pos[50000010]; int n = 1, m = 2, t; void change(int l, int r, long long d) { int p = pos[l], q = pos[r]; if (p == q) { for (int i = l; i <= r; i++) a[i] += d; sum[p] += d*(r - l + 1); } else { for (int i = p + 1; i <= q - 1; i++) add[i] += d; for (int i = l; i <= R[p]; i++) a[i] += d; sum[p] += d*(R[p] - l + 1); for (int i = L[q]; i <= r; i++) a[i] += d; sum[q] += d*(r - L[q] + 1); } } long long ask(int l, int r) { int p = pos[l], q = pos[r]; long long ans = 0; if (p == q) { for (int i = l; i <= r; i++) ans += a[i]; ans += add[p] * (r - l + 1); } else { for (int i = p + 1; i <= q - 1; i++) ans += sum[i] + add[i] * (R[i] - L[i] + 1); for (int i = l; i <= R[p]; i++) ans += a[i]; ans += add[p] * (R[p] - l + 1); for (int i = L[q]; i <= r; i++) ans += a[i]; ans += add[q] * (r - L[q] + 1); } return ans; } int main() { a[1] = 0; t = sqrt(n*1.0); for (int i = 1; i <= t; i++) { L[i] = (i - 1)*sqrt(n*1.0) + 1; R[i] = i*sqrt(n*1.0); } if (R[t] < n) t++, L[t] = R[t - 1] + 1, R[t] = n; for (int i = 1; i <= t; i++) for (int j = L[i]; j <= R[i]; j++) { pos[j] = i; sum[i] += a[j]; } while (m--) { int d; scanf("%d", &d); change(1, 1, d); } printf("%lld\n", ask(1, 1)); }
LCT(洛谷)
#include<bits/stdc++.h> 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",query(A,B)); return 0; }
LCA(洛谷)
#include<cstdio> //头文件 #define NI 2 //从来不喜欢算log所以一般用常数 不知道算不算坏习惯 因为3个节点 所以log3(当然以2为底)上取整得2 struct edge { int to,next,data; //分别表示边的终点,下一条边的编号和边的权值 }e[30]; //邻接表,点少边少开30是为了浪啊 int v[10],d[10],lca[10][NI+1],f[10][NI+1],tot=0; //数组开到10依然为了浪 //数组还解释嘛,v表示第一条边在邻接表中的编号,d是深度,lca[x][i]表示x向上跳2^i的节点,f[x][i]表示x向上跳2^i的距离和 void build(int x,int y,int z) //建边 { e[++tot].to=y; e[tot].data=z; e[tot].next=v[x]; v[x]=tot; e[++tot].to=x; e[tot].data=z; e[tot].next=v[y]; v[y]=tot; } void dfs(int x) //递归建树 { for(int i=1;i<=NI;i++) //懒,所以常数懒得优化 f[x][i]=f[x][i-1]+f[lca[x][i-1]][i-1], lca[x][i]=lca[lca[x][i-1]][i-1]; //建树的同时进行预处理 for(int i=v[x];i;i=e[i].next) //遍历每个连接的点 { int y=e[i].to; if(lca[x][0]==y) continue; lca[y][0]=x; //小技巧:lca[x][0]即为x的父亲~~(向上跳2^0=1不就是父节点嘛) f[y][0]=e[i].data; d[y]=d[x]+1; dfs(y); //再以这个节点为根建子树【这里真的用得到嘛??】 } } int ask(int x,int y) //询问,也是关键 { if(d[x]<d[y]) {int t=x;x=y;y=t;} //把x搞成深的点 int k=d[x]-d[y],ans=0; for(int i=0;i<=NI;i++) if(k&(1<<i)) //若能跳就把x跳一跳 ans+=f[x][i], //更新信息 x=lca[x][i]; for(int i=NI;i>=0;i--) //不知道能不能正着循环,好像倒着优,反正记得倒着就好了 if(lca[x][i]!=lca[y][i]) //如果x跳2^i和y跳2^j没跳到一起就让他们跳 ans+=f[x][i]+f[y][i], x=lca[x][i],y=lca[y][i]; return ans+f[x][0]+f[y][0]; //跳到LCA上去(每步跳的时候都要更新信息,而且要在跳之前更新信息哦~) } int main() { int a,b; scanf("%d%d",&a,&b); build(1,2,a); build(1,3,b); //分别建1 2、1 3之间的边 dfs(1); //以1为根建树 printf("%d",ask(2,3)); //求解2 3到它们的LCA的距离和并输出 }
Bellman-Ford
#include <bits/stdc++.h> using namespace std; int dis[50], u[50], v[50], w[50], n, m; void bellman(int start) { for (int i = 1;i <= n; i++) dis[i] = 0x3f3f3f3f; dis[start] = 0; for (int i = 1;i < n; i++) for (int j = 1;j <= m; j++) if (dis[v[j]] > dis[u[j]] + w[j]) dis[v[j]] = dis[u[j]] + w[j]; } int main() { n = 3; m = 2; for (int i = 1;i <= m; i++) cin >> w[i], u[i] = i, v[i] = i + 1; bellman(1); printf("%d\n", dis[3]); return 0; }
超时
#include <algorithm> //STL 通用算法 #include <bitset> //STL 位集容器 #include <cctype> //字符处理 #include <cerrno> //定义错误码 #include <cfloat> //浮点数处理 #include <ciso646> //对应各种运算符的宏 #include <climits> //定义各种数据类型最值的常量 #include <clocale> //定义本地化函数 #include <cmath> //定义数学函数 #include <complex> //复数类 #include <csignal> //信号机制支持 #include <csetjmp> //异常处理支持 #include <cstdarg> //不定参数列表支持 #include <cstddef> //常用常量 #include <cstdio> //定义输入/输出函数 #include <cstdlib> //定义杂项函数及内存分配函数 #include <cstring> //字符串处理 #include <ctime> //定义关于时间的函数 #include <cwchar> //宽字符处理及输入/输出 #include <cwctype> //宽字符分类 #include <deque> //STL 双端队列容器 #include <exception> //异常处理类 #include <fstream> //文件输入/输出 #include <functional> //STL 定义运算函数(代替运算符) #include <limits> //定义各种数据类型最值常量 #include <list> //STL 线性列表容器 #include <locale> //本地化特定信息 #include <map> //STL 映射容器 #include <memory> //STL通过分配器进行的内存分配 #include <new> //动态内存分配 #include <numeric> //STL常用的数字操作 #include <iomanip> //参数化输入/输出 #include <ios> //基本输入/输出支持 #include <iosfwd> //输入/输出系统使用的前置声明 #include <iostream> //数据流输入/输出 #include <istream> //基本输入流 #include <iterator> //STL迭代器 #include <ostream> //基本输出流 #include <queue> //STL 队列容器 #include <set> //STL 集合容器 #include <sstream> //基于字符串的流 #include <stack> //STL 堆栈容器 #include <stdexcept> //标准异常类 #include <streambuf> //底层输入/输出支持 #include <string> //字符串类 #include <typeinfo> //运行期间类型信息 #include <utility> //STL 通用模板类 #include <valarray> //对包含值的数组的操作 #include <vector> //STL 动态数组容器 //头文件拖延编译时间(虽然不能拖延运行时间,但能拖一点编译时间也很不错了hh) using namespace std; int main(){ int a; int b; //不用int a, b;,拖延运行时间 cin >> a >> b; //cin拖延运行时间 int ans = 1 * 10000 / 10 / 10 / 10 / 10 * 5 * 2 / 10 - 1; //ans表达式拖延编译和运行时间 for (int i = 1;i <= a; i++) ans += 5, ans -= 4; //拖延时间 for (int i = 1;i <= b; i++) ans += 5, ans -= 4; //拖延时间 ans = ans - ans + ans + ans - ans; //表达式拖延时间 cout << ans << endl; //cout和多输出回车拖延时间 return 0; }
可耻的打表
#include <bits/stdc++.h> using namespace std; int a, b; int main() { scanf("%d%d", &a, &b); if (a == 3 && b == 4) printf("7"); if (a == 45 && b == 55) printf("100"); if (a == 123 && b == 321) printf("444"); if (a == 91086199 && b == 18700332) printf("109786531"); if (a == 42267194 && b == 60645282) printf("102912476"); if (a == 69274392 && b == 10635835) printf("79910227"); if (a == 5710219 && b == 85140568) printf("90850787"); if (a == 75601477 && b == 24005804) printf("99607281"); if (a == 70597795 && b == 90383234) printf("160981029"); if (a == 82574652 && b == 22252146) printf("104826798"); return 0; }
只用一个变量跑A+B把一个long long拆成两个int指针啊!
#include<iostream> using namespace std; long long a; int main() { scanf("%d%d", (int*)(&a), (int*)(&a+1)); printf("%d\n", *((int*)&a) + *((int*)(&a+1))); return 0; }
矩阵乘法
#include<bits/stdc++.h> using namespace std; int a, b; int x[2][2] = { {0, 1}, {1, 1} }; void mo(int f[]) { int ans[2] = {0}; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) ans[i] += f[j] * x[i][j]; for(int i = 0; i < 2; i++) f[i] = ans[i]; } int main() { cin >> a >> b; int f[3] = {a, b}; mo(f); cout << f[1]; return 0; }
数学表达式
#include <bits/stdc++.h> using namespace std; long long a, b; int main() { cin >> a >> b; cout << a - b + (a * 2) - (a - b) - a + (a + (b - a)) << endl; return 0; }
define大法
#include <bits/stdc++.h> #define ___ int #define $$$ main #define _$_$_ return #define _ cin #define $ cout #define __ using #define $$ namespace #define o_o std __ $$ o_o; ___ $$$(){ ___ _$o$_,$o_o$; _ >> _$o$_ >> $o_o$; $ << _$o$_ + $o_o$; _$_$_ 0; }
压位高精度加法--奇怪的知识又增加了!
#include <bits/stdc++.h> using namespace std; const int mod = 100000000; vector<int> add(vector<int> &A, vector<int> &B) { vector<int> C; int t = 0; for (int i = 0; i < A.size() || i < B.size(); i++) { if (i < A.size()) t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % mod); t /= mod; } if (t) C.push_back(t); return C; } int main() { string a, b; cin >> a >> b; vector<int> A, B, C; for (int i = a.size() - 1, s = 0, j = 0, t = 1; i >= 0; i--) { s += (a[i] - '0') * t; j++; t *= 10; if (j == 8 || i == 0) A.push_back(s), s = 0, j = 0, t = 1; } for (int i = b.size() - 1, s = 0, j = 0, t = 1; i >= 0; i--) { s += (b[i] - '0') * t; j++; t *= 10; if (j == 8 || i == 0) B.push_back(s), s = 0, j = 0, t = 1; } C = add(A, B); cout << C.back(); for (int i = C.size() - 2; i >= 0; i--) printf("%08d", C[i]); return 0; }
暴力枚举优化版
和算法六区别“不大”但是时间优化了300ms……
#include <bits/stdc++.h> using namespace std; int main() { int a, b; scanf("%d%d", &a, &b); for (int i = min(2 * a, 2 * b);i <= max(2 * a, 2 * b); i++) if (a + b == i) { printf("%d", i); //注意要输出i,不然如果输出a+b循环就没意义了…… return 0; } }
快读快写
#include<bits/stdc++.h> using namespace std; int read() { int s = 0, f = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } void write(int x) { if(x < 0) { putchar('-'); x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); return; } int main() { int a, b; a = read(); b = read(); write(a + b); return 0; }
麻烦点个赞!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
3
基础
这题就是最基础的语句运用。
cout<<
- 作用,输出,后加单引号即为字符,双引号里面加字符串,直接不加就是数字。
- 输出的数字可以进行基本运算,+、-、乘(*)、除(/),在c++中,/会自动向下取整。程序会输出运算后的结果。
cin>>
输入,输入一个数(字符串),直到空格(换行符)。
头文件、程序开始与结束
- 头文件:
#include <bits/stdc++.h>
using namespace std;
在头文件之后,是必备工具。- 万能头文件,常用的都有。
- 程序开始:
int main() {
主函数,程序会优先运行它里面的语句。
- 结束:
return 0;}
结束主函数,之后我们会深入学习。
当我们明白了,以上后,就可以解这题了。
AC代码(求赞):
#include <bits/stdc++.h>//万能头文件。 using namespace std;//程序必备工具 int main()//程序开始 { int a,b; //定义两个整型变量,可储存的范围是-2^31-1到2^31-1。 cin>>a>>b;//输入 cout<<a+b;//会优先计算再输出 return 0;//程序的结束 }
-
3
A + B?我们可以想到用二分做,答案范围就是从 ,因为怕错,所以就扩大了点。
AC code
#include <cstdio> int a , b; int main(void){ scanf("%d%d" , &a , &b); int l = 0 , r = 300; while(r - l > 1){ int mid = (l + r) >> 1; if(mid - b < a) l = mid; else r = mid; } printf("%d\n" , r); }
那要怎么优化呢,众所周知,树状数组很快,所以我们可以用树状数组来优化。
code
#include <cstdio> using namespace std; long long a , b; long long qwq[10] , ans; int N; long long lowbit(int x){ return x & (-x); } void add(int i , long long k){ //第 i 位置上加上 k while(i <= N){ qwq[i] += k; i += lowbit(i); } } long long getsum(int i){ //求 qwq[1 ~ i] 的和 long long rrr = 0; while(i){ rrr += qwq[i]; i -= lowbit(i); } return rrr; } int main(void){ N = 3; add(1 , a); add(2 , b); ans = getsum(2); printf("%lld\n" , ans); add(1 , -a); add(2 , -b); //删去 }
成功AC了这道《NOI-》的超级难题
-
3
这题就是很简单的倆数相加哦~不过要给大家说一个新的知识点,C++语言的万能头文件
#include<bits/stdc++.h>
万能头文件里面包括了我们常用的一些库 比如说:#include<iostream>
#include<cstring>
#include<cmath>
这样我们就不用记头文件怎么拼写,好好学习算法了。
上AC代码#include<bits/stdc++.h> using namespace std; int main() { int a,b; cin >> a >> b; cout << a+b; return 0; }
-
1
你们咋都写这么复杂啊😕
我来个简简单单滴代码来净化一下:#include <iostream> #include <string> #include <algorithm> using namespace std; string addStrings(const string& num1, const string& num2) { int carry = 0; string sum; size_t len1 = num1.length(); size_t len2 = num2.length(); string a = len1 >= len2 ? num1 : num2; string b = len1 >= len2 ? num2 : num1; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); for (size_t i = 0; i < a.length(); i++) { int digitSum = (a[i] - '0') + carry; if (i < b.length()) { digitSum += (b[i] - '0'); } sum.push_back((digitSum % 10) + '0'); carry = digitSum / 10; } if (carry > 0) { sum.push_back(carry + '0'); } reverse(sum.begin(), sum.end()); return sum; } int main() { string num1, num2; cin >> num1 >> num2; string sum = addStrings(num1, num2); cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << endl; return 0; }
简单明了真好看
全是bug
信息
- ID
- 1
- 时间
- 1000ms
- 内存
- 16MiB
- 难度
- 6
- 标签
- 递交数
- 26157
- 已通过
- 8140
- 上传者