鱼C论坛

 找回密码
 立即注册
查看: 2732|回复: 2

[已解决]新手求助

[复制链接]
发表于 2021-10-2 16:08:49 | 显示全部楼层 |阅读模式
10鱼币
为什么就是算不出正确数值??

问题
从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?

Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。

Output
对于每个输入数据输出路径数,具体格式看Sample。

Sample Input
1
3
12
-1

Sample Output
1 1 2
2 3 10
3 12 416024
我的代码如下
  1. #include<stdio.h>

  2. int main()
  3. {
  4.         int count=1,i,j,num,sum,sum1,sum2,t;
  5.         while(scanf("%d",&num)!=EOF)
  6.         {
  7.                 count++;
  8.                 int sum1=1,sum2=1;
  9.                 for(i=num*2;i>num;i--)
  10.                 {
  11.                         sum1*=i;
  12.                 }
  13.                 for(j=num;j>0;j--)
  14.                 {
  15.                         sum2*=j;
  16.                 }
  17.                
  18.                 sum=sum1/sum2;
  19.                 printf("%d %d %d\n",count-1,num,sum);
  20.         }
  21.        
  22.         return 0;
  23. }
复制代码
最佳答案
2021-10-2 16:08:50
C++代码:
  1. #include <iostream>
  2. using namespace std;

  3. int func(int n){
  4.     if (n <= 1){return 1;}
  5.     int res = 0;
  6.     for (int i = 0; i < n; i++){
  7.         res += func(i) * func(n - 1 - i);
  8.     }
  9.     return res;
  10. }

  11. int f(int x){
  12.     return func(x) << 1;
  13. }

  14. int main (){
  15.     int i = 0;
  16.     while(true){
  17.         i++;
  18.         int n;
  19.         cin >> n;
  20.         if(n == -1){
  21.             break;
  22.         }
  23.         cout << i << ' ' << n << ' ' << f (n) << endl;
  24.     }
  25.     return 0;
  26. }
复制代码
C代码:
  1. #include <stdio.h>

  2. int func(int n){
  3.     if (n <= 1){return 1;}
  4.     int res = 0;
  5.     for (int i = 0; i < n; i++){
  6.         res += func(i) * func(n - 1 - i);
  7.     }
  8.     return res;
  9. }

  10. int f(int x){
  11.     return func(x) << 1;
  12. }

  13. int main(){
  14.     int i = 0;
  15.     while(1){
  16.         i++;
  17.         int n;
  18.         scanf("%d", &n);
  19.         if(n == -1){
  20.             break;
  21.         }
  22.         printf("%d %d %d\n", i, n, f(n));
  23.     }
  24.     return 0;
  25. }
复制代码
Python3代码:
  1. def func(n: int) -> int:
  2.     if n <= 1: return 1
  3.     res = 0
  4.     for i in range(n):
  5.         res += func(i)*func(n-1-i)
  6.     return res

  7. f = lambda x: func(x)<<1

  8. i = 0
  9. while True:
  10.     i += 1
  11.     n = int(input())
  12.     if n == -1:
  13.         break
  14.     print(f"{i} {n} {f(n)}")
复制代码
输出结果:
  1. 1
  2. 1 1 2
  3. 3
  4. 2 3 10
  5. 12
  6. 3 12 416024
  7. -1
复制代码

最佳答案

查看完整内容

C++代码:C代码:Python3代码:输出结果:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-2 16:08:50 | 显示全部楼层    本楼为最佳答案   
C++代码:
  1. #include <iostream>
  2. using namespace std;

  3. int func(int n){
  4.     if (n <= 1){return 1;}
  5.     int res = 0;
  6.     for (int i = 0; i < n; i++){
  7.         res += func(i) * func(n - 1 - i);
  8.     }
  9.     return res;
  10. }

  11. int f(int x){
  12.     return func(x) << 1;
  13. }

  14. int main (){
  15.     int i = 0;
  16.     while(true){
  17.         i++;
  18.         int n;
  19.         cin >> n;
  20.         if(n == -1){
  21.             break;
  22.         }
  23.         cout << i << ' ' << n << ' ' << f (n) << endl;
  24.     }
  25.     return 0;
  26. }
复制代码
C代码:
  1. #include <stdio.h>

  2. int func(int n){
  3.     if (n <= 1){return 1;}
  4.     int res = 0;
  5.     for (int i = 0; i < n; i++){
  6.         res += func(i) * func(n - 1 - i);
  7.     }
  8.     return res;
  9. }

  10. int f(int x){
  11.     return func(x) << 1;
  12. }

  13. int main(){
  14.     int i = 0;
  15.     while(1){
  16.         i++;
  17.         int n;
  18.         scanf("%d", &n);
  19.         if(n == -1){
  20.             break;
  21.         }
  22.         printf("%d %d %d\n", i, n, f(n));
  23.     }
  24.     return 0;
  25. }
复制代码
Python3代码:
  1. def func(n: int) -> int:
  2.     if n <= 1: return 1
  3.     res = 0
  4.     for i in range(n):
  5.         res += func(i)*func(n-1-i)
  6.     return res

  7. f = lambda x: func(x)<<1

  8. i = 0
  9. while True:
  10.     i += 1
  11.     n = int(input())
  12.     if n == -1:
  13.         break
  14.     print(f"{i} {n} {f(n)}")
复制代码
输出结果:
  1. 1
  2. 1 1 2
  3. 3
  4. 2 3 10
  5. 12
  6. 3 12 416024
  7. -1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-3 11:33:30 | 显示全部楼层
朋友,如果我的解答是你想要的请给最佳解答,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 22:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表