how can i check if n2 is a sub number of n1? (HW qst.)
1 回表示 (過去 30 日間)
古いコメントを表示
for ex.:
n1 = 123; n2 = 12; (n1>=n2)
n2 is a sub number of n1
i can't make any use of strings or arrays
7 件のコメント
Azzi Abdelmalek
2013 年 1 月 4 日
編集済み: Azzi Abdelmalek
2013 年 1 月 4 日
check this
2345-fix(2345/10^3)*10^3
then
345-fix(345/10^2)*10^2
and so on
採用された回答
Sean de Wolski
2013 年 1 月 4 日
Here is a hint:
a = 131245;
b = 12;
op = @(x)hidden_for_you_to_figure_out(x);
pa = op(a);
pb = op(b);
isASub = false; %guilty until proven innocent
for ii = pb:pa
amii = mod(a,(10^ii));
if amii == b
isASub = true;
return;
else
for jj = 1:(ii-pb)
afjj = floor(amii./(10^jj));
if b == afjj
isASub = true;
return;
end
end
end
end
All you have to do it figure out the contents of op().
その他の回答 (2 件)
Azzi Abdelmalek
2013 年 1 月 4 日
編集済み: Azzi Abdelmalek
2013 年 1 月 4 日
n1=12456;
n2=245
a=num2str(n1);
b=num2str(n2);
c=all(ismember(b,a))
if c is equal to 1 that means n2 is a sub number of n1
6 件のコメント
Walter Roberson
2013 年 1 月 4 日
Create a function that calculates the decimal expansion of a number in reverse order. mod() 10 gives the next digit to be output, integer division by 10, if the result is non-zero keep going.
Apply that to both numbers.
Now match the reversed n2 to the reversed n1.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!