Inequality, programming challenge in CPPOutput strings from a set in lexicographical orderMake Depth First...

What can I do if someone tampers with my SSH public key?

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Why is there an extra space when I type "ls" on the Desktop?

Dukha vs legitimate need

Is there a math equivalent to the conditional ternary operator?

Why dativ case for the verb widerspricht?

Is every open circuit a capacitor?

Named nets not connected in Eagle board design

“I had a flat in the centre of town, but I didn’t like living there, so …”

Learning to quickly identify valid fingering for piano?

Iron deposits mined from under the city

Sundering Titan and basic normal lands and snow lands

Genitives like "axeos"

Ultrafilters as a double dual

If nine coins are tossed, what is the probability that the number of heads is even?

Should I use HTTPS on a domain that will only be used for redirection?

PTIJ: Aliyot for the deceased

The (Easy) Road to Code

Preparing as much as possible of a cake in advance

Can a Mexican citizen living in US under DACA drive to Canada?

Paper published similar to PhD thesis

Computing the volume of a simplex-like object with constraints

Are angels creatures (Mark 16:15) and can they repent (Rev 2:5 and Rom 8:21)

Did Amazon pay $0 in taxes last year?



Inequality, programming challenge in CPP


Output strings from a set in lexicographical orderMake Depth First Search program more efficientThe Trip Programming challenge UVA10137LCD Display Programming challenge solutionJolly jumpers programming challengeHackerRank NCR codesprint: Spiral MessageParallelogram ConnectivityHackerrank: Lucky Number Eight (Dynamic Programming)Compute Convex HullFinding intersting path in a graph













0












$begingroup$



Inequality



Given n inequalities about X, how many of them can hold at the same time at most?



The inequalities are in the follwing forms:



X < C



X <= C



X = C



X > C



X >= C



input:



First line with an integer n.



Following n lines, each line with an inequality.



Data Limit:



1<=N<=50,0<=C<=1000



output



One line with an integer, denoting the answer.



Sample Input



4



X = 1



X = 2



X = 3



X > 0



Sample Output



2




Here is my code:



#include <iostream>
#include <string>

#include <vector>

#include <map>
#include <set>

using namespace std;




int main() {
//cout<<"请输入:"<<endl;
int T;
cin >> T;
int count = T;

vector<float> arrayOne(T, 0);
vector<int> arrayTwo(T, 0);
map<string, int> symbols = {
pair<string, int>(">", 1),
pair<string, int>(">=", 2),
pair<string, int>("=", 3),
pair<string, int>("<=", 4),
pair<string, int>("<", 5)
};

while(T--)
{
string sOne;
cin >> sOne;
string sThree;
cin >> sThree;
arrayTwo[count-T-1] = symbols.find(sThree)->second;
std::string sTwo;
std::cin >> sTwo;
float num = stof(sTwo);
arrayOne[count-T-1] = num;
}
set<float> answerSet;
for (float numPiece: arrayOne){
answerSet.insert(numPiece-0.5);
answerSet.insert(numPiece);
answerSet.insert(numPiece+0.5);
}
int answer = 0;
for (set<float>::iterator testNum=answerSet.begin(); testNum != answerSet.end(); ++testNum){
// cout<<*testNum;
int passNum = 0;
int i = 0;
for (int numTwo: arrayTwo){
switch (numTwo) {
case 1:
{
if (*testNum > arrayOne[i]) {
passNum += 1;
}
}
break;
case 2:
{
if (*testNum >= arrayOne[i]) {
passNum += 1;
}
}
break;
case 3:
{
if (*testNum == arrayOne[i]) {
passNum += 1;
}
}
break;
case 4:
{
if (*testNum <= arrayOne[i]) {
passNum += 1;
}
}
break;
case 5:
{
if (*testNum < arrayOne[i]) {
passNum += 1;
}
}
break;
default:
break;
}
i++;
}
answer = max(answer, passNum);
}
cout<<answer<<endl;
return 0;
}



Very intuitive , just get all the boundary numbers, then find the max passes.



How to make it more efficient?









share









$endgroup$

















    0












    $begingroup$



    Inequality



    Given n inequalities about X, how many of them can hold at the same time at most?



    The inequalities are in the follwing forms:



    X < C



    X <= C



    X = C



    X > C



    X >= C



    input:



    First line with an integer n.



    Following n lines, each line with an inequality.



    Data Limit:



    1<=N<=50,0<=C<=1000



    output



    One line with an integer, denoting the answer.



    Sample Input



    4



    X = 1



    X = 2



    X = 3



    X > 0



    Sample Output



    2




    Here is my code:



    #include <iostream>
    #include <string>

    #include <vector>

    #include <map>
    #include <set>

    using namespace std;




    int main() {
    //cout<<"请输入:"<<endl;
    int T;
    cin >> T;
    int count = T;

    vector<float> arrayOne(T, 0);
    vector<int> arrayTwo(T, 0);
    map<string, int> symbols = {
    pair<string, int>(">", 1),
    pair<string, int>(">=", 2),
    pair<string, int>("=", 3),
    pair<string, int>("<=", 4),
    pair<string, int>("<", 5)
    };

    while(T--)
    {
    string sOne;
    cin >> sOne;
    string sThree;
    cin >> sThree;
    arrayTwo[count-T-1] = symbols.find(sThree)->second;
    std::string sTwo;
    std::cin >> sTwo;
    float num = stof(sTwo);
    arrayOne[count-T-1] = num;
    }
    set<float> answerSet;
    for (float numPiece: arrayOne){
    answerSet.insert(numPiece-0.5);
    answerSet.insert(numPiece);
    answerSet.insert(numPiece+0.5);
    }
    int answer = 0;
    for (set<float>::iterator testNum=answerSet.begin(); testNum != answerSet.end(); ++testNum){
    // cout<<*testNum;
    int passNum = 0;
    int i = 0;
    for (int numTwo: arrayTwo){
    switch (numTwo) {
    case 1:
    {
    if (*testNum > arrayOne[i]) {
    passNum += 1;
    }
    }
    break;
    case 2:
    {
    if (*testNum >= arrayOne[i]) {
    passNum += 1;
    }
    }
    break;
    case 3:
    {
    if (*testNum == arrayOne[i]) {
    passNum += 1;
    }
    }
    break;
    case 4:
    {
    if (*testNum <= arrayOne[i]) {
    passNum += 1;
    }
    }
    break;
    case 5:
    {
    if (*testNum < arrayOne[i]) {
    passNum += 1;
    }
    }
    break;
    default:
    break;
    }
    i++;
    }
    answer = max(answer, passNum);
    }
    cout<<answer<<endl;
    return 0;
    }



    Very intuitive , just get all the boundary numbers, then find the max passes.



    How to make it more efficient?









    share









    $endgroup$















      0












      0








      0





      $begingroup$



      Inequality



      Given n inequalities about X, how many of them can hold at the same time at most?



      The inequalities are in the follwing forms:



      X < C



      X <= C



      X = C



      X > C



      X >= C



      input:



      First line with an integer n.



      Following n lines, each line with an inequality.



      Data Limit:



      1<=N<=50,0<=C<=1000



      output



      One line with an integer, denoting the answer.



      Sample Input



      4



      X = 1



      X = 2



      X = 3



      X > 0



      Sample Output



      2




      Here is my code:



      #include <iostream>
      #include <string>

      #include <vector>

      #include <map>
      #include <set>

      using namespace std;




      int main() {
      //cout<<"请输入:"<<endl;
      int T;
      cin >> T;
      int count = T;

      vector<float> arrayOne(T, 0);
      vector<int> arrayTwo(T, 0);
      map<string, int> symbols = {
      pair<string, int>(">", 1),
      pair<string, int>(">=", 2),
      pair<string, int>("=", 3),
      pair<string, int>("<=", 4),
      pair<string, int>("<", 5)
      };

      while(T--)
      {
      string sOne;
      cin >> sOne;
      string sThree;
      cin >> sThree;
      arrayTwo[count-T-1] = symbols.find(sThree)->second;
      std::string sTwo;
      std::cin >> sTwo;
      float num = stof(sTwo);
      arrayOne[count-T-1] = num;
      }
      set<float> answerSet;
      for (float numPiece: arrayOne){
      answerSet.insert(numPiece-0.5);
      answerSet.insert(numPiece);
      answerSet.insert(numPiece+0.5);
      }
      int answer = 0;
      for (set<float>::iterator testNum=answerSet.begin(); testNum != answerSet.end(); ++testNum){
      // cout<<*testNum;
      int passNum = 0;
      int i = 0;
      for (int numTwo: arrayTwo){
      switch (numTwo) {
      case 1:
      {
      if (*testNum > arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 2:
      {
      if (*testNum >= arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 3:
      {
      if (*testNum == arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 4:
      {
      if (*testNum <= arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 5:
      {
      if (*testNum < arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      default:
      break;
      }
      i++;
      }
      answer = max(answer, passNum);
      }
      cout<<answer<<endl;
      return 0;
      }



      Very intuitive , just get all the boundary numbers, then find the max passes.



      How to make it more efficient?









      share









      $endgroup$





      Inequality



      Given n inequalities about X, how many of them can hold at the same time at most?



      The inequalities are in the follwing forms:



      X < C



      X <= C



      X = C



      X > C



      X >= C



      input:



      First line with an integer n.



      Following n lines, each line with an inequality.



      Data Limit:



      1<=N<=50,0<=C<=1000



      output



      One line with an integer, denoting the answer.



      Sample Input



      4



      X = 1



      X = 2



      X = 3



      X > 0



      Sample Output



      2




      Here is my code:



      #include <iostream>
      #include <string>

      #include <vector>

      #include <map>
      #include <set>

      using namespace std;




      int main() {
      //cout<<"请输入:"<<endl;
      int T;
      cin >> T;
      int count = T;

      vector<float> arrayOne(T, 0);
      vector<int> arrayTwo(T, 0);
      map<string, int> symbols = {
      pair<string, int>(">", 1),
      pair<string, int>(">=", 2),
      pair<string, int>("=", 3),
      pair<string, int>("<=", 4),
      pair<string, int>("<", 5)
      };

      while(T--)
      {
      string sOne;
      cin >> sOne;
      string sThree;
      cin >> sThree;
      arrayTwo[count-T-1] = symbols.find(sThree)->second;
      std::string sTwo;
      std::cin >> sTwo;
      float num = stof(sTwo);
      arrayOne[count-T-1] = num;
      }
      set<float> answerSet;
      for (float numPiece: arrayOne){
      answerSet.insert(numPiece-0.5);
      answerSet.insert(numPiece);
      answerSet.insert(numPiece+0.5);
      }
      int answer = 0;
      for (set<float>::iterator testNum=answerSet.begin(); testNum != answerSet.end(); ++testNum){
      // cout<<*testNum;
      int passNum = 0;
      int i = 0;
      for (int numTwo: arrayTwo){
      switch (numTwo) {
      case 1:
      {
      if (*testNum > arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 2:
      {
      if (*testNum >= arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 3:
      {
      if (*testNum == arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 4:
      {
      if (*testNum <= arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      case 5:
      {
      if (*testNum < arrayOne[i]) {
      passNum += 1;
      }
      }
      break;
      default:
      break;
      }
      i++;
      }
      answer = max(answer, passNum);
      }
      cout<<answer<<endl;
      return 0;
      }



      Very intuitive , just get all the boundary numbers, then find the max passes.



      How to make it more efficient?







      c++ programming-challenge





      share












      share










      share



      share










      asked 7 mins ago









      black_pearlblack_pearl

      1187




      1187






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214976%2finequality-programming-challenge-in-cpp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214976%2finequality-programming-challenge-in-cpp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown