Exemple #1
0
int cal_sum(TreeNode* root, int &maxSum)
{
	if (root == nullptr)
		return 0;
	int left = cal_sum(root->left, maxSum);
	int right = cal_sum(root->right, maxSum);
	int sum = left + right + root->val;
	if (sum > maxSum)
		maxSum = sum;
	int thismax = std::max(left,right);
	return std::max(thismax + root->val, root->val);
}
Exemple #2
0
int icmp_pack(char type,int seq,int len,short id)
{
	icmp_t *icmp = (void *)(send_buf + sizeof(eth_t) + sizeof(ip_t));
	
	icmp->type = type;
	icmp->code = 0;
	icmp->checksum = 0;
	icmp->id = htons(id);
	icmp->sequence = htons(seq);
	icmp->checksum = cal_sum((void *)icmp,len + sizeof(*icmp));

	return ip_pack(len + sizeof(*icmp), 1);
}
Exemple #3
0
// lucky number defines as the sum of each digit square is 1. 
// for example
//    100 = 1^2 + 0^2 + 0^2 = 1
//
// if a number encounts a self-loop, then it is not lucky number.
//
int lucky_number(int n)
{
    // max possible value is: 9 * (1^2 + 2^2 + ... + 9^9), and must < 81 * 81
    char trace[81 * 81] = {0};

    do {
        int a[10] = {0};
        fill_pattern(n, a);

        int new_value = cal_sum(a);

        if (new_value == 1) return 1;

        if (trace[new_value])   return 0;

        trace[new_value] = 1;
        n = new_value;
    } while (1);
}
Exemple #4
0
int ip_pack(u32 length, u8 type)
{
	ip_t *ip = (void *)(send_buf + sizeof(eth_t));

	ip->ihl = sizeof(ip_t) >> 2;
	ip->version = 4;
	ip->tos = 0;
	ip->tot_len = htons(sizeof(ip_t) + length);
	ip->id = 0;
	ip->frag_off = 0;
	ip->ttl = 255;
	ip->protocol = type;
	ip->check = 0;
	ip->saddr = eip;
	ip->daddr = hip;
	
	ip->check = cal_sum((void *)ip, sizeof(ip_t));

	return eth_pack(sizeof(ip_t) + length, 0x0800);
}
Exemple #5
0
int ip_pack(unsigned int length, unsigned char type)
{
	ip_h *ip = (void *)(send_buf + sizeof(eth_h));

	ip->ihl = sizeof(ip_h) >> 2;
	ip->version = 4;
	ip->tos = 0;
	ip->tot_len = htons(sizeof(ip_h) + length);
	ip->id = 0;
	ip->frag_off = 0;
	ip->ttl = 255;
	ip->protocol = type;
	ip->check = 0;
	ip->saddr = htonl(eip);
	ip->daddr = htonl(hip);
	
	ip->check = cal_sum((void *)ip, sizeof(ip_h));

	return eth_pack(sizeof(ip_h) + length, 0x0800);
}
Exemple #6
0
int WriteCode::FindMaxPathSum(TreeNode* root){
	int maxSum = root->val;
	cal_sum(root, maxSum);
	return maxSum;
}