sum

LeetCode 113. Path Sum II

元气小坏坏 提交于 2020-01-24 01:45:41
题目链接: 点击这里 思路:二叉树的先序遍历 从根节点深度遍历二叉树,先序遍历时,将该节点值存储至 p a t h path p a t h 栈 中 (vector实现),使用 p a t h _ v a l u e path\_value p a t h _ v a l u e 累加节点值。 当遍历至叶结点时,检查 p a t h _ v a l u e path\_value p a t h _ v a l u e 值是否为 s u m sum s u m ,若为 s u m sum s u m ,则将 p a t h path p a t h p u s h push p u s h 进入 r e s u l t result r e s u l t 结果中。 在回溯时,将该节点值从 p a t h path p a t h 栈 中弹出, p a t h _ v a l u e path\_value p a t h _ v a l u e 减去节点值。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}

小学生的愤怒(1)

六月ゝ 毕业季﹏ 提交于 2020-01-23 21:42:06
最近,@十一维的智子发送了一篇关于NOIP(没错,这货起死回生了)的文章。 相信小学生党们已经怒了… 没关系!今天我们就用一道CTSC的原题反击这个博主。 开始战斗! [CTSC2017]网络 题目描述 一个一般的网络系统可以被描述成一张无向连通图。图上的每个节点为一个服务器,连接服务器与服务器的数据线则看作图上的一条边,边权为该数据线的长度。两个服务器之间的通讯距离定义为其对应节点之间最短路的长度。 现在,考虑一个当前图结构为树的网络系统。你作为该网络系统的管理员,被要求在这个系统中新加入一条给定长度的数据线。数据线可以连在任意两个服务器上。你的任务是,求出在所有合法的方案中,通讯距离最远的两个服务器之间的最小距离。 输入格式 输入包含多组数据。对于每组数据,输入的第一行包含二个正整数 N, L, 其中 N 表示服务器个数,L 为新加入数据线的长度。 接下来 n − 1 行,第 i 行有三个正整数 ai, bi, li,表示有一条长度为 li 的数据线连接服务器 ai, bi。服务器的编号为 1 ∼ N。 输入的末尾以两个 0 作为结束。 输出格式 对于每组数据,输出一行一个整数,描述在所有合法的方案中,通讯距离最远的两个服务器之间的最小距离。 输入输出样例 输入 #1复制 7 1 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 6 7 1 0 0 输出 #1 3

Java: How to sum the elements of two arrays with different lengths

房东的猫 提交于 2020-01-23 17:33:10
问题 I am trying to add the elements of two arrays with different lengths together. The code below is only for the same length and here is all I have so far. //for the same lengths int[]num1 = {1,9,9,9}; int[]num2 = {7,9,9,9};// {9,9,9} int total = 0, carry = 1; int capacity = Math.max(num1.length,num2.length); int []arraySum = new int [capacity]; for (int i = capacity - 1 ; i >= 0; i--) { arraySum[i] = num1[i]+ num2[i]; if (arraySum[i] > 9) { arraySum[i] = arraySum[i] % 10; num2[i-1] = num2[i-1]

字典{}

帅比萌擦擦* 提交于 2020-01-23 07:43:46
1. 字典 dict 用{}来表示 键值对数据 {key:value} 唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 2. 增删改查 字典的增加: dic[key] = value       #强制增加 dic = {'num': '1', 'sum': '2'} dic['key_1'] = 'value_1' print(dic)输出:{'num': '1', 'sum': '2', 'key_1': 'value_1'} dic.setdefault(key,value) #如果键在字典中存在不进行任何操作,否则就添加 dic = {'num': '1', 'sum': '2'} dic.setdefault('key_2', 'value_2') print(dic)输出:{'num': '1', 'sum': '2', 'key_2': 'value_2'} 字典的删除: dic.pop(key)          # 有返回值 返回的是被删除value dic = {'num': '1', 'sum': '2', 'key_2': 'value_2'} i = dic.pop('sum') print(dic) print(i)输出:{'num': '1', 'key_2': 'value_2'}   2 del dic[key

输入名字,增加名字,随机点名

邮差的信 提交于 2020-01-23 03:35:55
import java.util.Scanner; import java.util.Random; public class Demo{ public static void main(String[] args) { Scanner sc = new Scanner(System.in);//创建用户输入的实例 Random r = new Random(); //创建产生随机数的实例 String[] stuArr = new String[100]; //创建数组 int index = 0; //数组下标 用来保存随机数 int num = 0; //用来接收选择数的 int sum = 0; //用来存同学名称总数 String name = ""; //随机数方法二的时候用的 while(true){ //接收用户的输入用来确定数组的长度 System.out.print("请输入同学的个数:"); num = sc.nextInt(); //循环往数组中添加姓名 for(int i = sum; i<sum+num;i++){ System.out.print("请输入第" + (i+1) + "同学的名字:"); stuArr[i] = sc.next(); } //用来存同学名称总数 sum = sum + num; System.out.println(

How to sum the numbers(Int16) of stored core data - Swift 3

戏子无情 提交于 2020-01-23 03:32:44
问题 I have stored the letters(String), numbers(Int16) and Date(Date) in the core data. And the filter(NSPredicate) succeeded in organizing only the necessary data. I want to get the total sum of the numbers in this core data. Entity Name : Entity Attribute Name : Letter(String), Value(Int16), Date(Date) let context = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext let entityDesc: NSEntityDescription = NSEntityDescription.entity(forEntityName: "Entity", in: context)! let

HDU - 4419 Colourful Rectangle【扫描线.笔记】

↘锁芯ラ 提交于 2020-01-23 02:36:09
题目链接: http://acm.hdu.edu.cn/viewcode.php?rid=32159923 题目大意:有n个矩形块,这些矩形块有R, G, B三种不同的颜色,最终区域的颜色由该区域上颜色的种类决定,要求输出 R, G, B, RG, RB, GB, RGB七种颜色的面积是多少。 思路:在前面区域覆盖面积的例题里,我们用一维数组维护被覆盖的区间长度,因为这里有颜色之分,所以可以再开一维表示颜色。此外,区域的颜色是由不同颜色的种类决定的,所以颜色的判断就可以很好地利用到或运算。 (扫描线中为什么不用push_down)扫描线的例题中利用了线段树这种数据结构,但是我们每次查询的都是整体,没有去询问某个子区间,所以不用push_down。 AC1: //cover[root][i]表示区间被第i种颜色覆盖的次数 # include <iostream> # include <cstdio> # include <cmath> # include <string> # include <cstring> # include <algorithm> # include <limits> # include <vector> # include <stack> # include <queue> # include <set> # include <map> # define

LOJ泛做

萝らか妹 提交于 2020-01-22 23:45:49
SDOI2019 快速查询 考虑维护双标记,题目的难点在于如何维护单点赋值操作。 推式子发现,直接修改原本的值变为$\frac{x-add}{mul}$,用hash维护下标即可。 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define MOD 10000019 4 namespace hash{ 5 #define sz 299999 6 int h[300010], num[300010], clo[300010]; 7 int All, CLO; 8 inline int get_Num(int x) { 9 int wt = x % sz; 10 while(num[wt] != x && num[wt]) { 11 ++ wt; 12 if(wt == sz) wt = 0; 13 } 14 num[wt] = x; 15 return wt; 16 } 17 inline int upd(int x, int y, int CL) { 18 int wt = get_Num(x); 19 if(clo[wt] < CLO) h[wt] = All; 20 int ret = y - h[wt]; 21 h[wt] = y; clo[wt] = CL; 22 return ret; 23 } 24 inline

【HAOI2015】树上操作

随声附和 提交于 2020-01-22 22:02:02
(题面来自洛谷) 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a 。 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。 操作 3 :询问某个节点 x 到根的路径中所有点的点权和。 数据范围:N <= 1e5 分析:简化版的ETT。建立括号序列,操作1、3都很容易解决。为了实现操作2,给序列上的点打标记,表示这里存储的是原节点权值的正/负值。用线段树维护区间和,上推时分别合并正负节点数,区间修改时每个线段树节点值 \(sum+=val*(pos-neg)\) ,这样就实现了正负节点的区分操作。同时要求移植子树就变成了ETT的模板题,用Splay/FHQ维护即可。 代码: #include <iostream> #include <cstdio> #include <cstring> const int maxn(100010); typedef long long LL; using namespace std; int n, m; LL wt[maxn]; int head[maxn], etop; struct E { int to, nxt; } edge[maxn<<1]; inline void insert(int u, int v) { edge[++etop]

dijkstra扩展应用

喜夏-厌秋 提交于 2020-01-22 18:58:45
1.图中点与路均有权值,且存在多条最短路,要求输出最短路条数、点权和最小的最短路路径及其点权和。 1)如何求最短路数量? 数据结构:开一个count[]数组,记录到某点的最短路条数。 代码:在dij算法中的“更新各点最短路d[i]”的部分,除了判断体①d[u]>d[v]+cost[v][u],还要新增判断体②d[u]==d[v]+cost[v][u];在前者,更新count[u]=count[v],在后者更新count[u]+=count[v]. 初始化:count[s]=0,其余为0 2)如何找到最短路中的最大点权和? 数据结构:开sum[]数组记录到点i的最短路径中点权和最大的路径的点权和(好绕= =)。开val[]存每个点的和,这个是固定的。 代码:在判断体①中,写sum[u]=sum[v]+val[u];在②中,if(sum[u]<sum[v]+val[u]),更新sum[u]=sum[v]+val[u]; 初始化:sum[s]=val[s];其余为0 3)如何将2)中的那条路记录下来? path[]数组反向记录即可.即path[i]=j表示点i的最短路是由j点转来的.如果正向,由于一个点可能通向多个点,且均为到这些点的最短路,那么会产生覆盖. 天梯赛 L2-001 紧急救援 ac代码 public class Main { static int INF =