相关疑难解决方法(0)

在C++ 11中创建N元素constexpr数组

您好我正在学习C++ 11,我想知道如何制作一个constexpr 0到n数组,例如:

n = 5;

int array[] = {0 ... n};
Run Code Online (Sandbox Code Playgroud)

所以阵列可能是 {0, 1, 2, 3, 4, 5}

c++ array-initialization constexpr c++11

56
推荐指数
4
解决办法
4万
查看次数

C中的帕斯卡三角形

我是一名计算机工程专业的学生,​​下学期我将开始 C 课程。因此,为了让自己做好准备,我开始自学 C 并偶然发现了一项有趣的任务,该任务旨在让我乍一看似乎不是一个非常高级的水平。

任务是编写一个程序来计算Pascal's Triangle 中给定位置的值。计算它的公式写为element = row! /(位置!*(行 - 位置)!)

我已经编写了一个简单的控制台程序,它似乎可以正常工作,直到我开始使用大量数字对其进行测试。

当用第 16 行和位置 3 尝试这个程序时,它计算出的值为 0,虽然很明显不可能有这样的值(实际上它应该计算值为 560),但这个三角形的所有单元格都应该是整数并且大于一。

我想我在存储和处理大数时遇到了问题。阶乘函数似乎可以正常工作,并且我使用的公式可以正常工作,直到我开始尝试大数字

到目前为止,这里找到了最好的解决方案 -如何打印一个 unsigned long long int(unsigned long long int 的格式说明符)? 使用类型为 uint64_t 的 inttypes.h 库,但它仍然没有给我我需要的结果。

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

void clear_input(void);
uint64_t factorial(int x);

int main()
{
    // Printing
    printf("This program computes the value of a given position in Pascal's Triangle.\n");
    printf("You will be asked for row and …
Run Code Online (Sandbox Code Playgroud)

c largenumber pascals-triangle

3
推荐指数
1
解决办法
1776
查看次数