bool doSum(TreeNode *root, int sum, int cur) { if (root == NULL) return false; cur = cur + root->val; if (cur == sum && root->left == NULL && root->right == NULL) return true; return doSum(root->left, sum, cur) || doSum(root->right, sum, cur); }
bool doSum(TreeNode *root, int sum, int cur, vector<int>v) { if (root == NULL) return false; cur = cur + root->val; v.push_back(root->val); if (cur == sum && root->left == NULL && root->right == NULL) { results.push_back(v); return true; } bool left = doSum(root->left, sum, cur, v); bool right = doSum(root->right, sum, cur, v); return left || right; }
int main(void) { int integers[NINTS] = { 0 }; intNodePtr list1 = NULL; int i, sum = 0; srand(time(NULL)); printf("Unsorted data:\n"); /* initializing the array */ for (i = 0; i < NINTS; i++) { printf("%3d%s", integers[i] = dice(), (i + 1) % WRAP == 0 ? "\n" : " "); } printf("\n"); /* initialize the list */ for (i = 0; i < NINTS; i++) { insert(&list1, integers[i]); } printList(list1); /* stats */ sum = doSum(list1); printf("Sum = %d\nMean = %.2f\n", sum, ((float) sum) / NINTS); return 0; }
vector<vector<int> > pathSum(TreeNode *root, int sum) { // Start typing your C/C++ solution below // DO NOT write int main() function results.clear(); vector<int> v; doSum(root, sum, 0, v); return results; }
void printDiff(){ double Sn=(M_PI*M_PI)/6; double sum=0; double time=0; for (int i = 4; i < 15; ++i) { time = WallTime(); sum = doSum(genVector(pow(2, i))); printf("Diff (n=%f) = %f,",pow(2, i), sum-Sn); printf(" Elapsed: %fs\n", WallTime()-time); } }
int main(int argc, char** argv) { int size, rank; #ifdef HAVE_MPI MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); #endif if (!(size & (size-1))==0) { printf("Number of processes must be power of two"); #ifdef HAVE_MPI MPI_Finalize(); #endif return 1; } double time = WallTime(); double Sn=(M_PI*M_PI)/6; double sum=0; for (int i = 4; i <15 ; ++i) { int n= pow(2, i); int *startIndex, *len; splitVector(n, size, &len, &startIndex); Vector vec = genVector(startIndex[rank],startIndex[rank]+len[rank]); sum = doSum(vec); #ifdef HAVE_MPI double s2=sum; MPI_Reduce(&s2, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); #endif if (rank == 0) { printf("Diff (n=%d) = %f,",n, sum-Sn); printf(" Elapsed: %fs\n", WallTime()-time); } } #ifdef HAVE_MPI MPI_Finalize(); #endif return 0; }
static void ICACHE_FLASH_ATTR saveSum(void) { sysCfg.sumcheck = doSum(); }
static bool ICACHE_FLASH_ATTR checkSum(void) { return sysCfg.sumcheck == doSum(); }
bool hasPathSum(TreeNode *root, int sum) { // Start typing your C/C++ solution below // DO NOT write int main() function doSum(root, sum, 0); }
int MergeToRAM(void) { FILE *srcFile, *destFile, *listFile; int i=0,idx; char fileLine[LENGTH_READ_BUF], mulu[LENGTH_READ_PATH],tmp[513]; char opcode[LENGTH_READ_BUF]; int l,j, fd; #ifdef CONFIG_NK_DB_CHECKSUM unsigned int sum=0; #endif //protect merge process umask(0); fd = open("/tmp/test.txt", O_RDONLY | O_CREAT | O_EXCL, 0666); j = 0; while ( fd < 0) { srand(time(0)); j = 700 + (int) (900.0 * rand()/(RAND_MAX + 1.0)); for (i = 0; i < j; i ++); fd = open ("/tmp/test.txt", O_RDONLY | O_CREAT | O_EXCL, 0666); } i = close(fd); i = unlink("/tmp/test.txt"); fileLine[0]='\0'; tmp[0]='\0'; if ( NULL == (destFile = fopen("/tmp/new_one", "w+"))) return 0; else { if ( NULL == (listFile = fopen("/tmp/category_list", "r+"))) { fclose(destFile); return 0; } else { while(fgets(fileLine,LENGTH_READ_SIZE,listFile) != NULL) { idx = i = 0; while ( fileLine[idx] != ' ' && fileLine[idx] != '\0' && fileLine[idx] != '\n') opcode[i++] = fileLine[idx++]; opcode[i] = '\0'; sprintf(mulu,"/tmp/splitDB/%s",opcode); if ( NULL == (srcFile = fopen(mulu, "r+"))) { fclose(destFile); fclose(listFile); return 0; } else { #ifdef CONFIG_NK_DB_CHECKSUM if ( !strcmp ( opcode, "CHECKSUM" ) ) { } else { while(fgets(fileLine,LENGTH_READ_SIZE,srcFile) != NULL) { sum += doSum ( fileLine, strlen ( fileLine ) ); fputs(fileLine,destFile); }//while(fgets(fileLine,LENGTH_READ_SIZE,srcFile) != NULL) sum += doSum ( "\n", strlen ( "\n" ) ); fputs("\n",destFile); } fclose(srcFile); #else while(fgets(fileLine,LENGTH_READ_SIZE,srcFile) != NULL) { fputs(fileLine,destFile); }//while(fgets(fileLine,LENGTH_READ_SIZE,srcFile) != NULL) fputs("\n",destFile); fclose(srcFile); #endif } }//while(fgets(fileLine,LENGTH_READ_SIZE,listFile) != NULL) fclose(listFile); #ifdef CONFIG_NK_DB_CHECKSUM fputs ( "[CHECKSUM]\n", destFile ); sprintf ( tmp, "SUM=%u\n", sum ); fputs ( tmp, destFile ); #endif } } fclose(destFile); rename("/tmp/new_one", "/tmp/nk_sysconfig"); i = close(fd); i = unlink("/tmp/test.txt"); return 1; }
void risk(T_MODULE * modules, int n_mod, int *covVector, int options) { int i; int j; int cov; T_MODULE *mod; int decis_var; int maxLine; float (*total)[COV_N]; float (*covered)[COV_N]; float sum[COV_N]; T_FUNC *func; int nFunc; int start; int end; total = NULL; covered = NULL; for (mod = modules; mod < modules + n_mod; ++mod) { if (mod->n_file < 1) continue; maxLine = 0; nFunc = 0; for (i = 0; i < (int) mod->n_func; ++i) { func = mod->func + i; if (func->ignore) continue; ++nFunc; /* * Blocks. */ if (options & OPTION_BLOCK) for (j = 1; j < (int) func->n_blk; ++j) { cov = covVector[func->blkCovStart + j]; if (cov == -1) cov = 0; doBlk(func->blk + j, cov, &maxLine, &total, &covered, COV_BLOCK, AMT_BLOCK); } /* * Decisions. */ if (options & OPTION_DECIS) for (j = 0; j < (int) func->n_puse; ++j) { decis_var = func->decis_var; if (func->puse[j].varno != decis_var) break; cov = covVector[func->pUseCovStart + j]; if (cov == -1) cov = 0; doBlk(func->blk + func->puse[j].blk2, cov, &maxLine, &total, &covered, COV_DECIS, AMT_DPRED); doBlk(func->blk + func->puse[j].blk3, cov, &maxLine, &total, &covered, COV_DECIS, AMT_DTARG); } /* * C-uses */ if (options & OPTION_CUSE) for (j = 0; j < (int) func->n_cuse; ++j) { cov = covVector[func->cUseCovStart + j]; if (cov == -1) cov = 0; doBlk(func->blk + func->cuse[j].blk1, cov, &maxLine, &total, &covered, COV_CUSE, AMT_CDEF); doBlk(func->blk + func->cuse[j].blk2, cov, &maxLine, &total, &covered, COV_CUSE, AMT_CUSE); } /* * P-uses */ if (options & OPTION_PUSE) for (j = 0; j < (int) func->n_puse; ++j) { decis_var = func->decis_var; if (func->puse[j].varno == decis_var) continue; cov = (covVector[func->pUseCovStart + j]); if (cov == -1) cov = 0; doBlk(func->blk + func->puse[j].blk1, cov, &maxLine, &total, &covered, COV_PUSE, AMT_PDEF); doBlk(func->blk + func->puse[j].blk2, cov, &maxLine, &total, &covered, COV_PUSE, AMT_PPRED); doBlk(func->blk + func->puse[j].blk3, cov, &maxLine, &total, &covered, COV_PUSE, AMT_PTARG); } } /* * F filename #units #lines { cov types } { totals } { covered } */ printf("F %s %d %d {", mod->file[0].filename, nFunc, maxLine); if (options & OPTION_BLOCK) printf(" b"); if (options & OPTION_CUSE) printf(" c"); if (options & OPTION_DECIS) printf(" d"); if (options & OPTION_PUSE) printf(" p"); printf(" } {"); /* * Totals: */ doSum(total, sum, 1, maxLine); if (options & OPTION_BLOCK) printf(" %.1f", sum[COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", sum[COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", sum[COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", sum[COV_PUSE]); /* * Covered: */ printf(" } {"); doSum(covered, sum, 1, maxLine); if (options & OPTION_BLOCK) printf(" %.1f", sum[COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", sum[COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", sum[COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", sum[COV_PUSE]); printf(" }\n"); for (i = 0; i < (int) mod->n_func; ++i) { func = mod->func + i; if (func->ignore) continue; start = func->blk[0].pos.start.line; end = func->blk[0].pos.end.line; if (start > end) { start = end; end = func->blk[0].pos.start.line; } /* * U funcname #funcs beginLine endLine { totals } { covered } */ printf("U %s %d %d {", func->fname, start, end); /* * Totals: */ doSum(total, sum, start, end); if (options & OPTION_BLOCK) printf(" %.1f", sum[COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", sum[COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", sum[COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", sum[COV_PUSE]); /* * Covered: */ printf(" } {"); doSum(covered, sum, start, end); if (options & OPTION_BLOCK) printf(" %.1f", sum[COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", sum[COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", sum[COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", sum[COV_PUSE]); printf(" }\n"); /* * L { totals } { covered } */ if (total == NULL || covered == NULL) continue; for (j = start; j <= end; ++j) { printf("L {"); if (options & OPTION_BLOCK) printf(" %.1f", total[j][COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", total[j][COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", total[j][COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", total[j][COV_PUSE]); printf(" } {"); if (options & OPTION_BLOCK) printf(" %.1f", covered[j][COV_BLOCK]); if (options & OPTION_CUSE) printf(" %.1f", covered[j][COV_CUSE]); if (options & OPTION_DECIS) printf(" %.1f", covered[j][COV_DECIS]); if (options & OPTION_PUSE) printf(" %.1f", covered[j][COV_PUSE]); printf(" }\n"); } } free(total); free(covered); } }