利用while或者for循环计算n的阶乘 您所在的位置:网站首页 求n的阶乘编程c语言for 利用while或者for循环计算n的阶乘

利用while或者for循环计算n的阶乘

#利用while或者for循环计算n的阶乘| 来源: 网络整理| 查看: 265

写王道作业,尝试了几种写法,在这里记录一下 作业要求 方法1:用while循环 思路:从1乘到n

int main() { int n; scanf("%d", &n);//读取输入 int i = 1, total = 1; while (i int n; int i, total = 1; scanf("%d", &n); for (i = 1; i int n; scanf("%d", &n); int i,total = 1; i = n; while (i >= 1) { total = i * total; i--; } printf("%d", total); return 0; }

方法4:用for循环 思路:从n乘到1

int main() { int n; scanf("%d", &n); int i = 1, total = 1; for (i = n; i >= 1; i--) { total = i * total; } printf("%d", total); return 0; }

总结,两种思路:从1乘到n和从n乘到1,翁恺视频里面说过从大乘到小,不用考虑上限,所以暂且认为从n乘到1的思路好一点吧。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有