[Basic C++] Increasing array size

[Basic C++] Increasing array size

  • 题目描述
  • 答题区域
  • 提交记录
  • 成绩报告
  • 排名情况
  • 标准答案
  • 相关讨论

出题

邮箱

郑斯达

zhengsd3@mail2.sysu.edu.cn

评测

截止时间

实时评测

2018-03-17 23:55

空间限制

时间限制

32MB

1000ms


Description

Increasing array size

Once an array is created, its size is fixed. Occasionally, you need to add more values to an array, but the array is full. In this case, you may create a new larger array to replace the existing array. Write a function with the following header:

int * doubleCapacity(int *list, int size)

The function create a new array that doubles the size of the parameter list, and copy the values in the list to the new array, the remaining value should to zero.

For example, the following codes outputs: 1 2 3 4 5 0 0 0 0 0

int list[5]={1,2,3,4,5};

int *newlist = doubleCapacity(list,5);

for(int i=0;i < 2*5;i++)

cout << *(newlist+i) << " ";

Hint

Don't submit the main() function.

标准答案:

 

  • 1001.cpp

1

<span style="color:#75715e">// Problem#: 17754</span>

2

<span style="color:#75715e">// Submission#: 4643990</span>

3

<span style="color:#75715e">// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</span>

4

<span style="color:#75715e">// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/</span>

5

<span style="color:#75715e">// All Copyright reserved by Informatic Lab of Sun Yat-sen University</span>

6

int * doubleCapacity(int *list, int size) { * doubleCapacity(int *list, int size) {

7

 int  *list1 = new int[size * 2];int  *list1 = new int[size * 2];

8

 for(int i = 0; i < 2 * size; i++) {for(int i = 0; i < 2 * size; i++) {

9

  if(i < size)if(i < size)

10

   *(list1+i) = *(list + i);*(list1+i) = *(list + i);

11

  elseelse

12

   *(list1+i) = 0;*(list1+i) = 0;

13

 }

14

 return list1;return list1;

15

}

16

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值