TJU 3474

Elite 2009 December Competition contes, Bronze division

题目比较简单,给一串数按照下面的方法求最后的值,是到模拟基础题

1     2     3     4     5     6  |  7     8     9     10     11
 
1     2     3  |  4     5     6
 
1     2  |  3
1     2        => 1*2=2 added to sum -> sum=2
3               => sent home with rose
 
4     5  |  6
4     5        => 4*5=20 added to sum -> sum=22
6               => sent home with rose
 
7     8     9  | 10    11
7     8  |  9
7     8        => 7*8=56 added to sum -> sum=78
9               => sent home with rose
10   11     => 10*11=110 added to sum -> sum=188
 
So the sum for this dance would be 188.
rookie的代码:
#include<stdio.h> int fact(int left,int right){ int sum=0; if(right-left==0) sum=0; else if(right-left==1) sum+=left*right; else{ if((right-left)%2) sum+=fact(left,(right+left-1)/2)+fact((right+left+1)/2,right); else sum+=fact(left,(right+left)/2)+fact((right+left)/2+1,right); } return sum; } int main(void) { int n; scanf("%d",&n); printf("%d/n",fact(1,n)); return 0; }

你可能感兴趣的:(TJU 3474)