c - 如何在C中的整数数组中添加二次序列
问题描述
我正在尝试在整数数组(在 C 中)中添加一个二次序列,而不是手动输入它。
#include <stdio.h>
int main ()
{
int x [100] = {0, 50, 150, 300, 500, 750, 1050, 1400};
//+0, +50, +100, +150, +200, +250, +300, +350, etc.
return 0;
}
有没有办法做到这一点?
解决方案
num_add 在每个循环中添加 50。
#include <stdio.h>
int main() {
int x[100];
int num_add = 50;
x[0] = 0;
for (int i = 1; i < 100; i++) {
x[i] = x[i - 1] + num_add;
num_add += 50;
}
for (int i = 0; i < 100; i++) {
printf("%d ", x[i]);
}
return 0;
}
推荐阅读
- python - 如何使用 Python 在 Selenium Webdriver 中选择一个具有常见类名的元素?
- google-url-shortener - Google URL Shortner cURL 返回错误
- node.js - 如何在 VSCode 终端中运行 NPM 命令?
- c - 用递归方法遍历链表
- django - Django celery 任务信息保存在哪里?
- javascript - React 组件 DRY 重构
- php - 选择用户详细信息时的 MySQL 更新活动
- ios - DJI iOS SDK (Swift) - iPhone 通过 USB 连接 Mavic 遥控器时,App 无法连接飞行器
- c++ - 使用 vtkImageReslice 重新切片 3d 原始图像
- python - 将 2 个地图与 matplotlib 进行比较