Please help with insights

Problem: Assuming you have n=[16,18,23,24,39,40] values, how would you create a funtionality in Matlab to output the number of values needed to make a sum of 100, every time you call out a random value amongst them.
To eplain further: Assume it's a dart board, and the question is, how many darts do I need to throw and hit any number and after a number of throws the hit numbers add up to a 100?
I'm new to Matlab but I would solve the same with C/C++. Any insights are highly appreciated.

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-4-19
编辑:Azzi Abdelmalek 2013-4-19
n=[16,18,23,24,39,40]
find(cumsum(n)>=100,1)
%or
a=0;
k=1
while a<100 & k<numel(n)
a=a+n(k);
k=k+1;
end
out=k-1

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

提问:

2013-4-19

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by