int Fib ( int n ) { if( n < 2 ) { if ( g_sim ) { tbb::task_scheduler_init tsi(P_nested); } return n; } else { tbb::task_scheduler_init *tsi = NULL; tbb::task *cur = NULL; if ( g_sim ) { if ( n % 2 == 0 ) { if ( g_sim == tbbsched_auto_only || (g_sim == tbbsched_mixed && n % 4 == 0) ) { // Trigger TBB scheduler auto-initialization cur = &tbb::task::self(); } else { ASSERT ( g_sim == tbbsched_explicit_only || (g_sim == tbbsched_mixed && n % 4 != 0), NULL ); // Initialize TBB scheduler explicitly tsi = new tbb::task_scheduler_init(P_nested); } } } int x, y; x = cilk_spawn Fib(n-2); y = cilk_spawn Fib(n-1); cilk_sync; if ( tsi ) delete tsi; return x+y; } }
int Fib(int index) { if (index<=2) return 1; else return Fib(index-1)+Fib(index-2); }
int Fib(int N) { if(N <= 1) return 1; else return Fib(N - 1) + Fib(N - 2); }
int Fib(int A){ Con++; if(A == 0) return 0; else if(A == 1) return 1; else if(A > 1){ return Fib(A - 1) + Fib(A - 2); } }
int Fib(int i) { int j, k; if (i < 2) return 1; j = Fib(i-1); k = Fib(i-2); return j + k; }
int main(int argc, char *argv[]) { int termIndex; if(argc==2 && sscanf( argv[1], "%d", &termIndex ) && termIndex >= 0){ if( !cldthread_init() ){ fprintf( stderr, "Please schedule this application using the CloudApp CIEL executor " "instead of attempting to invoke it directly. \n" ); exit( EXIT_FAILURE ); } asprintf( &_group_id, "%p", Fib ); return cldapp_exit( Fib((void*)termIndex) ); } else { printf( "\nInvalid parameters (input must be a positive integer).\n\nUsage: fib <term-index>\n\n" ); } return -1; }
int _tmain(int argc, _TCHAR* argv[]) { int n = 40; printf("\n%d-th member of Fibbonachi sequence is: ", n); //Fib(); printf("%d\n", Fib(n)); getchar(); }
int main(int argc, char *argv[]) { //reads command line printf("The %dth fibanocci number is %d.", atoi(argv[1]), Fib(atoi(argv[1]))); //print statement that calls the fibonnaci function return 0; }
int main(void) { int n,i; printf("Enter the number of terms you want in Fibonacci series \n"); scanf("%d",&n); printf("The Fibonacci number for %d is: \n",n); printf("%d",Fib(n)); }
int main() { int arr[GetConst()] = { 0 };//无法使用运行时常量来初始化数组 enum{ e1 = GetConst(), e2 };//也无法使用运行时常量作为枚举值 int arr2[GetConstexpr()] = { 0 };//ok constexpr ConstexprClass mc(0); //必须使用常量表达式赋值 //constexpr ConstexprClass mc = { 0 };等价上一条 //constexpr ConstexprClass mc{ 0 }; 等价上一条 //gcc-5.1报错:"error: passing 'const Date' as 'this' argument discards qualifiers [-fpermissive]" constexpr Date d(1990, 11, 28); constexpr int year = d.GetYear(); constexpr int month = d.GetMonth(); constexpr int day = d.GetDay(); std::cout << "year:" << year << std::endl << "month" << month << std::endl << "day" << day << std::endl; //结构体NotLiteral不是定义了常量表达式构造函数的类型,因此不能够声明为常量表达式值 //而模版函数一旦以NotLiteral作为参数,那么其constexpr关键字就会被忽略 NotLiteral nl; NotLiteral nl1 = ConstExprFun(nl); constexpr NotLiteral nl2 = ConstExprFun(nl);//编译失败 constexpr int a = ConstExprFun(1); constexpr Literal l1; Literal l2 = ConstExprFun(l1);//ok constexpr Literal l3 = ConstExprFun(l1);//ok //constexpr函数递归 int fibs[] = { Fib(11), Fib(12), Fib(13), Fib(14), Fib(15) }; std::copy(std::begin(fibs), std::end(fibs), std::ostream_iterator<int>(std::cout, " "));//89 144 233 377 610 std::cout << std::endl; //使用模板元实现递归编程 int fibs2[] = { Fibo<11>::result, Fibo<12>::result, Fibo<13>::result, Fibo<14>::result, Fibo<15>::result }; std::copy(std::begin(fibs2), std::end(fibs2), std::ostream_iterator<int>(std::cout, " "));//89 144 233 377 610 return 0; }
int main() { int s,x,y; //seleccion x e y respectivamente do{ //ejemplo de do while printf("nn Seleccione 1 opcionn" //un "menu" las lineas de un printf pueden ser cortadas "-------------------------n" //de esta forma poniendo todo entre comillas " 1.- Potencian" " 2.- Raiz Cuadradan" " 3.- Sumatorian" " 4.- Sumatoria Recursivan" " 5.- Factorialn" " 6.- Factorial Recursivon" " 7.- Fibonaccin" "n 0.- Salirn" "-------------------------n"); //recien aca cerramos la funcion do{ //otro ejemplo da vueltas mientras la seleccion sea "invalida" printf(" Seleccion: "); scanf("%d",&s); }while(s<0||s>7); //la seleccion tiene que estar entre 0 y 7 if(!s) // ! es negacion, en este kaso si s es 0 break; // sale del loop principal printf("Ingrese x: "); //ingreso de datos scanf("%d",&x); if(s==1){ //si es potencia pide el exponente :D printf("Ingrese y: "); scanf("%d",&y); } printf("n"); //una linea en blanco loca switch(s){ // :O? otra funcion de seleccion switch ejecuta desde case 1: printf("%d elevado a %d es: %dn",x,y,Pow(x,y)); //el primer caso verdadero break; //hasta ke termina case 2: printf("La raiz cuadrada de %d es: %dn",x,Sqr(x)); //o encuentra un break break; //por lo general en seleccion case 3: printf("La sumatoria de %d es: %dn",x,Sum(x)); //multiple se usa un break por break; //linea como aca case 4: printf("La sumatoria de %d es: %dn",x,SumLoop(x)); //los case solo verifican break; // igualdad case 5: printf("El factorial de %d es: %dn",x,Fac(x)); //case 5 es lo mismo que break; //if(n==5) case 6: printf("El factorial de %d es: %dn",x,FacLoop(x)); //si sako los break se ejecutari break; //dede la seleecion en adelane case 7: printf("El %dº numero Fibonacci es: %dn",x,Fib(x)); //util par agrupar onda de break; //1 a 3 has esto de 4 a 5 otro default: break; //caso especial este se activa } //en caso de que s sea distinto //de todas las anteriores }while(s); //mientras s sea verdadero osea distinto de 0 return 0; }
int main() { int i; int n; int ans; scanf("%d", &n); printf("%d\n", Fib(n)); return 0; }
int main(int argc, char *argv[]) { int n = 0; printf("请输入你想知道的fib数列的下标数:\n"); while (scanf("%d", &n) == 1) { printf("%d\n", Fib(n)); printf("请输入另外一个下标数:\n"); } return 0; }
int main(int argc, char const *argv[]) { int lines,i,index; scanf("%d",&lines); getchar(); for (i=0;i<lines;i++){ scanf("%d",&index); getchar(); printf("%d\n",Fib(index)); } return 0; }
int main(){ int Cont, Vezes, N; scanf("%d", &Vezes); for(Cont=0; Cont < Vezes; Cont++){ scanf("%d", &N); printf("fib(%d) = %d calls = %d\n", N, Con-1,Fib(N)); Con = 0; } return 0; }
/**************main()**************/ void main(void) { int i; printf("費氏數列如下:"); for (i = 0; i <= 25; i++) { if (i % 8 == 0) { printf("\n"); } printf("%d\t", Fib(i)); } printf("......\n"); /* system("pause"); */ }
int Fib (int userInput) { //parameter for function int value[userInput]; //initial for use later in function int i; //initiailize for use in 'for loop' if (userInput == 0) { //part of fibonacci's number to equal 0 return 0; //0 will be returned } if (userInput == 1) { //it is part of fibonacci's number to equal 1 when userInput = 1 return 1; } if (userInput >1) { //If userInpt is greater than 1, it'll enter the if statement for(i=2; i<=userInput; ++i) { value[1] = 1; //stores 1 into value[1] value [0] = 0; //stores 0 into value[0] value[i] = value [i-1] + value [i - 2]; //Fibonnaci's theorem } return Fib(userInput-1)+value[userInput - 2]; //Recursive function } return 0; }
void RunCilkOnly ( tbb_sched_injection_mode_t sim ) { g_sim = sim; int m = Fib(N); ASSERT( m == F, NULL ); }
int Fib(int n) //fibonnaci recursvivo { if(n>1) //si n es mayor a 1... return Fib(n-1)+Fib(n-2); //DOBLEMENTE RECURSIVO si la recursividad aun no te afecta return n; //este es el momento XD, (caso base n (0 y 1) }
int main() { int n; scanf("%d",&n); printf("fib %d is %d",n,Fib(n)); return 0; }
int main(int argc, char **argv){ if(argc != 3){ usage(argv[0]); exit(EXIT_FAILURE); } std::ifstream fin; fin.open(argv[1]); if(!fin.good()){ std::cerr << argv[0] << ": "; perror(argv[1]); exit(EXIT_FAILURE); } std::ofstream fout; fout.open(argv[2]); if(!fout.good()){ std::cerr << argv[0] << ": "; perror(argv[2]); exit(EXIT_FAILURE); } /*** parse file and create graph ***/ int nGraphs, nVertices, nEdges;//, v1, v2 , weight; fin >> nGraphs; fout << "There are " << nGraphs << " graphs in " << argv[1] << std::endl; for(int i = 0; i < nGraphs; i++){ // build graph fin >> nVertices; fin >> nEdges; Graph graph(nVertices); for(int e = 0; e < nEdges; e++){ Edge edge; fin >> edge; if(DEBUG) std::cerr << "received edge: " << edge << std::endl; graph.insertEdge(edge); } if(DEBUG){ std::cerr << graph.numVertices() << " vertices, " << graph.numEdges() << " edges" << std::endl; graph.showEdges(); graph.showAdj(false); } // run algorithms and time Graph kruskal_mst(nVertices); Timer kruskal_timer; kruskal_timer.start(); Kruskal(graph, kruskal_mst); kruskal_timer.end(); Graph prim_mst(nVertices); Timer prim_timer; prim_timer.start(); Prim(graph, prim_mst); prim_timer.end(); Graph fib_mst(nVertices); Timer fib_timer; fib_timer.start(); Fib(graph, fib_mst); fib_timer.end(); // report timings fout << "Graph " << i+1 << ": " << nVertices << " vertices, " << nEdges << " edges" << std::endl; fout << "Kruskal: "; kruskal_timer.printElapsedSeconds(fout); if(DEBUG){ std::cerr << "Kruskal MST:" << std::endl; kruskal_mst.showEdges(); kruskal_mst.printEdges(fout); fout << "\tKRUSKALS: SUM OF EDGES = " << kruskal_mst.sumEdges() << std::endl;// " numEdges=" << kruskal_mst.numEdges() << std::endl; } fout << "Prim: "; prim_timer.printElapsedSeconds(fout); extractMST(prim_mst); if(DEBUG){ std::cerr << "Prim MST:" << std::endl; prim_mst.showEdges(); prim_mst.printEdges(fout); fout << "\tPRIMS: SUM OF EDGES = " << prim_mst.sumEdges() << std::endl;//" numEdges=" << prim_mst.numEdges() << std::endl; } fout << "Fib Heap: "; fib_timer.printElapsedSeconds(fout); extractMST(fib_mst); if(DEBUG){ std::cerr << "Fib MST:" << std::endl; fib_mst.showEdges(); fib_mst.printEdges(fout); fout << "\tFIB-HEAP: SUM OF EDGES = " << fib_mst.sumEdges() << std::endl;// << " numEdges=" << fib_mst.numEdges() << std::endl; } } return 0; }
static Fib getFib(int n) { Fib x = Fib(n); return x; }
int fib() { if (n < 2) return 1; return Fib(n - 1).fib() + Fib(n - 2).fib(); }
void operator() ( int ) const { int m = Fib(N); ASSERT( m == F, NULL ); }
//constexpr递归问题 constexpr int Fib(int n) { return (n == 1 || n == 2 ? 1 : (Fib(n - 1) + Fib(n - 2))); }
int main(int argc, char *argv[]) { int i; pid_t p1, p2; //< the pid of two child process int n; //< the input number char *endptr; //< the pointer to the last char int shmid; //< the shared memory identify void *sharedMem = (void *)0; SharedStruct *data; /* Check the parameter*/ if(argc != 2) { fprintf(stderr, "Usage: %s N\n", argv[0]); exit(1); } errno = 0; n = strtol(argv[1], &endptr, 10); if(errno != 0) { perror("strtol"); exit(1); } if(endptr == argv[1] || *endptr != '\0') { fprintf(stderr, "Argument type error\n"); exit(1); } if(n > MAX_SEQUENCE) { fprintf(stderr, "Out of max sequence\n"); exit(1); } else if(n < 0) { fprintf(stderr, "Argument shouldn't be negative\n"); exit(1); } /* fork the first child process*/ p1 = fork(); if(p1 == -1) { perror("fork"); exit(1); } else if(p1) { /* the first child process*/ printf("Pid of 1st child process: %d\n", getpid()); execlp("ls", "ls", "-lR", (char *)0); perror("execlp"); exit(1); } wait(NULL); //< wait for the first child process terminated /* Get a new shared memory identifier*/ shmid = shmget(IPC_PRIVATE, sizeof(SharedStruct), 0666 | IPC_CREAT); if(shmid == -1) { fprintf(stderr, "shmget failed\n"); exit(1); } /* Fork a second child process*/ p2 = fork(); switch(p2) { case -1: perror("fork"); exit(1); break; case 0: /* child process*/ sharedMem = shmat(shmid, (void *)0, 0); if(sharedMem == (void *)-1) { fprintf(stderr, "shmat failed"); exit(1); } /* get the shared memory, and write data*/ data = (SharedStruct *)sharedMem; Fib(n, data); if(shmdt(sharedMem) == -1) { fprintf(stderr, "shmdt failed\n"); exit(1); } break; default: /* parent process*/ sharedMem = shmat(shmid, (void *)0, 0); if(sharedMem == (void *)-1) { fprintf(stderr, "shmat failed"); exit(1); } data = (SharedStruct *)sharedMem; wait(NULL); //< wait for the second child process terminated /* print the data*/ printf("The fitst %d numbers of Fibonacci sequence is:\n", n); for(i = 0; i < data->sequenceSize; i++) { printf("%ld\t", data->fib[i]); if(i % 8 == 7) putchar('\n'); } if(i % 8) putchar('\n'); if(shmdt(sharedMem) == -1) { fprintf(stderr, "shmdt failed\n"); exit(1); } if(shmctl(shmid, IPC_RMID, 0) == -1) { fprintf(stderr, "shmctl(IPC_RMID) failed\n"); exit(1); } break; } exit(0); }