2017-04-02 74 views
4

假设我们已经为因式分解下面的脚本格式因式分解算法的

z=input('enter your number : '); 
    for ii=2:z 
     s=0; 
     while z/ii==floor(z/ii) % check if z is divisible by ii 
      z=z/ii; 
      s=s+1; 
     end 
     if s>0 
       str = [num2str(ii) '^' num2str(s) ]; 
      disp(str) ;  
      % If z = 1, no more divisions are necessary, 
      % thus breaks the loop and quits 
      if z == 1 
       break 
      end 
     end 

    end 

但是这个代码的输出未格式化好,例如

>> integer_factorization 
    enter your number : 30 
    2^1 
    3^1 
    5^1 

我怎么可以这样做,我得到了

30=2^1*3^1*5^1? 

在此先感谢

+0

请检查我的答案,它完美的工作。 – ABcDexter

回答

2

这是相当直接的:使用fprintf(''); instead打印/显示的因素。


z=input('enter your number : '); 

ans='' %the final answer string 

for ii=2:z 
    s=0; 
    while z/ii==floor(z/ii) % check if z is divisible by ii 
     z=z/ii; 
     s=s+1; 
    end 
    if s>0 
      str = [num2str(ii) '^' num2str(s) ]; 
     %disp(str) ; 

     strcat(ans,'*',str); %concats the * str as per requirement. 

     % If z = 1, no more divisions are necessary, 
     % thus breaks the loop and quits 
     if z == 1 
      break 
     end 
    end 
end 

ans=ans(2:end); % to remove the first * 
fprintf(ans); % can even use the disp() function. 

因此,基本上,增加了一个字符串的因素追加到它,并显示在端部,所述环的外部。

+1

我已删除最后一个:D –

+1

@datodatuashvili感谢您提出这个问题,一年后得到练习Matlab:D – ABcDexter

+1

我发布了我的答案根据您的答案,我练习matlab和C++ –

1

您可以简单地创建一个字符串并将您的数字添加到字符串并最终打印字符串。如下:

z=input('enter your number : '); 
for ii=2:z 
    s=0; 
    while z/ii==floor(z/ii) % check if z is divisible by ii 
     z=z/ii; 
     s=s+1; 
    end 
    if s>0 
     str = str + [num2str(ii) '^' num2str(s) '*' ];%only update 
     % If z = 1, no more divisions are necessary, 
     % thus breaks the loop and quits 
     if z == 1 
      break 
     end 
    end 
    str = str(0,str.size -1) %use proper command to remove the last * from your string result 
    disp(str) ; %display the str at the end in one line 
end 
+0

没有不删除的答案,这是错字,我要求不同的问题 –

+0

编辑答案,你应该只编辑我用来删除结果字符串(str)的最后一个字符的命令,因为它有一个额外的'*' –

1

Litle修改你的代码。

z=input('enter your number : '); 
for ii=2:z 
    s=0; 
    while z/ii==floor(z/ii) % check if z is divisible by ii 
     z=z/ii; 
     s=s+1; 
    end 
    if s>0 
      str += [num2str(ii) '^' num2str(s) ]; 

     % If z = 1, no more divisions are necessary, 
     % thus breaks the loop and quits 
     if z == 1 
      break 
     else 
      str+='*'; 
     end 
    end 

end 
disp(str); 
1

这可行!只是

z=input('enter your number : '); 
str=[num2str(z) '=']; 
first=0; 
for ii=2:z 
    s=0; 
    while z/ii==floor(z/ii) % check if z is divisible by ii 
     z=z/ii; 
     s=s+1; 
    end 
    if s>0 
     if first==0 
      str=[str num2str(ii) '^' num2str(s)]; 
      first=1; 
     else 
      str=[str '*' num2str(ii) '^' num2str(s)]; 
      if z == 1 
       break 
      end 
     end 
    end 
end 
1

首先感谢大家对你的买不起,这里是我的最终解决方案

z=input('enter your number : '); 
    string=''; 
    for ii=2:z 
     s=0; 
     while z/ii==floor(z/ii) % check if z is divisible by ii 
      z=z/ii; 
      s=s+1; 
     end 
     if s>0 
       str =[num2str(ii) '^' num2str(s) ]; 

        string=strcat(string,str); 
        string= strcat(string,'*'); 
      % If z = 1, no more divisions are necessary, 
      % thus breaks the loop and quits 
      if z == 1 
       break 
      end 
     end 

    end 
    string=string(1:end-1);% remove last sign of multiplicaiton 
fprintf('prime factorization is %s\n',string); 

这里一些改变你的代码是几个例子

>> integer_factorization 
enter your number : 30 
prime factorization is 2^1*3^1*5^1 

另一个

>> integer_factorization 
enter your number : 35 
prime factorization is 5^1*7^1 

和最后一个

>> integer_factorization 
enter your number : 100 
prime factorization is 2^2*5^2