C++ help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • untitled

    #16
    Re: C++ help

    ok here is my idea, it gives 1cent below the required number,

    x is the required number

    int sevens=x/7 //without the fractures
    remainder = x-(sevens*7)
    int twos= remainder/2 //without fractures
    uint nines=sevens-twos //without sign
    sevens=sevens-nines
    twos=twos-nines

    print nines,sevens,tw os

    i'll write the one with one above the required number in few minutes...

    Comment

    • untitled

      #17
      Re: C++ help

      then its an easy one,

      x is the requested number


      int sevens=x/7 //without the fractures
      remainder=x - (sevens*7)
      int twos= remainder/2 //without the fractures
      if (remainder - (twos*2)) =1 then twos++
      if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
      sevens=0
      if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
      =0

      just put it in c code

      Comment

      • Alan Johnson

        #18
        Re: C++ help

        untitled wrote:
        then its an easy one,
        >
        x is the requested number
        >
        >
        int sevens=x/7 //without the fractures
        remainder=x - (sevens*7)
        int twos= remainder/2 //without the fractures
        if (remainder - (twos*2)) =1 then twos++
        if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
        sevens=0
        if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
        =0
        >
        just put it in c code
        >
        Judging from the OP's description, this is an assignment for some sort
        of algorithms class. If that's the case, then coming up with a solution
        that actually depends on the number 2, 7, and 9 isn't particularly useful.

        How would you solve the problem if the denominations were D1, D2, and
        D3, rather than some specific three numbers? Or better yet, let's say
        you have denominations D1 through Dk.

        --
        Alan Johnson

        Comment

        • davy.zou@brentwood.bc.ca

          #19
          Re: C++ help

          On Mar 3, 9:54 pm, Alan Johnson <a...@yahoo.com wrote:
          untitled wrote:
          then its an easy one,
          >
          x is the requested number
          >
          int sevens=x/7 //without the fractures
          remainder=x - (sevens*7)
          int twos= remainder/2 //without the fractures
          if (remainder - (twos*2)) =1 then twos++
          if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
          sevens=0
          if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
          =0
          >
          just put it in c code
          >
          Judging from the OP's description, this is an assignment for some sort
          of algorithms class. If that's the case, then coming up with a solution
          that actually depends on the number 2, 7, and 9 isn't particularly useful.
          >
          How would you solve the problem if the denominations were D1, D2, and
          D3, rather than some specific three numbers? Or better yet, let's say
          you have denominations D1 through Dk.
          >
          --
          Alan Johnson
          It is not an algorithm class, it is purely C++, we get to algorithms
          next year, I think.
          But you have peaked my interest, would D1 through Dk be inputed by
          user? If it is inputed by user, then it shouldn't be too difficult to
          add the lines of codes to the program that will assign the user input
          to D1 through Dk.

          Davy

          Comment

          • davy.zou@brentwood.bc.ca

            #20
            Re: C++ help

            On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
            then its an easy one,
            >
            x is the requested number
            >
            int sevens=x/7 //without the fractures
            remainder=x - (sevens*7)
            int twos= remainder/2 //without the fractures
            if (remainder - (twos*2)) =1 then twos++
            if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
            sevens=0
            if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
            =0
            >
            just put it in c code
            1 1int sevens=x/7 //without the
            fractures
            2 remainder=x - (sevens*7)
            3 int twos= remainder/2 //without the fractures
            4 if (remainder - (twos*2)) =1 then twos++
            5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
            sevens=0
            6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
            twos=0

            I don't get it. Several things in fact.

            First, I can't see what your program does. Since it doesn't have an
            output statement. But I will go on a tangent and guess that it is for
            comparing which ever way is the most effective way to distribute the
            stamps.

            Second, why not just use x%7 for line 2?

            Third, since you assigned that twos be reminder/2, then the act of
            mutiplying twos by 2 would result in the remainder from line2.
            Therefore, in line4, having remainder-remainder would result in 0.

            Fourth, in line4, what do you mean by twos++?

            Five, line 5 and 6 doesn't make much sense either.

            Six, what is if final out put?

            I really appreciate you trying to help, and I know that with the above
            list I sound like an ungrateful little brat. But I am grateful. I just
            don't get your programing. Please explain.

            Davy

            Comment

            • davy.zou@brentwood.bc.ca

              #21
              Re: C++ help

              On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
              then its an easy one,
              >
              x is the requested number
              >
              int sevens=x/7 //without the fractures
              remainder=x - (sevens*7)
              int twos= remainder/2 //without the fractures
              if (remainder - (twos*2)) =1 then twos++
              if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
              sevens=0
              if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
              =0
              >
              just put it in c code
              1 1int sevens=x/7 //without the
              fractures
              2 remainder=x - (sevens*7)
              3 int twos= remainder/2 //without the fractures
              4 if (remainder - (twos*2)) =1 then twos++
              5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
              sevens=0
              6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
              twos=0

              I don't get it. Several things in fact.

              First, I can't see what your program does. Since it doesn't have an
              output statement. But I will go on a tangent and guess that it is for
              comparing which ever way is the most effective way to distribute the
              stamps.

              Second, why not just use x%7 for line 2?

              Third, since you assigned that twos be reminder/2, then the act of
              mutiplying twos by 2 would result in the remainder from line2.
              Therefore, in line4, having remainder-remainder would result in 0.

              Fourth, in line4, what do you mean by twos++?

              Five, line 5 and 6 doesn't make much sense either.

              Six, what is if final out put?

              I really appreciate you trying to help, and I know that with the above
              list I sound like an ungrateful little brat. But I am grateful. I just
              don't get your programing. Please explain.

              Davy

              Comment

              • davy.zou@brentwood.bc.ca

                #22
                Re: C++ help

                On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
                then its an easy one,
                >
                x is the requested number
                >
                int sevens=x/7 //without the fractures
                remainder=x - (sevens*7)
                int twos= remainder/2 //without the fractures
                if (remainder - (twos*2)) =1 then twos++
                if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                sevens=0
                if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
                =0
                >
                just put it in c code
                1 1int sevens=x/7 //without the
                fractures
                2 remainder=x - (sevens*7)
                3 int twos= remainder/2 //without the fractures
                4 if (remainder - (twos*2)) =1 then twos++
                5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                sevens=0
                6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
                twos=0

                I don't get it. Several things in fact.

                First, I can't see what your program does. Since it doesn't have an
                output statement. But I will go on a tangent and guess that it is for
                comparing which ever way is the most effective way to distribute the
                stamps.

                Second, why not just use x%7 for line 2?

                Third, since you assigned that twos be reminder/2, then the act of
                mutiplying twos by 2 would result in the remainder from line2.
                Therefore, in line4, having remainder-remainder would result in 0.

                Fourth, in line4, what do you mean by twos++?

                Five, line 5 and 6 doesn't make much sense either.

                Six, what is if final out put?

                I really appreciate you trying to help, and I know that with the above
                list I sound like an ungrateful little brat. But I am grateful. I just
                don't get your programing. Please explain.

                Davy

                Comment

                • Alan Johnson

                  #23
                  Re: C++ help

                  davy.zou@brentw ood.bc.ca wrote:
                  But you have peaked my interest, would D1 through Dk be inputed by
                  user? If it is inputed by user, then it shouldn't be too difficult to
                  add the lines of codes to the program that will assign the user input
                  to D1 through Dk.
                  >
                  Davy
                  >
                  You can expand the recurrence I posted earlier in this thread to any
                  number of denominations. So if I had an array of denominations d.

                  C(n) = 0 if n = 0
                  C(n) = minimum value over all i such that d[i] < n of 1 + C(n - d[i])

                  If we were to translate this to pseudocode it might look something like:

                  // n is the number for which we are finding a solution.
                  // d is an array of denominations.
                  // k is the number of denominations
                  // S is an array of index denominations.
                  Solve(n, d, k)

                  Allocate arrays C and S of size n

                  // Base condition of recurrence C(0)
                  C[0] = 0

                  // Now compute C(1) through C(n)
                  for v = 1 to n

                  // maybe something like std::numeric_li mits<unsigned>: :max()
                  min = infinity

                  // Find the minimum value of 1+C(n-d[i])
                  for i = 1 to k
                  if (d[i] <= k)
                  value = 1 + C[v - d[i]]
                  if (value < min)
                  min = value
                  stamp = i

                  // Store the results for the next loop.
                  C[v] = min
                  S[v] = stamp

                  return S


                  The array C holds the minimum count of stamps needed for each value,
                  which is a necessary piece of information, but not exactly what we want.
                  The array S keeps up with the index of which denomination stamp we add
                  at each step. We can use that knowledge to construct the actual set of
                  stamps with something like:

                  // n is the number for which we are finding a solution.
                  // d is an array of denominations.
                  // S is an array of index denominations.
                  PrintSet(n, d, S)
                  while (n 0)
                  Print S[n]
                  n = n - d[S[n]]

                  This works due to the same logic we used to create the array in the
                  first place. At each step we are just subtracting the denomination that
                  we decided was necessary to add to the optimal substructure to get the
                  solution for n.

                  --
                  Alan Johnson

                  Comment

                  • Alan Johnson

                    #24
                    Re: C++ help

                    Alan Johnson wrote:
                    davy.zou@brentw ood.bc.ca wrote:
                    >But you have peaked my interest, would D1 through Dk be inputed by
                    >user? If it is inputed by user, then it shouldn't be too difficult to
                    >add the lines of codes to the program that will assign the user input
                    >to D1 through Dk.
                    >>
                    >Davy
                    >>
                    >
                    You can expand the recurrence I posted earlier in this thread to any
                    number of denominations. So if I had an array of denominations d.
                    >
                    C(n) = 0 if n = 0
                    C(n) = minimum value over all i such that d[i] < n of 1 + C(n - d[i])
                    >
                    If we were to translate this to pseudocode it might look something like:
                    >
                    // n is the number for which we are finding a solution.
                    // d is an array of denominations.
                    // k is the number of denominations
                    // S is an array of index denominations.
                    Solve(n, d, k)
                    >
                    Allocate arrays C and S of size n
                    >
                    // Base condition of recurrence C(0)
                    C[0] = 0
                    >
                    // Now compute C(1) through C(n)
                    for v = 1 to n
                    >
                    // maybe something like std::numeric_li mits<unsigned>: :max()
                    min = infinity
                    >
                    // Find the minimum value of 1+C(n-d[i])
                    for i = 1 to k
                    if (d[i] <= k)
                    value = 1 + C[v - d[i]]
                    if (value < min)
                    min = value
                    stamp = i
                    >
                    // Store the results for the next loop.
                    C[v] = min
                    S[v] = stamp
                    >
                    return S
                    >
                    >
                    The array C holds the minimum count of stamps needed for each value,
                    which is a necessary piece of information, but not exactly what we want.
                    The array S keeps up with the index of which denomination stamp we add
                    at each step. We can use that knowledge to construct the actual set of
                    stamps with something like:
                    >
                    // n is the number for which we are finding a solution.
                    // d is an array of denominations.
                    // S is an array of index denominations.
                    PrintSet(n, d, S)
                    while (n 0)
                    Print S[n]
                    n = n - d[S[n]]
                    >
                    Oops. One small correction. You'd probably like to print the actual
                    denominations in the set, rather than the indices, so:
                    Print d[S[n]]
                    This works due to the same logic we used to create the array in the
                    first place. At each step we are just subtracting the denomination that
                    we decided was necessary to add to the optimal substructure to get the
                    solution for n.
                    >

                    --
                    Alan Johnson

                    Comment

                    • Alan Johnson

                      #25
                      Re: C++ help

                      Alan Johnson wrote:
                      Alan Johnson wrote:
                      >davy.zou@brentw ood.bc.ca wrote:
                      >>But you have peaked my interest, would D1 through Dk be inputed by
                      >>user? If it is inputed by user, then it shouldn't be too difficult to
                      >>add the lines of codes to the program that will assign the user input
                      >>to D1 through Dk.
                      >>>
                      >>Davy
                      >>>
                      >>
                      >You can expand the recurrence I posted earlier in this thread to any
                      >number of denominations. So if I had an array of denominations d.
                      >>
                      >C(n) = 0 if n = 0
                      >C(n) = minimum value over all i such that d[i] < n of 1 + C(n - d[i])
                      >>
                      >If we were to translate this to pseudocode it might look something like:
                      >>
                      >// n is the number for which we are finding a solution.
                      >// d is an array of denominations.
                      >// k is the number of denominations
                      >// S is an array of index denominations.
                      >Solve(n, d, k)
                      >>
                      > Allocate arrays C and S of size n
                      >>
                      > // Base condition of recurrence C(0)
                      > C[0] = 0
                      >>
                      > // Now compute C(1) through C(n)
                      > for v = 1 to n
                      >>
                      > // maybe something like std::numeric_li mits<unsigned>: :max()
                      > min = infinity
                      >>
                      > // Find the minimum value of 1+C(n-d[i])
                      > for i = 1 to k
                      > if (d[i] <= k)
                      Gah. One more correction. This should have been:
                      if (d[i] <= v)

                      > value = 1 + C[v - d[i]]
                      > if (value < min)
                      > min = value
                      > stamp = i
                      >>
                      > // Store the results for the next loop.
                      > C[v] = min
                      > S[v] = stamp
                      >>
                      > return S
                      >>
                      >>
                      >The array C holds the minimum count of stamps needed for each value,
                      >which is a necessary piece of information, but not exactly what we
                      >want. The array S keeps up with the index of which denomination stamp
                      >we add at each step. We can use that knowledge to construct the
                      >actual set of stamps with something like:
                      >>
                      >// n is the number for which we are finding a solution.
                      >// d is an array of denominations.
                      >// S is an array of index denominations.
                      >PrintSet(n, d, S)
                      > while (n 0)
                      > Print S[n]
                      > n = n - d[S[n]]
                      >>
                      >
                      Oops. One small correction. You'd probably like to print the actual
                      denominations in the set, rather than the indices, so:
                      Print d[S[n]]
                      >
                      >This works due to the same logic we used to create the array in the
                      >first place. At each step we are just subtracting the denomination
                      >that we decided was necessary to add to the optimal substructure to
                      >get the solution for n.
                      >>
                      >
                      >

                      --
                      Alan Johnson

                      Comment

                      • untitled

                        #26
                        Re: C++ help

                        On Mar 4, 5:35 am, davy....@brentw ood.bc.ca wrote:
                        On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
                        >
                        then its an easy one,
                        >
                        x is the requested number
                        >
                        int sevens=x/7 //without the fractures
                        remainder=x - (sevens*7)
                        int twos= remainder/2 //without the fractures
                        if (remainder - (twos*2)) =1 then twos++
                        if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                        sevens=0
                        if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
                        =0
                        >
                        just put it in c code
                        >
                        1 1int sevens=x/7 //without the
                        fractures
                        2 remainder=x - (sevens*7)
                        3 int twos= remainder/2 //without the fractures
                        4 if (remainder - (twos*2)) =1 then twos++
                        5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                        sevens=0
                        6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
                        twos=0
                        >
                        I don't get it. Several things in fact.
                        >
                        First, I can't see what your program does. Since it doesn't have an
                        output statement. But I will go on a tangent and guess that it is for
                        comparing which ever way is the most effective way to distribute the
                        stamps.
                        >
                        Second, why not just use x%7 for line 2?
                        >
                        Third, since you assigned that twos be reminder/2, then the act of
                        mutiplying twos by 2 would result in the remainder from line2.
                        Therefore, in line4, having remainder-remainder would result in 0.
                        >
                        Fourth, in line4, what do you mean by twos++?
                        >
                        Five, line 5 and 6 doesn't make much sense either.
                        >
                        Six, what is if final out put?
                        >
                        I really appreciate you trying to help, and I know that with the above
                        list I sound like an ungrateful little brat. But I am grateful. I just
                        don't get your programing. Please explain.
                        >
                        Davy
                        you are right, it wasn't clear,

                        first i'll explain how the program will think then i'll answer your
                        questions:

                        first line will count how many 7cent stamp could be afforded with the
                        money required, of course there will be some extra cents we will
                        process it in line 2
                        line 2 will see how many extra cents you have after we got all the
                        sevens.
                        line 3 will check how many 2cent stamps can be afforded for that extra
                        cents
                        line 4 if the remainder was less than 2 cents (ie. 1 cent) then of
                        course we will consider it 2cent that is adding one cent over the
                        required money as you told me.
                        in line 5,6 we replace every 7cent and 2cent stamp to one 9cent stamp
                        so we got the most effective number of stamps.
                        line 5 if the 7c stamps was less than the 2c stamps, then combine all
                        the sevens with an equivilant number of 2c stamps and replace them
                        with 9c stamps. then delete the combined 7 and 2c stamps.
                        line 6 do the same if the twos was more than sevens.

                        the out put will be
                        int sevens
                        int nines
                        int twos

                        i'll write the code in few minutes.

                        Comment

                        • untitled

                          #27
                          Re: C++ help

                          On Mar 4, 1:36 pm, "untitled" <awsra...@gmail .comwrote:
                          On Mar 4, 5:35 am, davy....@brentw ood.bc.ca wrote:
                          >
                          >
                          >
                          >
                          >
                          On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
                          >
                          then its an easy one,
                          >
                          x is the requested number
                          >
                          int sevens=x/7 //without the fractures
                          remainder=x - (sevens*7)
                          int twos= remainder/2 //without the fractures
                          if (remainder - (twos*2)) =1 then twos++
                          if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                          sevens=0
                          if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos, twos
                          =0
                          >
                          just put it in c code
                          >
                          1 1int sevens=x/7 //without the
                          fractures
                          2 remainder=x - (sevens*7)
                          3 int twos= remainder/2 //without the fractures
                          4 if (remainder - (twos*2)) =1 then twos++
                          5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                          sevens=0
                          6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
                          twos=0
                          >
                          I don't get it. Several things in fact.
                          >
                          First, I can't see what your program does. Since it doesn't have an
                          output statement. But I will go on a tangent and guess that it is for
                          comparing which ever way is the most effective way to distribute the
                          stamps.
                          >
                          Second, why not just use x%7 for line 2?
                          >
                          Third, since you assigned that twos be reminder/2, then the act of
                          mutiplying twos by 2 would result in the remainder from line2.
                          Therefore, in line4, having remainder-remainder would result in 0.
                          >
                          Fourth, in line4, what do you mean by twos++?
                          >
                          Five, line 5 and 6 doesn't make much sense either.
                          >
                          Six, what is if final out put?
                          >
                          I really appreciate you trying to help, and I know that with the above
                          list I sound like an ungrateful little brat. But I am grateful. I just
                          don't get your programing. Please explain.
                          >
                          Davy
                          >
                          you are right, it wasn't clear,
                          >
                          first i'll explain how the program will think then i'll answer your
                          questions:
                          >
                          first line will count how many 7cent stamp could be afforded with the
                          money required, of course there will be some extra cents we will
                          process it in line 2
                          line 2 will see how many extra cents you have after we got all the
                          sevens.
                          line 3 will check how many 2cent stamps can be afforded for that extra
                          cents
                          line 4 if the remainder was less than 2 cents (ie. 1 cent) then of
                          course we will consider it 2cent that is adding one cent over the
                          required money as you told me.
                          in line 5,6 we replace every 7cent and 2cent stamp to one 9cent stamp
                          so we got the most effective number of stamps.
                          line 5 if the 7c stamps was less than the 2c stamps, then combine all
                          the sevens with an equivilant number of 2c stamps and replace them
                          with 9c stamps. then delete the combined 7 and 2c stamps.
                          line 6 do the same if the twos was more than sevens.
                          >
                          the out put will be
                          int sevens
                          int nines
                          int twos
                          >
                          i'll write the code in few minutes.- Hide quoted text -
                          >
                          - Show quoted text -
                          ok here is the code, tell me if it workes, it works with me by the
                          way:

                          #include <cstdlib>
                          #include <iostream>

                          using namespace std;

                          int main(int argc, char *argv[])
                          {
                          int twos=0;
                          int sevens=0;
                          int nines=0;
                          int reqMoney=0;
                          int remainderCents= 0;

                          cout<<"input required money"<<endl;
                          cin>>reqMoney;

                          sevens=reqMoney/7; //check how many sevens can be afforded
                          remainderCents= reqMoney-(sevens*7); //check the extra cents you
                          can use % but i prefer this i don't know why
                          twos= remainderCents/2; //check how many twos can be afforded for
                          the extra cents left
                          if ( (remainderCents-(twos*2)) ==1) twos++; //add one cent above
                          the reqmoney if the extra cents=1
                          if ( (sevens != 0) && (sevens<twos))
                          {
                          twos=twos-sevens;
                          nines=sevens;
                          sevens=0;
                          }

                          if ( (twos != 0) && (sevens>twos))
                          {
                          sevens=sevens-twos;
                          nines=twos;
                          twos=0;
                          }
                          cout<<"twos="<< twos<<endl;
                          cout<<"sevens=" <<sevens<<end l;
                          cout<<"nines="< <nines<<endl;



                          system("PAUSE") ;
                          return EXIT_SUCCESS;
                          }

                          Comment

                          • roy axenov

                            #28
                            Re: C++ help

                            On Mar 4, 2:36 pm, "untitled" <awsra...@gmail .comwrote:
                            On Mar 4, 5:35 am, davy....@brentw ood.bc.ca wrote:
                            On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .com>
                            wrote:
                            int sevens=x/7
                            remainder=x - (sevens*7)
                            int twos= remainder/2
                            if (remainder - (twos*2)) =1 then twos++
                            if sevens != 0 && sevens<twos then twos=twos-sevens,
                            nines= sevens, sevens=0
                            if twos != 0 && twos<sevens then sevens=sevens-twos,
                            nines= twos, twos =0
                            i'll write the code in few minutes.
                            Unless I'm much mistaken, this doesn't work. Try 81. The
                            correct answer is 9x9, while your algorithm would suggest
                            9x7, 2x9.

                            Here's what seems to be a general solution (the only
                            problem with it is that its efficiency borders on that of
                            bogosort):

                            #include <iostream>
                            #include <stack>
                            #include <map>

                            const int stamp_1 = 2 ;
                            const int stamp_2 = 7 ;
                            const int stamp_3 = 9 ;

                            namespace xyz
                            { int f ( std :: map < int , int x )
                            { int result = 0 ;
                            for
                            ( std :: map < int , int :: iterator i =
                            x . begin ( ) ;
                            i != x . end ( ) ; ++ i )
                            result += ( * i ) . second ;
                            return result ; }
                            int g ( std :: map < int , int x )
                            { int result = 0 ;
                            for
                            ( std :: map < int , int :: iterator i =
                            x . begin ( ) ;
                            i != x . end ( ) ; ++ i )
                            result += ( * i ) . first * ( * i ) . second ;
                            return result ; }
                            std :: map < int , int solve
                            ( int s , std :: stack < int c )
                            { std :: map < int , int result ;
                            int best = 0 ;
                            if ( c . size ( ) )
                            { result [ 0 ] = 1 ;
                            int cur_c = c . top ( ) ;
                            c . pop ( ) ;
                            int max_c = s / cur_c ;
                            std :: map < int , int tmp ;
                            for ( int i = 0 ; i <= max_c ; ++ i )
                            { tmp = solve ( s - cur_c * i , c ) ;
                            if ( tmp [ 0 ] ) continue ;
                            tmp [ cur_c ] = i ;
                            if ( g ( tmp ) != s ) continue ;
                            int cur_val = f ( tmp ) ;
                            if ( ! best || cur_val < best )
                            { result = tmp ; best = cur_val ; } } }
                            return result ; } } ;

                            int main ( )
                            { int sum ;
                            std :: cin >sum ;
                            std :: stack < int stamps ;
                            stamps . push ( stamp_1 ) ;
                            stamps . push ( stamp_2 ) ;
                            stamps . push ( stamp_3 ) ;
                            std :: map < int , int solution =
                            xyz :: solve ( sum , stamps ) ;
                            for
                            ( std :: map < int , int :: iterator i =
                            solution . begin ( ) ;
                            i != solution . end ( ) ; ++ i )
                            std :: cout << ( * i ) . first << " " <<
                            ( * i ) . second << std :: endl ; }

                            --
                            roy axenov

                            Comment

                            • Kai-Uwe Bux

                              #29
                              Re: C++ help

                              untitled wrote:
                              On Mar 4, 1:36 pm, "untitled" <awsra...@gmail .comwrote:
                              >On Mar 4, 5:35 am, davy....@brentw ood.bc.ca wrote:
                              >>
                              >>
                              >>
                              >>
                              >>
                              On Mar 3, 9:39 pm, "untitled" <awsra...@gmail .comwrote:
                              >>
                              then its an easy one,
                              >>
                              x is the requested number
                              >>
                              int sevens=x/7 //without the fractures
                              remainder=x - (sevens*7)
                              int twos= remainder/2 //without the fractures
                              if (remainder - (twos*2)) =1 then twos++
                              if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                              sevens=0
                              if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
                              twos =0
                              >>
                              just put it in c code
                              >>
                              1 1int sevens=x/7 //without the
                              fractures
                              2 remainder=x - (sevens*7)
                              3 int twos= remainder/2 //without the fractures
                              4 if (remainder - (twos*2)) =1 then twos++
                              5 if sevens != 0 && sevens<twos then twos=twos-sevens, nines= sevens,
                              sevens=0
                              6 if twos != 0 && twos<sevens then sevens=sevens-twos, nines= twos,
                              twos=0
                              >>
                              I don't get it. Several things in fact.
                              >>
                              First, I can't see what your program does. Since it doesn't have an
                              output statement. But I will go on a tangent and guess that it is for
                              comparing which ever way is the most effective way to distribute the
                              stamps.
                              >>
                              Second, why not just use x%7 for line 2?
                              >>
                              Third, since you assigned that twos be reminder/2, then the act of
                              mutiplying twos by 2 would result in the remainder from line2.
                              Therefore, in line4, having remainder-remainder would result in 0.
                              >>
                              Fourth, in line4, what do you mean by twos++?
                              >>
                              Five, line 5 and 6 doesn't make much sense either.
                              >>
                              Six, what is if final out put?
                              >>
                              I really appreciate you trying to help, and I know that with the above
                              list I sound like an ungrateful little brat. But I am grateful. I just
                              don't get your programing. Please explain.
                              >>
                              Davy
                              >>
                              >you are right, it wasn't clear,
                              >>
                              >first i'll explain how the program will think then i'll answer your
                              >questions:
                              >>
                              >first line will count how many 7cent stamp could be afforded with the
                              >money required, of course there will be some extra cents we will
                              >process it in line 2
                              >line 2 will see how many extra cents you have after we got all the
                              >sevens.
                              >line 3 will check how many 2cent stamps can be afforded for that extra
                              >cents
                              >line 4 if the remainder was less than 2 cents (ie. 1 cent) then of
                              >course we will consider it 2cent that is adding one cent over the
                              >required money as you told me.
                              >in line 5,6 we replace every 7cent and 2cent stamp to one 9cent stamp
                              >so we got the most effective number of stamps.
                              >line 5 if the 7c stamps was less than the 2c stamps, then combine all
                              >the sevens with an equivilant number of 2c stamps and replace them
                              >with 9c stamps. then delete the combined 7 and 2c stamps.
                              >line 6 do the same if the twos was more than sevens.
                              >>
                              >the out put will be
                              >int sevens
                              >int nines
                              >int twos
                              >>
                              >i'll write the code in few minutes.- Hide quoted text -
                              >>
                              >- Show quoted text -
                              >
                              ok here is the code, tell me if it workes, it works with me by the
                              way:
                              >
                              #include <cstdlib>
                              #include <iostream>
                              >
                              using namespace std;
                              >
                              int main(int argc, char *argv[])
                              {
                              int twos=0;
                              int sevens=0;
                              int nines=0;
                              int reqMoney=0;
                              int remainderCents= 0;
                              >
                              cout<<"input required money"<<endl;
                              cin>>reqMoney;
                              >
                              sevens=reqMoney/7; //check how many sevens can be afforded
                              remainderCents= reqMoney-(sevens*7); //check the extra cents you
                              can use % but i prefer this i don't know why
                              twos= remainderCents/2; //check how many twos can be afforded for
                              the extra cents left
                              if ( (remainderCents-(twos*2)) ==1) twos++; //add one cent above
                              the reqmoney if the extra cents=1
                              if ( (sevens != 0) && (sevens<twos))
                              {
                              twos=twos-sevens;
                              nines=sevens;
                              sevens=0;
                              }
                              >
                              if ( (twos != 0) && (sevens>twos))
                              {
                              sevens=sevens-twos;
                              nines=twos;
                              twos=0;
                              }
                              cout<<"twos="<< twos<<endl;
                              cout<<"sevens=" <<sevens<<end l;
                              cout<<"nines="< <nines<<endl;
                              >
                              >
                              >
                              system("PAUSE") ;
                              return EXIT_SUCCESS;
                              }
                              I just tried it. For 100 it tells me to use

                              100c = 13 x 7c + 1 x 9c

                              This is not optimal because

                              100c = 4 x 7c + 8 x 9c

                              is better.


                              Best

                              Kai-Uwe Bux

                              Comment

                              • el3anchoke@gmail.com

                                #30
                                Re: C++ help

                                This "program" is not written in c++, i'm a Java programmer , but you
                                will get the idea by just looking at the code.
                                didnt have time to comment it , the number of function can be
                                optimized to only one if you forward where you want the function to
                                start as a second varible :)

                                Hope it works for you .




                                //three function are used to calculate the lest number of stamps
                                needed

                                function int dvide_9 (int money)
                                {
                                int num = 0;
                                rest = moeny%9
                                num=num+(moeny/9)
                                if (rest = 8) {

                                num=num+1
                                rest = 0;
                                }
                                rest = rest%7
                                num = num +(rest/7)
                                if (rest = 6) {

                                num=num+1
                                rest = 0;
                                }
                                rest = rest % 2
                                num = num + (rest/2)
                                if (rest = 1) {

                                num=num+1
                                rest = 0;
                                }

                                resturn num;
                                }


                                function int dvide_7 (int money)
                                {
                                int num = 0;

                                rest = money%7
                                num = num +(money/7)
                                if (rest = 6) {

                                num=num+1
                                rest = 0;
                                }
                                rest = rest % 2
                                num = num + (rest/2)
                                if (rest = 1) {

                                num=num+1
                                rest = 0;
                                }

                                resturn num;
                                }



                                function int dvide_2 (int money)
                                {
                                int num = 0;

                                rest = money % 2
                                num = num + (money/2)
                                if (rest = 1) {

                                num=num+1
                                rest = 0;
                                }

                                resturn num;
                                }

                                --------------------------------------------------
                                main program

                                choses the function with less number of (num value) which is the
                                number of stamps used.

                                Comment

                                Working...