fork(3) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void Fns(int a[],int low,int high)
  5. {
  6. if(low>=high)return ;
  7. int pivot=low;
  8. int temp;
  9. int i=low,j=low+1;
  10. while(i<=j&&j<=high)
  11. {
  12. while(i+1<=high&&a[i+1]<a[pivot])
  13. i++;
  14. j=i+1;
  15. while(j<=high&&a[j]>=a[pivot])
  16. j++;
  17. if(j<=high)
  18. {
  19. temp=a[i+1];
  20. a[i+1]=a[j];
  21. a[j]=temp;
  22. j++;
  23. i++;
  24. }
  25. }
  26. temp=a[i];
  27. a[i]=a[pivot];
  28. a[pivot]=temp;
  29. Fns(a,pivot,i-1);
  30. Fns(a,i+1,high);
  31. }
  32. int TotalSum(int *a,int j,int k)
  33. { int tempi=-1;
  34. int i=0,count=0;
  35. while(i<=j)
  36. {
  37. if(a[i]+a[j]==k)
  38. {
  39. count++;
  40. i++;
  41. if(i&&a[i-1]==a[i]&&tempi==-1)
  42. tempi=i-1;
  43. else if(i+1<=j&&a[i+1]!=a[i]&&tempi!=-1)
  44. {
  45. i=tempi;
  46. j--;
  47. tempi=-1;
  48. }
  49. }
  50. else if(a[i]+a[j]>k)
  51. {
  52. j--;
  53. }
  54. else
  55. i++;
  56. }
  57. return count;
  58. }
  59.  
  60. int main()
  61. {
  62.  
  63. int a[12];
  64. for(int i=0;i<=11;i++)
  65. cin>>a[i];
  66. Fns(a,0,11);
  67. for(int j=0;j<=11;j++)
  68. cout<<a[j]<<" ";
  69. int k;
  70. cin>>k;
  71. cout<<endl<<TotalSum(a,11,k);
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 3300KB
stdin
12
18
14
5
15
6
7
8
5
18
15
8
20
stdout
5 5 6 7 8 8 12 14 15 15 18 18 
7