资源描述:
《浙江大学城市学院程序设计二上机答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、夏5周10014计算函数P(n,x)输入一个正整数repeat(0=0)和一个双精度浮点数x,输出函数p(n,x)的值(保留2位小数)。[1(n=0)p(n,x)=[x(n=1)[((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/n(n>1)例:括号内是说明输入3(repeat=3)00.9(n=0,x=0.9)1-9.8(n=1,x=-9.8)101.7(n=10,x=1.7)输出p(0,0.90)=1.00p(1,-9.80)=-9.80p(10,1.70)=3.05#include2、o.h>doublep(intn,doublex);intmain(void){intrepeat,ri;intn;doublex,result;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d%lf",&n,&x);result=p(n,x);printf("p(%d,%.2lf)=%.2lf",n,x,result);}}doublep(intn,doublex){doublet;if(n==0)t=1;elseif(n==1)t=x;elset=((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/
3、n;returnt;}10016十进制转换二进制输入一个正整数repeat(04、r(ri=1;ri<=repeat;ri++){scanf("%d",&n);dectobin(n);printf("");}}voiddectobin(intn){inta[32],i;if((n==0)
5、
6、(n==1)){printf("%d",n);return;}else{i=0;while(n!=0){a[i++]=n%2;n=n/2;}}while(i>0)printf("%d",a[--i]);}#include"stdio.h"intmain(void){intri,repeat;inti,n;voiddectobin(intn);scanf("%d",&repe
7、at);for(ri=1;ri<=repeat;ri++){scanf("%d",&n);dectobin(n);printf("");}}voiddectobin(intn){if(n==1
8、
9、n==0){printf("%d",n);return;}dectobin(n/2);printf("%d",n%2);}10017递归函数程序设计求Fabonacci数列输入一个正整数repeat(010、+f(n-1)(n>=2)其中f(0)=0,f(1)=1。例:(括号内为说明)输入3(repeat=3)016输出fib(0)=0fib(1)=1fib(6)=8#includelongfib(intn);intmain(void){intn,ri,repeat;scanf("%d",&repeat);for(ri=1;ri<=repeat;ri++){scanf("%d",&n);printf("fib(%d)=%ld",n,fib(n));}}longfib(intn){intt;if(n==0)t=0;elseif(n==1)t=1;elset=fib(n
11、-2)+fib(n-1);returnt;}10019改错题error10_1.cpp用递归函数计算x^n(n>=1)的值。例:(括号内为说明)输入23(x=2,n=3)输出Root=8.00#includedoublefun(intn,doublex);intmain(void){intn;doublex,root;scanf("%lf%d",&x,&n);root=fun(n,x);printf("Root=%0.2f",root