栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

棋和麦粒问题(C语言回调函数实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

棋和麦粒问题(C语言回调函数实现)

#include

#define N 63

long long store(int n) {
    if (n == 0)
        return 1;
    return n = 2 * store(n - 1);

//每个格子存放的麦粒数为(2^n)个,每次回调函数进行计算。
}

long long summation(int m) {
    if (m == 0)
        return store(0);
    return store(m - 1) + summation(m - 1);

//当前格子存放的麦粒数为前面(m-1)个格子存放的麦粒数之和加上当前格子的(2^i)个麦粒。
}

//如果不使用long long数据类型的函数,会在计算到后面的数字时,出现数据存储越界,导致数据为0;

//回调函数在使用时一定设置临界条件退出回调,不然会导致程序出现死循环。

int main() {


    int i;
    int j ;
    printf("Please enter what the ith cell is:n");
    scanf("%d", &i);
    printf("The designated i grid that you enter is %d:n",i);
    store(i);
    for (j = 0; j < i-1; j++) {
        printf("The number of grains stored on the %d board is %lldn", j+1, store(j));
    }
    summation(i);
    printf("So the number of grains stored in the i(%d) cell is %lldn", i, summation(i)-1);
    return 0;


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008967.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号