Breaking a Number into 1000s, 100, 50, 10, 5,1
1 回表示 (過去 30 日間)
古いコメントを表示
Ntandoyakhe Tshuma
2021 年 5 月 25 日
回答済み: Ntandoyakhe Tshuma
2021 年 5 月 25 日
Hello Everyone. I am trying to figure out a way to automatically break a given number into sums of 1000, 500, 100, 50, 10, 5 and 1. For example given 1994, I want to break it into 1000 + 500 + 100 + 100 + 100 + 100 +50+ 10 + 10 + 10 +10 + 4. Or given 75 it would be 50 + 10 + 10 + 5. I would appreciate any insight on how to do this. I am trying to use this to create a function that converts a given value into Roman numerals.
2 件のコメント
DGM
2021 年 5 月 25 日
There are a number of user-submitted tools on the File Exchange that can do conversion between various number systems. You could look at a few and see what approaches the various authors use.
採用された回答
その他の回答 (1 件)
Jan
2021 年 5 月 25 日
This sounds like a homework question. So please show us, what you have tried so far and ask a specific question concerning Matlab.
n = 1994;
pool = [1000, 500, 100, 50, 10, 5, 1];
v = zeros(size(pool));
for k = 1:numel(pool)
v(k) = ? % How many multiples of pool(k) are matching into n?
n = ? % Reduce n by number of found multiples
end
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!