fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3. int dig1_max(int );
  4. void num_max(int *,int *);
  5. main()
  6. {
  7. int n,temp,*arr,a,b;
  8. cout<<"Enter number of non-negative numbers to be arranged : ";
  9. cin>>n;
  10. arr=new int[n];
  11. cout<<("\nEnter the numbers to be arranged : ")<<endl;
  12. for(int k=0;k<n;k++)
  13. cin>>arr[k];
  14. for(int i=0;i<n-1;i++)
  15. {
  16. for(int j=0;j<n-1;j++)
  17. {
  18. if(dig1_max(arr[j])<dig1_max(arr[j+1]))
  19. {
  20. temp=arr[j];
  21. arr[j]=arr[j+1];
  22. arr[j+1]=temp;
  23. }
  24. else if(dig1_max(arr[j])==dig1_max(arr[j+1]))
  25. {
  26. a=arr[j];
  27. b=arr[j+1];
  28. num_max(&a,&b);
  29. arr[j]=a;
  30. arr[j+1]=b;
  31. }
  32. else{}
  33. }}
  34. cout<<"The largest number formed combining entered numbers is ";
  35. for(int z=0;z<n;z++)
  36. cout<<arr[z];
  37. }
  38. int dig1_max(int x)
  39. {
  40. int n=0;
  41. while(x>0)
  42. {
  43. n=x%10;
  44. x=x/10;
  45. }
  46. return(n);
  47. }
  48. void num_max(int *m,int *n)
  49. {
  50. int s=1,k,c=0,num1,num2,p,q;
  51. p=*m;
  52. q=*n;
  53. k=q;
  54. for(;k>0;c++)
  55. {
  56. k=k/10;
  57. }
  58. for(;c>0;c--)
  59. {
  60. s=s*10;
  61. }
  62. num1=p*s+q;
  63. k=p;
  64. c=0;
  65. for(;k>0;c++)
  66. {
  67. k=k/10;
  68. }
  69. s=1;
  70. for(;c>0;c--)
  71. {
  72. s=s*10;
  73. }
  74. num2=q*s+p;
  75. if(num1>num2)
  76. {
  77. *m=p; *n=q;
  78. }
  79. else
  80. {
  81. *m=q; *n=p;
  82. }
  83. }
Success #stdin #stdout 0s 4400KB
stdin
3 3 327 94
stdout
Enter number of non-negative numbers to be arranged : 
Enter the numbers to be arranged : 
The largest number formed combining entered numbers is 943327