fork(5) download
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. inline int minimum (int a, int b) { return a < b ? a : b; }
  5. int fact[100]={0};
  6. void getFactors(int n){
  7. int i,j=0;
  8. for (i=2;i<n;i++){
  9. if(n%i==0) {
  10. fact[j]=i;
  11. j++;
  12. }
  13. }//for
  14. printf("factors");
  15. i=0;
  16. while(fact[i]>0){
  17. printf(" %d ",fact[i]);
  18. i++;
  19. }
  20. }//getFactor
  21. int getMinTiles( int width , int height, int nTiles)
  22. {
  23. int w=width;
  24. int h=height;
  25. int n=nTiles;
  26. int i,j,l,fact1,fact2;
  27. int min=10; //min any number greater thn 2
  28.  
  29.  
  30. if( w*h <= n ) return -1;
  31. // w*h total number of tiles are more or equal to required, return -1
  32.  
  33. getFactors(n);
  34. //store all factors of a nTiles except 1 and nTiles.
  35. i=l=0;
  36. while(fact[i]>0){
  37. i++;
  38.  
  39. }//while
  40. l=i;
  41. printf("\nLength - %d ",l);
  42. //l = length(fact);
  43.  
  44. //for each pair of factors
  45. for (i=0,j=l ; i <=j ; i++,j--)
  46. {
  47. fact1=fact[i];
  48. fact2=fact[j];
  49.  
  50. if ( fact1 == w && fact2 < h ) {min = 1;}
  51. else if ( fact2 == h && fact1 < w ) {min = 1;}
  52.  
  53. // eg : fact1=4,fact2=3 ; h=4 ,w=5 , n=12;
  54. // When row or col number are matching with width or column , with only one cut we can produce desired result.
  55. // if another fact is smaller than column or width (opposite)
  56.  
  57. else if ( (fact1 < w && fact1 < h) || (fact2 < w && fact2 < h))
  58. { min = minimum (min, 2); }
  59. // both factors are very small then height and width then 2 cut are required one horizontally and one vertically)
  60.  
  61.  
  62. else { min = 0;}
  63.  
  64. }//for
  65.  
  66. return min;
  67.  
  68. }//Function
  69.  
  70. int main() {
  71. int cut;
  72. cut = getMinTiles( 5, 4, 18);
  73. printf("\nNumber of cut required %d ",cut);
  74. //cout<<cut;
  75. return 0;
  76. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
factors 2  3  6  9 
Length - 4 
Number of cut required 0