how can i check if n2 is a sub number of n1? (HW qst.)

조회 수: 2 (최근 30일)
Zaza
Zaza 2013년 1월 4일
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
Azzi Abdelmalek 2013년 1월 4일
The highest power of 10 correspond to 1 not 5
Azzi Abdelmalek
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
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().
  댓글 수: 1
Zaza
Zaza 2013년 1월 6일
편집: Zaza 2013년 1월 6일
op() == num_of_digits() ???

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
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
Zaza
Zaza 2013년 1월 4일
can i use floor instead of fix?
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 4일
Yes you can

댓글을 달려면 로그인하십시오.


Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by