fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int* plusOne(int* digits, int digitsSize, int* returnSize){
  6. int *arr = malloc((digitsSize+1)*sizeof(int));
  7. int k = digitsSize, c = 1;
  8. for (int i = digitsSize-1; i >= 0; i--) {
  9. if (digits[i]+c >= 10) {
  10. c = 1;
  11. arr[k--] = 0; }
  12. else {
  13. arr[k--] = digits[i]+c;
  14. c = 0; } }
  15. if (*returnSize = digitsSize, arr[1] == 0) {
  16. *returnSize += 1;
  17. arr[0] = 1;
  18. return arr; }
  19. return arr+1; }
  20.  
  21. int main(void) {
  22. int n, *x, i, p, *y;
  23. for (scanf("%d",&n), x = malloc(n*sizeof(int)), i = 0; i < n; ++i)
  24. scanf("%d",x+i);
  25. for (y = plusOne(x,n,&p), i = 0; i < p; ++i)
  26. printf("%d ",y[i]);
  27. return 0; }
  28.  
Success #stdin #stdout 0s 5516KB
stdin
3
1 2 3
stdout
1 2 4