solution.md 1.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8
# 用递归求第n项的值

1,2,4,2,3,6,12,6,3,....求第n项值

## template

```cpp
#include <stdio.h>
9
int fun(int n, int *x, int *y)
每日一练社区's avatar
每日一练社区 已提交
10
{
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    int sum = 0, i;
    int size = 0;
    int dd = 1;
    for (i = 1; i <= (*x); i++)
    {
        sum += (2 * i - 1);
    }
    if (sum == n)
    {
        *y = 2 * (*x) - 1;
        return (*x);
    }
    else if (sum > n)
    {
        (*y) = n - (sum - (2 * (*x) - 1));
        size = 2 * (*x) - 1;
        dd = (*x);
        for (i = 2; i <= (*y); i++)
        {
            if (i <= (*x))
                dd *= 2;
            else
                dd /= 2;
        }
        return dd;
    }
    else
    {
        (*x)++;
        return fun(n, x, y);
    }
每日一练社区's avatar
每日一练社区 已提交
42 43 44
}
int main()
{
45 46 47 48 49 50 51 52 53 54
    int n;
    int row = 1, col = 0;
    int val;
    row = 1;
    col = 0;
    printf("请输入n:");
    scanf("%d", &n);
    val = fun(n, &row, &col);
    printf("第%d项是:%d\n", n, val);
    return 0;
每日一练社区's avatar
每日一练社区 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}
```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```