回復 volume2730
以下都是 pseudo code
不過幾乎都寫出來了,可以參考一下,
- 2.(a)
- #include<math.h>
- for(double i=2; i<100; i+=3)
- sum+=pow(i,(1/3));
- 2.(b)
- for(int i=0~1000)
- if(i%3==0 || i%5 ==0)
- sum+=i;
複製代碼- 3.
- double Fac(int n)
- {
- double ret=1;
- for(; n>=1; n--)
- ret *= n;
- return ret;
- }
- (a)
- double FN(int n)
- {
- for(int i=1~n)
- ans+= 1.0/Fac(i);
- }
- (b)
- double FN(int n)
- {
- for(int i=1~n)
- {
- sum+= i;
- ans+=sum/Fac(i);
- }
- }
- (c)
- double FN(int n,int x)
- {
- for(int i=1~n)
- ans+=pow(x,i)/Fac(i);
- }
複製代碼- 4.
- // LCM
- int lcm(int a,int b)
- {
- int n;
- for(n=1;;n++)
- {
- if(n%a == 0 && n%b == 0)
- return n;
- }
- }
- // GCD
- int gcd(int a,int b)
- {
- int c;
- while(1)
- {
- c = a%b;
- if(c==0)
- return b;
- a = b;
- b = c;
- }
- }
複製代碼- 5. Hanoi Tower
- void hanoi(int x, char from,char to,char aux)
- {
- if(x==1)
- p rintf("Move Disk From %c to %c\n",from,to);
- else
- {
- hanoi(x-1,from,aux,to);
- p rintf("Move Disk From %c to %c\n",from,to);
- hanoi(x-1,aux,to,from);
- }
- }
- void main()
- {
- int disk;
- int moves;
- clrscr();
- p rintf("Enter the number of disks you want to play with:");
- scanf("%d",&disk);
- moves=pow(2,disk)-1;
- p rintf("\nThe No of moves required is=%d \n",moves);
- hanoi(disk,'A','C','B');
- getch();
- }
複製代碼- 6. 計算 fin_r, fin_l 時間
- time_t start, finish;
- time(&start);
- /* call fin_r, fin_l function */
- time(&finish);
- double elapsed_tm=difftime(finish,start);
複製代碼- 7.
- 個位數與十位數交換
- 11 x 11 <=> 11 x 11 = 121
- 12 x 12 <=> 21 x 21 = 441
- 13 x 13 <=> 31 x 31 = 961
- 14 x 14 <=> 41 x 41 > 999
複製代碼 ... |