[Question]
  [
Write a function which takes in a list of positive integers and returns a list of integers approximating the percent of total for the corresponding integer in the same position.
All integers in the return list must exactly add up to 100. You can assume the sum of integers passed in is greater than 0. How you want to round or truncate decimals is up to you as long as any single resulting integer returned as a percentage is off by no more than 1 in either direction.
```
p([1,0,2])      ->  [33,0,67] or [34,0,66]
p([1000,1000])  ->  [50,50] or [49,51] or [51,49]
p([1,1,2,4])    ->  [12,12,25,51] or [13,12,25,50] or [12,13,25,50] or [13,13,25,49] or [13,12,26,49] or [12,13,26,49] or [12,12,26,50]
p([0,0,0,5,0])  ->  [0,0,0,100,0]
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code in bytes wins!
  
[Answer]
# Dyalog APL, ~~21~~ ~~19~~ 16 bytes
```
+\⍣¯1∘⌊100×+\÷+/
```
The above is a train equivalent of 
```
{+\⍣¯1⌊100×+\⍵÷+/⍵}
```
[Try it online.](http://tryapl.org/?a=p%u2190+%5C%u2363%AF1%u2218%u230A100%D7+%5C%F7+/%20%u22C4%20p%201%201%202%204&run)
### How it works
```
                 ⍝ Sample input: 1 1 2 4
           +\    ⍝ Cumulative sum of input. (1 2 4 8)
              +/ ⍝ Sum of input. (8)
             ÷   ⍝ Divide the first result by the second. (0.125 0.25 0.5 1)
       100×      ⍝ Multiply each quotient by 100. (12.5 25 50 100)
      ⌊          ⍝ Round the products down to the nearest integer... (12 25 50 100)
     ∘           ⍝ and ...
  ⍣¯1            ⍝ apply the inverse of...
+\               ⍝ the cumulative sum. (12 13 25 50)
```
[Answer]
# TI-BASIC, ~~26~~ ~~23~~ 16 bytes
For TI-83+/84+ series calculators.
```
ΔList(augment({0},int(cumSum(ᴇ2Ans/sum(Ans
```
Thanks to @Dennis for a beautiful algorithm! We take the cumulative sum of the list after converting to percents, then floor, tack a 0 onto the front, and take differences. `ᴇ2` is one byte shorter than `100`.
At the same byte count is:
```
ΔList(augment({0},int(cumSum(Ans/sum(Ans%
```
Fun fact: `%` is a [two-byte token](http://tibasicdev.wikidot.com/percent) that multiplies a number by .01—but there's no way to type it into the calculator! You need to either edit the source outside or use an assembly program.
Old code:
```
int(ᴇ2Ans/sum(Ans
Ans+(ᴇ2-sum(Ans)≥cumSum(1 or Ans
```
The first line calculates all floored percents, then the second line adds 1 to the first `N` elements, where `N` is the percentage left over. `cumSum(` stands for "cumulative sum".
Example with `{1,1,2,4}`:
```
          sum(Ans                  ; 8
int(ᴇ2Ans/                         ; {12,12,25,50}
                        1 or Ans   ; {1,1,1,1}
                 cumSum(           ; {1,2,3,4}
     ᴇ2-sum(Ans)                   ; 1
                ≥                  ; {1,0,0,0}
Ans+                               ; {13,12,25,50}
```
We won't have `N>dim([list]`, because no percentage is decreased by more than 1 in flooring.
[Answer]
# CJam, ~~25~~ ~~23~~ 22 bytes
```
{_:e2\:+f/_:+100-Xb.+}
```
*Thanks to @Sp3000 for 25 → 24.*
[Try it online.](http://cjam.aditsu.net/#code=q~%20e%23%20Read%20input.%20Evaluate.%0A%0A%7B_%3Ae2%5C%3A%2Bf%2F_%3A%2B100-Xb.%2B%7D%0A%0A~p%20e%23%20Execute%20function.%20Print.&input=%5B1%201%202%204%5D)
### How it works
```
_                   e# Push a copy of the input.
 :e2                e# Apply e2 to each integer, i.e., multiply by 100.
    \               e# Swap the result with the original.
     :+             e# Add all integers from input.
       f/           e# Divide the product by the sum. (integer division)
        _:+         e# Push the sum of copy.
           100-     e# Subtract 100. Let's call the result d.
               Xb   e# Convert to base 1, i.e., push an array of |d| 1's.
                 .+ e# Vectorized sum; increment the first |d| integers.
```
[Answer]
### [J (8.04 beta)](http://jsoftware.com/), 59 bytes (30 stolen bytes)
30 byte literal J-port of [Dennis's APL answer](https://codegolf.stackexchange.com/a/60062/571):
```
    f=.3 :'+/\^:_1<.100*(+/\%+/)y'
    f 1 1 2 4
12 13 25 50
```
59 bytes answer, best I could do myself:
```
f=.3 :0
p=.<.100*y%+/y
r=.100-+/p
p+((r$1),(#p-r)$0)/:\:p
)
```
(Based on the remainder having to go to the highest values, no more than +1 each, split over multiple values in the case of a remainder > 1 or a tie for highest value).
e.g.
```
   f 1 0 2
33 0 67
   f 1000 1000
50 50
   f 1 1 2 4
12 12 25 51
   f 0 0 0 5 0
0 0 0 100 0
   f 16 16 16 16 16 16
17 17 17 17 16 16
   f 0 100 5 0 7 1
0 89 4 0 7 0
```
Explanation
* `f=.3 : 0` - 'f' is a variable, which is a verb type (3), defined below (:0):
* `p=.` variable 'p', built from:
	+ `y` is a list of numbers `1 0 2`
	+ `+/y` is '+' put between each value '/', the sum of the list `3`
	+ `y % (+/y)` is original y values divided by the sum: `0.333333 0 0.666667`
	+ `100 * (y%+/y)` is 100x those values: `33.33.. 0 0.66...` to get the percentages.
	+ `<. (100*y%+/y)` is the floor operator applied to the percentages: `33 0 66`
* `r=.` variable 'r', built from:
	+ `+/p` is the sum of the floored percentages: `99`
	+ `100 - (+/p)` is 100 - the sum, or the remaining percentage points needed to make the percentages sum to 100.
* result, not stored:
	+ `r $ 1` is a list of 1s, as long as the number of items we need to increment: `1 [1 1 ..]`
	+ `#p` is the length of the percentage list
	+ `(#p - r)` is the count of items that won't be incremented
	+ `(#p-r) $ 0` is a list of 0s as long as that count: `0 0 [0 ..]`
	+ `((r$1) , (#p-r)$0)` is the 1s list followed by the 0s list: `1 0 0`
	+ `\: p` is a list of indexes to take from `p` to put it in descending order.
	+ `/: (\:p)` is a list of indexes to take from `\:p` to put that in ascending order
	+ `((r$1),(#p-r)$0)/:\:p` is taking the elements from the 1 1 .. 0 0 .. mask list and sorting so there are 1s in the positions of the biggest percentages, one for each number we need to increment, and 0s for other numbers: `0 0 1`.
	+ `p + ((r$1),(#p-r)$0)/:\:p` is the percentages + the mask, to make the result list which sums to 100%, which is the function return value.
e.g.
```
33 0 66 sums to 99
100 - 99 = 1
1x1 , (3-1)x0 = 1, 0 0
sorted mask   = 0 0 1
33 0 66
 0 0  1
-------
33 0 67
```
and
* `)` end of definition.
I'm not very experienced with J; I wouldn't be too surprised if there's a "turn list into percentages of the total" operation built in, and a cleaner way to "increment *n* biggest values" too. (This is 11 bytes less than my first attempt).
[Answer]
# Mathematica, 41 bytes
```
(s=Floor[100#/Tr@#];s[[;;100-Tr@s]]++;s)&
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
-2 thanks to Dennis, reminding me to use another new feature (`Ä`) and using `:` instead of what I initially had.
```
ŻÄ×³:SI
```
[Try it online!](https://tio.run/##y0rNyan8///o7sMth6cf2mwV7Pk/2lDHUMdIxyT2cPt/AA "Jelly – Try It Online")
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
```
0;+\÷S×ȷ2ḞI
```
[Try it online!](https://tio.run/##y0rNyan8/9/AWjvm8Pbgw9NPbDd6uGOe5////6MNdQx1jHRMYgE "Jelly – Try It Online")
Done alongside [caird coinheringaahing](https://codegolf.stackexchange.com/users/66833/caird-coinheringaahing) and [user202729](https://codegolf.stackexchange.com/users/69850/user202729) in [chat](https://chat.stackexchange.com/rooms/57815/jelly-hypertraining).
## How it works
```
0;+\÷S×ȷ2ḞI - Full program.
0;          - Prepend a 0.
  +\        - Cumulative sum.
    ÷S      - Divided by the sum of the input.
      ×ȷ2   - Times 100. Replaced by ×³ in the monadic link version.
         ḞI - Floor each, compute the increments (deltas, differences).
```
[Answer]
# JavaScript (ES6), 81 bytes
```
a=>(e=0,a.map(c=>((e+=(f=c/a.reduce((c,d)=>c+d)*100)%1),f+(e>.999?(e--,1):0)|0)))
```
That "must equal 100" condition (rather than rounding and adding up) nearly doubled my code (from 44 to 81). The trick was to add an pot for decimal values that, once it reaches 1, takes 1 from itself and adds it to the current number. Problem then was floating points, which means something like [1,1,1] leaves a remainder of .9999999999999858. So I changed the check to be greater than .999, and decided to call that precise enough.
[Answer]
# Haskell, ~~42~~ 27 bytes
```
p a=[div(100*x)$sum a|x<-a]
```
Pretty much the trivial method in Haskell, with a few spaces removed for golfing.
Console (brackets included to be consistent with example):
```
*Main> p([1,0,2])
[33,0,66]
*Main> p([1000,1000])
[50,50]
*Main> p([1,1,2,4])
[12,12,25,50]
*Main> p([0,0,0,5,0])
[0,0,0,100,0]
```
Edit: practised my putting, made some obvious replacements.
Original:
```
p xs=[div(x*100)tot|x<-xs]where tot=sum xs
```
[Answer]
### Haskell, ~~63~~ ~~56~~ 55 bytes
```
p l=tail>>=zipWith(-)$[100*x`div`sum l|x<-0:scanl1(+)l]
```
[Answer]
# Perl, 42 bytes
Based on Dennis's algorithm
Includes +1 for `-p`
Run with the list of numbers on STDIN, e.g.
```
perl -p percent.pl <<< "1 0 2"
```
`percent.pl`:
```
s%\d+%-$-+($-=$a+=$&*100/eval y/ /+/r)%eg
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 150 bytes
```
((([]){[{}]({}<>)<>([])}{})[()])<>([]){{}<>([{}()]({}<([()])>)<>(((((({})({})({})){}){}){}{}){}){}(<()>))<>{(({}<({}())>)){({}[()])<>}{}}{}([])}<>{}{}
```
[Try it online!](https://tio.run/##NY0xDoAwCEWv4vgZXHRtepGmQx1MjMbBlfTs@CFtgUA/Dzi@dr3r@bTbDECpokV7hfaUJWUXunYpkDq@6i0QouQYohdwPNIzhBE@MxJIElXEqC9xQVmNE2TpcZcYS7Ntmbb/ "Brain-Flak – Try It Online")
Starting from the end and working backward, this code ensures at each step that the sum of the output numbers so far is equal to the total percentage encountered, rounded down.
```
(
  # Compute and push sum of numbers
  (([]){[{}]({}<>)<>([])}{})
# And push sum-1 above it (simulating a zero result from the mod function)
[()])
<>
# While elements remain
([]){{}
  # Finish computation of modulo from previous step
  <>([{}()]({}
    # Push -1 below sum (initial value of quotient in divmod)
    <([()])>
  # Add to 100*current number, and push zero below it
  )<>(((((({})({})({})){}){}){}{}){}){}(<()>))
  # Compute divmod
  <>{(({}<({}())>)){({}[()])<>}{}}{}
([])}
# Move to result stack and remove values left over from mod
<>{}{}
```
[Answer]
# JavaScript (ES6) 60 ~~63 95~~
Adapted and simplified from [my (wrong) answer](https://codegolf.stackexchange.com/a/45717/21348) to another challenge  
Thk to @l4m2 for discovering that this was wrong too
Fixed saving 1 byte (and 2 byte less, not counting the name `F=`)
```
v=>v.map(x=>(x=r+x*100,r=x%f,x/f|0),f=eval(v.join`+`),r=f/2)
```
Test running the snippet below in any EcmaScript 6 compliant browser
```
F=
v=>v.map(x=>(x=r+x*100,r=x%f,x/f|0),f=eval(v.join`+`),r=f/2)
console.log('[1,0,2] (exp [33,0,67] [34,0,66])-> '+F([1,0,2]))
console.log('[1000,1000] (exp [50,50])-> '+F([1000,1000]))
console.log('[1,1,2,4] (exp[12,12,25,51] [13,12,25,50] [12,13,25,50] [12,12,26,50])-> '+F([1,1,2,4]))
console.log('[0,0,0,5,0] (exp [0,0,0,100,0])-> '+F([0,0,0,5,0]))
console.log('[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,980] -> '+F([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,980]))
console.log('[2,2,2,2,2,3] -> ' + F([2,2,2,2,2,3]))
```
```
<pre id=O></pre>
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 14 bytes
```
Ẋ-Θmȯ⌊*100/Σ¹∫
```
[Try it online!](https://tio.run/##yygtzv7//@GuLt1zM3JPrH/U06VlaGCgf27xoZ2POlb///8/2lAHCGMB "Husk – Try It Online") or [Verify all test cases](https://tio.run/##yygtzv7/P/dRUyPXw11duudm5J5Y/6inS8vQwED/3OJDOx91rP7//390tKGOgY5RrA6QBkIQbWBgoAMioGJGOiZAloEOCJrqGMTGAgA "Husk – Try It Online")
Algorithm from Dennis' answer.
## Explanation
```
Ẋ-Θmȯ⌊*100/Σ¹∫
             ∫ cumulative sums
   mȯ          map to the follwing 4 functions
          /Σ¹  divide by sum of input
      *100     times 100
     ⌊         floored
  Θ            prepend a zero
Ẋ-             fold consecutive pairs with subtraction
```
[Answer]
# Octave, 40 bytes
```
@(x)diff(ceil([0,cumsum(100*x/sum(x))]))
```
[Answer]
### Python 2, 89 bytes
```
def f(L):
 p=[int(x/0.01/sum(L))for x in L]
 for i in range(100-sum(p)):p[i]+=1
 return p
print f([16,16,16,16,16,16])
print f([1,0,2])
->
[17, 17, 17, 17, 16, 16]
[34, 0, 66]
```
[Answer]
# JavaScript, 48 bytes
```
F=
x=>x.map(t=>s+=t,y=s=0).map(t=>-y+(y=100*t/s|0))
// Test 
console.log=x=>O.innerHTML+=x+'\n';
console.log('[1,0,2] (exp [33,0,67] [34,0,66])-> '+F([1,0,2]))
console.log('[1000,1000] (exp [50,50])-> '+F([1000,1000]))
console.log('[1,1,2,4] (exp[12,12,25,51] [13,12,25,50] [12,13,25,50] [12,12,26,50])-> '+F([1,1,2,4]))
console.log('[0,0,0,5,0] (exp [0,0,0,100,0])-> '+F([0,0,0,5,0]))
```
```
<pre id=O></pre>
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
.¥IO/т*ï¥
```
[Try it online!](https://tio.run/##yy9OTMpM/f9f79BST3/9i01ah9cfWvr/f7ShDhDGAgA "05AB1E – Try It Online")
*+3 for a bugfix*
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), 12 bytes
```
∑/₁*ṙ:∑₁$-Þ…
```
[Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=%E2%88%91%2F%E2%82%81*%E1%B9%99%3A%E2%88%91%E2%82%81%24-%C3%9E%E2%80%A6&inputs=%5B1%2C%201%2C%201%5D&header=&footer=)
## Explained
```
∑/₁*ṙ:∑₁$-Þ…
∑             # sum(input)
 /            # input ÷ ↑         # get the weight of each number
  ₁*          # ↑ * 100           # and make it a percentage
    ṙ         # map(round, ↑)     # rounding each number
     :∑       # ↑, sum(↑)         # now sum the above
       ₁$-    # 100 - ↑           # and subtract from 100. The result represents any potential round errors that need correction
          Þ…  # distribute(↑, ↑↑) # spread ↑ over the rounded list evenly
```
[Answer]
# Rust, 85 bytes
This uses vectors instead of arrays because as far as I am aware there is no way to accept arrays of multiple different lengths.
```
let a=|c:Vec<_>|c.iter().map(|m|m*100/c.iter().fold(0,|a,x|a+x)).collect::<Vec<_>>();
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) v2.0a0, 13 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
å+ mzUx÷L)änT
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=2.0a0&code=5SsgbXpVePdMKeRuVA&input=WzEsMSwxXQ)
[Answer]
# [Jq 1.5](https://stedolan.github.io/jq/), 46 bytes
```
add as$s|.[1:]|map(100*./$s|floor)|[100-add]+.
```
Expanded
```
  add as $s               # compute sum of array elements
| .[1:]                   # \ compute percentage for all elements 
| map(100*./$s|floor)     # / after the first element
| [100-add] + .           # compute first element as remaining percentage
```
[Try it online!](https://tio.run/##XY7LCsIwEEX3fkVAFz4mNWmaCi78kTCLYBEqtal94Cbfbpy0VYqTYeCeudzM/RmMkSAgRTbVml8YM0oRy0/IXEsiiyLHFRgphIA48GfVArQYdyAhhQwXMTIF6lSDllOUVF8gZkAO9Q/Ikc@hAuLTsPhwQnQEwRUa9MEWBbPdpvOJkWf0D9tsab1PjoRulXPtzsfLOfnwkITwdk1furoLnNdDVfGyboaeRGtf3A39KK4f "jq – Try It Online")
[Answer]
# PHP, 82 bytes
```
for(;++$i<$argc-1;$s+=$x)echo$x=$argv[$i]/array_sum($argv)*100+.5|0,_;echo 100-$s;
```
takes input from command line arguments, prints percentages delimited by underscore.
Run with `-nr` or [try it online](http://sandbox.onlinephpfunctions.com/code/a5f5644f5fb6f90b9e9586865e626b1883a6fc8e).
]