fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. int main(){
  5. int testCase, a1, a2, a3, a4, joke, mood;
  6. joke = mood = 0;
  7. std::cin >> testCase;
  8. for (int i = 0; i < testCase; ++i) {
  9. std::cin >> a1 >> a2 >> a3 >> a4;
  10. if (a1 != 0){
  11. joke = mood = a1;
  12. if (a2 != a3){
  13.  
  14. if (a2 > a3){
  15. joke += (a3 * 2);
  16. }
  17. else{
  18. joke += (a2 * 2);
  19. }
  20. for (int j = 0; j < abs(a2 - a3); ++j){
  21. if (mood != -1){
  22. mood -= 1;
  23. joke += 1;
  24. }
  25. else{
  26. break;
  27. }
  28. }
  29. }
  30. else{
  31. joke += (a2 * 2);
  32. }
  33. for (int k = 0; k < a4; ++k){
  34. if (mood != -1){
  35. mood -= 1;
  36. joke += 1;
  37. }
  38. else{
  39. break;
  40. }
  41. }
  42. std::cout << joke << std::endl;
  43. }
  44. else{
  45. std::cout << 1 << std::endl;
  46. }
  47. }
  48. return 0;
  49. }
  50.  
  51. //Please indent properly.
  52.  
  53. <?php
  54. //program to find the common elements of the two array
  55. //here we have to array A and B from which w have to find the common element
  56. //first we sort then using merge sort and after then for traversing through
  57. //the array in one iteration we can find the comman elements the given array
  58. //this is an inspace algorithm meansno extra space is needed
  59.  
  60. //best case time complexity=O(nlogn)
  61. //O(nlogn)-> for sorting
  62. //O(n)-> for while loop to find comman element
  63.  
  64. //average case time complexity=O(nlogn)
  65. //O(nlogn)-> for sorting
  66. //O(n)-> for while loop to find comman element
  67.  
  68. //worst case time complexity =O(nlogn)
  69. //O(nlogn)-> for sorting
  70. //O(n)-> for while loop to find comman element
  71.  
  72.  
  73.  
  74. $commonArray=array();
  75. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  76. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  77. sort($A);
  78. sort($B);
  79. $size1=sizeof($A);
  80. $size2=sizeof($B);
  81. $counter1=0;
  82. $counter2=0;
  83. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  84. {
  85.  
  86. if ($A[$counter1] == $B[$counter2])
  87. {
  88. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  89. $counter1=$counter1+1;
  90. $counter2=$counter2+1;
  91. }
  92. else if ($A[$counter1] < $B[$counter2])
  93. {
  94. $counter1=$counter1+1; }
  95.  
  96. else
  97. {
  98. $counter2=$counter2+1;
  99. }
  100. }
  101.  
  102. print_r($commonArray);//to print the output array
  103. ?>
  104.  
  105.  
Success #stdin #stdout 0.02s 25636KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <cmath>

int main(){
  int testCase, a1, a2, a3, a4, joke, mood;
  joke = mood = 0;
  std::cin >> testCase;
  for (int i = 0; i < testCase; ++i) {
    std::cin >> a1 >> a2 >> a3 >> a4;
    if (a1 != 0){
      joke = mood = a1;
      if (a2 != a3){

        if (a2 > a3){
          joke += (a3 * 2);
        }
        else{
          joke += (a2 * 2);
        }
        for (int j = 0; j < abs(a2 - a3); ++j){
          if (mood != -1){
            mood -= 1;
            joke += 1;
          }
          else{
            break;
          }
        }
      }
      else{
        joke += (a2 * 2);
      }
      for (int k = 0; k < a4; ++k){
        if (mood != -1){
          mood -= 1;
          joke += 1;
        }
        else{
          break;
        }
      }
      std::cout << joke << std::endl;
    }
    else{
      std::cout << 1 << std::endl;
    }
  }
  return 0;
}

//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)