/* * @brief Utility to find the sum of node values a subtree */ int get_sum(struct t_node *node) { if (node == NULL) return 0; return (node->data + get_sum(node->left) + get_sum(node->right)); }
int main(int argc, const char *argv[]) { int m, n, i, sum=0; scanf("%d", &m); n = square(m); //printf("DEBUG INPUT MATRIX:\n"); int a[n]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); //DEBUG //printf("%3d ", a[i]); //if (!((i+1) % m)) // printf("\n"); } if (n == 1) { printf("%d", a[0]); return 0; } //printf("DEBUG DIA:\n"); sum += get_sum(a, n, 0, m+1); //DEBUG //printf("\n"); sum += get_sum(a, n-1, m-1, m-1); if (m % 2) { //printf("DEBUG - %d\n", a[(n-1)/2]); sum -= a[(n-1)/2]; } printf("%d", sum); return 0; }
int get_sum(int i, int j, int k) { if (k >= n*4) { return 0; } TreeNode root = tree[k]; if (i <= root.start && root.end <= j) { return root.val; } int li = 2*k; int ri = 2*k + 1; int leftSum = 0; if (li < 4*n) { if (tree[li].end >= i) { leftSum = get_sum(i,j, 2*k); } } int rightSum = 0; if (ri < 4*n) { if (tree[ri].start <= j) { rightSum = get_sum(i,j,2*k +1); } } return leftSum + rightSum; }
void solve_answer(int n, int m, int k) { int ans = 0; for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { ans = max(ans, A[i - 1][j - 1] + C[i][j - 1] + D[1][j]); ans = max(ans, C[1][j - 1] + B[i - 1][j] + D[i][j]); ans = max(ans, A[i - 1][j - 1] + B[i - 1][j] + D[i][1]); ans = max(ans, B[i - 1][1] + C[i][j - 1] + D[i][j]); } } for(int i = k; i + k <= n; ++i) { for(int j = 1; j + k - 1 <= m; ++j) { ans = max(ans, B[i][1] + D[i + k + 1][1] + get_sum(i + 1, j, i + k, j + k - 1)); } } for(int i = 1; i + k - 1 <= n; ++i) { for(int j = k; j + k <= m; ++j) { ans = max(ans, C[1][j] + D[1][j + k + 1] + get_sum(i, j + 1, i + k - 1, j + k)); } } std::printf("%d", ans); }
int get_sum(struct node *temp){ int sum = 0; if (temp == NULL) return 0; sum = get_sum(temp->left); sum += get_sum(temp->right) + temp->data; return sum; }
int get_sum(struct node *root) { int sum = 0; if (root != NULL) { sum += get_sum(root->left); sum += root->data; sum += get_sum(root->right); } return sum; }
void update_l(component * components, segment * data, int K){ for (int k = 0; k < K; k++){ //forward double left_SUM=0, right_SUM=get_sum(data,components[k].forward.j ,components[k].forward.k,1); double N = left_SUM+right_SUM; double null_vl = 0.05 / (data->maxX - data->minX ); double null_ll = N*LOG(1. / (data->maxX - data->minX )); double null_BIC = -2*null_ll + LOG(N); double vl = 0, w=0; double mod_ll, mod_BIC; double prev_prev=0, prev=0, current=0; double BIC_best = 0; int arg_l = components[k].forward.k; for (int l = components[k].forward.j; l < components[k].forward.k; l++ ){ left_SUM+=data->X[1][l]; right_SUM-=data->X[1][l]; vl = 1.0/(data->X[0][l]-data->X[0][components[k].forward.j]); w = left_SUM/(N); mod_ll = LOG(w*vl)*left_SUM + LOG(null_vl)*right_SUM ; mod_BIC = -2*mod_ll + 5*LOG(N); current = null_BIC/mod_BIC; if (prev > current and prev > prev_prev and prev > 1.0){ if (prev > BIC_best){ BIC_best=prev, arg_l=l; } } prev_prev=prev; prev = current; } components[k].forward.b = data->X[0][arg_l]; //reverse arg_l = components[k].reverse.j; left_SUM=0, right_SUM=get_sum(data,components[k].reverse.j,components[k].reverse.k,2 ); N = left_SUM+right_SUM; null_ll = N*LOG(1. / (data->maxX - data->minX )); null_BIC = -2*null_ll + LOG(N); prev_prev=0, prev=0, current=0,BIC_best = 0; for (int l = components[k].reverse.j; l < components[k].reverse.k; l++ ){ left_SUM+=data->X[2][l]; right_SUM-=data->X[2][l]; vl = 1.0/(data->X[0][components[k].reverse.k] - data->X[0][l]); w = right_SUM/(N); mod_ll = LOG(null_vl)*left_SUM + LOG(w*vl)*right_SUM ; mod_BIC = -2*mod_ll + 5*LOG(N); current = null_BIC/mod_BIC; if (prev > current and prev > prev_prev and prev > 1.0){ if (prev > BIC_best){ BIC_best=prev, arg_l=l; } } prev_prev=prev; prev = current; } components[k].reverse.a = data->X[0][arg_l]; } }
int main() { int n; while(scanf("%d",&n),n>0) { memset(tree,0,sizeof(int)*MAX*MAX); scanf("%d%d",&w,&h); for(int i=0;i<n;i++) { int x,y; scanf("%d%d",&x,&y); update(x,y); } int s,t; scanf("%d%d",&s,&t); int max=0; for(int i=1;i+s-1<=w;i++) { for(int j=1;j+t-1<=h;j++) { int sum=get_sum(i,j,i+s-1,j+t-1); if(max<sum) { max=sum; } } } printf("%d\n",max); } return 0; }
int main() { int i = 100, result; result = get_sum(i); printf("1+2+...+%d = %d\n", i, result); return 0; }
/** * Compute command */ void command_compute(char* line) { char cmd[MAX_BUFFER]; char key[MAX_BUFFER]; char func[MAX_BUFFER]; char arg1[MAX_BUFFER]; int argc = sscanf(line, "%s %s %s %s", cmd, func, key, arg1); if (argc < 3) { goto invalid; } MATRIX_GUARD(key); uint32_t result = 0; if (strcasecmp(func, "sum") == 0) { result = get_sum(m); } else if (strcasecmp(func, "trace") == 0) { result = get_trace(m); } else if (strcasecmp(func, "minimum") == 0) { result = get_minimum(m); } else if (strcasecmp(func, "maximum") == 0) { result = get_maximum(m); } else if (strcasecmp(func, "frequency") == 0) { result = get_frequency(m, atoll(arg1)); } else { goto invalid; } printf("%" PRIu32 "\n", result); return; invalid: puts("invalid arguments"); }
void solve() { test_no ++; for ( int i = 0; i < N; ++ i ) id[i] = i; min_sum = std::numeric_limits<double>::max(); double min_x[SIZE]; double min_y[SIZE]; do { for ( int i = 0; i < N; ++ i ) { tmp_x[i] = X[id[i]]; tmp_y[i] = Y[id[i]]; } double sum = get_sum(tmp_x, tmp_y); if ( sum < min_sum ) { min_sum = sum; for ( int i = 0; i < N; ++ i ) { min_x[i] = tmp_x[i]; min_y[i] = tmp_y[i]; } } } while ( next_permutation(id, id + N) ); for ( int i = 0; i < N; ++ i ) { X[i] = min_x[i]; Y[i] = min_y[i]; } }
int main() { int input; while (scanf("%d", &input) != EOF) printf("%d\n\n", get_sum(input)); return 0; }
int main(void) { int ten[N] = {56, 89, 66, 37, 98, 77, 62, 82, 50, 71}; int sum; sum = get_sum(ten, N); printf("合計点は%d点です。\n", sum); return 0; }
float Histogramm::get_mean(float border1, float border2) { float weighted_sum = 0; for(int i = get_class_num(border1); i <= get_class_num(border2); i++) { weighted_sum += histogramm[i] * get_class_middle(i); } int sum = get_sum(border1, border2); float mean = 0; if(sum > 0) { mean = weighted_sum/sum; } return mean; }
/* * @brief Checks if the given binary tree is a sumtree or NOT */ bool is_sumtree(struct t_node *node) { int left_sum = 0; int right_sum = 0; /* If the node is NULL or it's left & right subtrees are both null, * quiet obviously the tree is a sumtree as per definition */ if (node == NULL || (node->left == NULL && node->right == NULL)) return true; /* If node != NULL & it's left & right subtree are not NULL, check * if the sum of left & right nodes equal that of their parent node */ left_sum = get_sum(node->left); right_sum = get_sum(node->right); if ((node->data == left_sum + right_sum) && is_sumtree(node->left) && is_sumtree(node->right)) return true; /* All else fails, return false */ return false; }
void DLL_EXPORT modify(queue_t * queue) { int sum = get_sum(queue); while(sum >= 50) { int el = queue_dequeue(queue); sum -= el; if(el > 0) el = - el; queue_enqueue(queue, el); sum+= el; } }
Int rec( const Int& cur ) { Int res = std::numeric_limits<Int>::min(); for ( auto i = cur; i < N; ++ i ) { if ( ! check_connected(i) ) continue; used[i] = true; cnt ++; max(res, std::max(get_sum(), rec(i + 1))); cnt --; used[i] = false; } return res; }
void solve_partial(int n, int m, int k) { for(int i = k; i <= n; ++i) { for(int j = k; j <= m; ++j) { A[i][j] = max(A[i - 1][j], A[i][j - 1], get_sum(i - k + 1, j - k + 1, i, j)); } } for(int i = k; i <= n; ++i) { for(int j = m - k + 1; j; --j) { B[i][j] = max(B[i - 1][j], B[i][j + 1], get_sum(i - k + 1, j, i, j + k - 1)); } } for(int i = n - k + 1; i; --i) { for(int j = k; j <= m; ++j) { C[i][j] = max(C[i + 1][j], C[i][j - 1], get_sum(i, j - k + 1, i + k - 1, j)); } } for(int i = n - k + 1; i; --i) { for(int j = m - k + 1; j; --j) { D[i][j] = max(D[i + 1][j], D[i][j + 1], get_sum(i, j, i + k - 1, j + k - 1)); } } }
int main() { int diag1[m_size]; for (int i = 0; i < m_size; ++i) { diag1[i] = 0; } long count = 0; while(1) { int sum = get_sum(diag1, m_size); int diag2[m_size]; for (int i = 0; i < m_size; ++i) { diag2[i] = 0; } while(1) { int last = sum - get_sum(diag2, m_size - 1); if (last < 10 && last >= 0) { diag2[m_size - 1] = last; count += try_diagonals1(diag1, diag2, sum) * try_diagonals2(diag1, diag2, sum); } if (!inc(diag2, m_size - 1)) { break; } } if (!inc(diag1, m_size)) { break; } } std::cout << count << std::endl; }
int main(){ int extra; char *len = "prime"; int num; int is_prime,i; for(i = 0;i < 10;i++){printf("Introduceti numar:"); scanf("%d", &num); is_prime = check_if_prime(num); if(is_prime) printf("is %s\n",len); else printf("is not %s\n",len); extra = get_sum(num); printf("sum (1..%d) is %d\n",num,extra); } return 0; }
int main() { char name[STUDENTS][20]; int marks[STUDENTS][SUBJECTS+1]; printf("Input students names & their marks in four subjects\n"); get_list(name, marks, STUDENTS, SUBJECTS); get_sum(marks, STUDENTS, SUBJECTS+1); printf("\n"); print_list(name,marks,STUDENTS,SUBJECTS+1); get_rank_list(name, marks, STUDENTS, SUBJECTS+1); printf("\nRanked List\n\n"); print_list(name,marks,STUDENTS,SUBJECTS+1); return 0; }
int main(){ int n; scanf("%d",&n); int i; for(i=1;i<=32000+1;i++)tree[i]=0; for(i=0;i<n;i++)star[i]=0; for(i=1;i<=n;i++){ int x,y; scanf("%d%d",&x,&y); x++; star[ get_sum(x) ]++; add(x,1); } for(i=0;i<n;i++)printf("%d\n",star[i]); return 0; }
int main() { int n = 0; scanf("%d", &n); struct frac sum = {0, 1}; for (int i = 0; i < n; i++) { scanf("%ld/%ld", &temp.a, &temp.b); sum = get_sum(sum, temp); } if (sum.a == 0) printf("0"); if (abs(sum.a / sum.b) > 0) { printf("%ld", sum.a / sum.b); sum.a %= sum.b; if (sum.a != 0) printf(" "); } if (sum.a != 0) printf("%ld/%ld", sum.a, sum.b); return 0; }
int main() { int t; scanf("%d",&t); for(int no=1;no<=t;no++) { int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<k;i++) { scanf("%d%d",&(points[i].x),&(points[i].y)); } std::sort(points,points+k,cmp); long long count=0; memset(tree,0,sizeof(int)*1001); for(int i=0;i<k;i++) { count+=get_sum(points[i].x-1); update(points[i].x,n); } printf("Test case %d: %lld\n",no,count); } return 0; }
/** * Compute command. */ void command_compute(char* line) { char cmd[MAX_BUFFER]; char key[MAX_BUFFER]; char func[MAX_BUFFER]; char arg1[MAX_BUFFER]; int argc = sscanf(line, "%s %s %s %s", cmd, func, key, arg1); if (argc < 3) { goto invalid; } MATRIX_GUARD(key); float result = 0; if (strcasecmp(func, "sum") == 0) { result = get_sum(m); } else if (strcasecmp(func, "trace") == 0) { result = get_trace(m); } else if (strcasecmp(func, "minimum") == 0) { result = get_minimum(m); } else if (strcasecmp(func, "maximum") == 0) { result = get_maximum(m); } else if (strcasecmp(func, "determinant") == 0) { result = get_determinant(m); } else if (strcasecmp(func, "frequency") == 0) { ssize_t count = get_frequency(m, atof(arg1)); printf("%zu\n", count); return; } else { goto invalid; } printf("%.2f\n", result); return; invalid: puts("invalid arguments"); }
int sumRange(int i, int j) { return get_sum(i, j, 1); }
inline int get_sum(int l,int b,int r,int t) { return get_sum(r,t)-get_sum(l-1,t)-get_sum(r,b-1)+get_sum(l-1,b-1); }
int test(int iteration) { long i; pthread_attr_t attr; pthread_t thread[nbthreads]; hash_init(); init_allocator(); pthread_attr_init(&attr); done = 0; go = 0; ready = 0; memory_barrier(); //printf("loop %d is started\n", iteration); //printf("thread !!%d\n",thread); for(i=0; i< nbthreads; i++) { set_affinity(i); //printf("thread = %d ...", i); if(pthread_create(&thread[i], &attr, run, (void*)i)) { perror("creat"); } //printf("thread = %d created\n", i); } while(ready != nbthreads) memory_barrier(); go = 1; memory_barrier(); void* res; for(i=(nbthreads-1); i >= 0; i--) { pthread_join(thread[i], &res); if(i == nbupdaters) { done = 1;//Stop the writers! memory_barrier(); } } //printf("Done!\n", i); unsigned nbcores = get_nbcores(); unsigned mtc = nbcores < nbreaders ? nbcores : nbreaders; run_avg_rd[iteration] = get_avg(&avg_time[nbupdaters], nbreaders); run_avg_wr[iteration] = get_avg(&avg_time[0], nbupdaters); run_max_rd[iteration] = get_max(&avg_time[nbupdaters], nbreaders); run_max_wr[iteration] = get_max(&avg_time[0], nbupdaters); run_min_rd[iteration] = get_min(&avg_time[nbupdaters], nbreaders); run_min_wr[iteration] = get_min(&avg_time[0], nbupdaters); run_retry[iteration] = get_sum(&thd_retry[nbupdaters], nbreaders); run_relink[iteration] = get_sum(&thd_relink[nbupdaters], nbreaders); run_reprev[iteration] = get_sum(&thd_reprev[nbupdaters], nbreaders); run_cput[iteration] = get_cpu_work(&thd_time[nbupdaters], nbreaders, mtc)/NB_TEST; run_mallocs[iteration] = get_sum(&thd_mallocs[0], nbupdaters); run_blockeds[iteration] = get_sum(&thd_blockeds[0], nbupdaters); run_sea[iteration] = get_avg(&suc_sea[nbupdaters], nbreaders); run_del[iteration] = get_avg(&suc_del[0], nbupdaters); run_ins[iteration] = get_avg(&suc_ins[0], nbupdaters); //printf("avg = %gus\n", run_avg_rd[iteration]); //printf("first reader %gus\n", thd_time[nbupdaters]); double time = get_avg(&thd_time[nbupdaters], nbreaders); trd_ops[iteration] = get_avg(&thd_ops[nbupdaters], nbreaders)/time; twr_ops[iteration] = get_avg(&thd_ops[0], nbupdaters)/time; //hash_delete(); //delete_allocator(); }
void frag_and_send(u_int8_t *payload, u_int32_t total_pload_size) { /* Builds and sends the first packet, calling get_sum() to * * get the correct checksum for the ICMP packet (with the * * whole payload). Then builds and sends IP fragments * * until all the payload is sent. */ char ip_addr_str[16]; u_int32_t ip_addr, src_addr; u_int16_t id, seq, ip_id; /* hdr_offset = fragmentation flags + offset (in bytes) * * divided by 8 */ int pload_offset, hdr_offset; int bytes_written, max_pload_size, packet_pload_size; libnet_ptag_t ip_tag; /* Generating random IDs */ id = (u_int16_t)libnet_get_prand(LIBNET_PR16); /* We need a non-zero id number for the IP headers, * * otherwise libnet will increase it after each * * build_ipv4, breaking the fragments */ ip_id = (u_int16_t)libnet_get_prand(LIBNET_PR16); seq = 1; /* Getting IP addresses */ src_addr = libnet_get_ipaddr4(l); if (src_addr == -1) { fprintf(stderr, "Couldn't get own IP address: %s\n", libnet_geterror(l)); libnet_destroy(l); exit(EXIT_FAILURE); } printf("Destination IP address: "); scanf("%15s", ip_addr_str); ip_addr = libnet_name2addr4(l, ip_addr_str, LIBNET_DONT_RESOLVE); if (ip_addr == -1) { fprintf(stderr, "Error converting IP address.\n"); libnet_destroy(l); exit(EXIT_FAILURE); } /* Getting max payload size */ max_pload_size = (MTU - LIBNET_IPV4_H); /* making it a multiple of 8 */ max_pload_size -= (max_pload_size % 8); pload_offset = 0; /* Building the first packet, which carries the ICMP * * header */ /* We're doing (payload size - icmp header size) and not * * checking if it's a multiple of 8 because we know the * * header is 8 bytes long */ if (total_pload_size > (max_pload_size - LIBNET_ICMPV4_ECHO_H)) { hdr_offset = IP_MF; packet_pload_size = max_pload_size - LIBNET_ICMPV4_ECHO_H; } else { hdr_offset = 0; packet_pload_size = total_pload_size; } /* ICMP header */ if (libnet_build_icmpv4_echo(ICMP_ECHO, 0, get_sum(payload, total_pload_size, id, seq), id, seq, payload, packet_pload_size, l, 0) == -1) { fprintf(stderr, "Error building ICMP header: %s\n", libnet_geterror(l)); libnet_destroy(l); exit(EXIT_FAILURE); } /* First IP header (no payload, offset == 0) */ if (libnet_build_ipv4( (LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H + packet_pload_size), 0, ip_id, hdr_offset, 255, IPPROTO_ICMP, 0, src_addr, ip_addr, NULL, 0, l, 0) == -1) { fprintf(stderr, "Error building IP header: %s\n", libnet_geterror(l)); libnet_destroy(l); exit(EXIT_FAILURE); } /* Writing packet */ bytes_written = libnet_write(l); if (bytes_written != -1) printf("%d bytes written.\n", bytes_written); else fprintf(stderr, "Error writing packet: %s\n", libnet_geterror(l)); /* Updating the offset */ pload_offset += packet_pload_size; /* Clearing */ /* We need to get rid of the ICMP header to build the * * other fragments */ libnet_clear_packet(l); ip_tag = LIBNET_PTAG_INITIALIZER; /* Looping until all the payload is sent */ while (total_pload_size > pload_offset) { /* Building IP header */ /* checking if there will be more fragments */ if ((total_pload_size - pload_offset) > max_pload_size) { /* In IP's eyes, the ICMP header in the first packet * needs to be in the offset, so we add its size to * the payload offset here */ hdr_offset = IP_MF + (pload_offset + LIBNET_ICMPV4_ECHO_H) / 8; packet_pload_size = max_pload_size; } else { /* See above */ hdr_offset = (pload_offset + LIBNET_ICMPV4_ECHO_H) / 8; packet_pload_size = total_pload_size - pload_offset; } ip_tag = libnet_build_ipv4((LIBNET_IPV4_H + max_pload_size), 0, ip_id, hdr_offset, 255, IPPROTO_ICMP, 0, src_addr, ip_addr, (payload + pload_offset), packet_pload_size, l, ip_tag); if (ip_tag == -1) { fprintf(stderr, "Error building IP header: %s\n", libnet_geterror(l)); libnet_destroy(l); exit(EXIT_FAILURE); } /* Writing packet */ bytes_written = libnet_write(l); if (bytes_written != -1) printf("%d bytes written.\n", bytes_written); else fprintf(stderr, "Error writing packet: %s\n", libnet_geterror(l)); /* Updating the offset */ pload_offset += packet_pload_size; } }
static inline int partial_process(vector<int>& ratings, int index, int& base, int& sum) { int inc = 0, dec = 0; int max = 0; if (index == ratings.size() - 1) { sum += base + 1; return -1; } //连续相等情况[5,1,1,1],可以忽略base if (index == 0 || ratings[index - 1] == ratings[index]) { base = 0; } if (ratings[index + 1] == ratings[index]) { sum += base + 1; base = 0; return index+1; } while(ratings[index + 1] > ratings[index]) { inc++; index++; //最后的时候max是极大值 max = ++base; sum += max; //到达末尾了 if (index == ratings.size() - 1) { sum += base + 1; break; } }; if (index == ratings.size() -1) { return -1; } //获得极大值 max = base + 1; bool is_max_equal = false; if (ratings[index] == ratings[index + 1]) { is_max_equal = true; } //保证最少有一个dec do { dec++; index++; }while(index < ratings.size() - 1 && ratings[index + 1] < ratings[index]); if (dec >= max) { sum += get_sum(1, dec + 1); } else { sum += max; sum += get_sum(1, dec); } //[7,8,8,7]的情况。max = 2,dec = 2,但是8 == 8,所以两个8的值可以相等, if (dec == max && is_max_equal) { sum--; } base = 1; if (index == ratings.size() -1) { return -1; } else { return index + 1; } }