Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c++

c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
9 Posts 4 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tuanguyen2000
    wrote on last edited by
    #1

    #include
    using namespace std;

    bool checkSquareNumber(int num)
    {
    int i=0;
    while(i*i <= num)
    {
    if(i*i==num)
    return true;
    i++;
    }
    return false;
    }
    int squareNumber(int num)
    {
    int i=0;
    while(i*i <= num)
    {
    if(i*i==num)
    return i;
    i++;
    }
    return i;
    }
    int numberOfStep(int num)
    {
    int cnt =0;
    int i =0;
    while(num)
    {
    if(checkSquareNumber(num)== true)
    {
    num = squareNumber(num);
    cnt++;
    }
    else if(num%3==0)
    {
    num = num/3;
    cnt++;
    }
    else if(num%2==0)
    {
    num/=2;
    cnt++;
    }
    else
    {
    num--;
    cnt++;
    }
    }
    return cnt;
    }
    int main()
    {
    int num,t;
    cin>>t;
    while(t--)
    {
    cin>>num;
    cout<

    T 1 Reply Last reply
    0
    • T tuanguyen2000

      #include
      using namespace std;

      bool checkSquareNumber(int num)
      {
      int i=0;
      while(i*i <= num)
      {
      if(i*i==num)
      return true;
      i++;
      }
      return false;
      }
      int squareNumber(int num)
      {
      int i=0;
      while(i*i <= num)
      {
      if(i*i==num)
      return i;
      i++;
      }
      return i;
      }
      int numberOfStep(int num)
      {
      int cnt =0;
      int i =0;
      while(num)
      {
      if(checkSquareNumber(num)== true)
      {
      num = squareNumber(num);
      cnt++;
      }
      else if(num%3==0)
      {
      num = num/3;
      cnt++;
      }
      else if(num%2==0)
      {
      num/=2;
      cnt++;
      }
      else
      {
      num--;
      cnt++;
      }
      }
      return cnt;
      }
      int main()
      {
      int num,t;
      cin>>t;
      while(t--)
      {
      cin>>num;
      cout<

      T Offline
      T Offline
      tuanguyen2000
      wrote on last edited by
      #2

      Quote:

      I'm having a problem with my code. can anyone point out where my error is?. i am trying to count the minimum number of steps to get any number to 0

      J 1 Reply Last reply
      0
      • T tuanguyen2000

        Quote:

        I'm having a problem with my code. can anyone point out where my error is?. i am trying to count the minimum number of steps to get any number to 0

        J Offline
        J Offline
        James Curran
        wrote on last edited by
        #3

        Ok, so it appears the rules of this game are: - If a number is a perfect square, take its square root - if a number is divisible by 3, divide it by 3 - if a number is divisible by 2, divide it by 2 - otherwise, subtract 1. So, given an input of 32, we'd go 32 -> 16 -> 4 -> 2 -> 1 -> 0 for 5 steps. and given 75, we'd go 75 -> 25 -> 5 -> 4 -> 2 -> 1 -> 0 for 6 steps. Now, what problem are you having? A quick glance over the code didn't show any obvious errors. Your checkSquareNumber() and squareNumber() functions are quite inefficient. You'd be better off googling a better square root algorithm (or just using the library sqrt() function). Ad since they are doing the same thing, they could be combined (return -1 if it is not a perfect square, and you can replace the two calls with one)

        Truth, James

        T 1 Reply Last reply
        0
        • J James Curran

          Ok, so it appears the rules of this game are: - If a number is a perfect square, take its square root - if a number is divisible by 3, divide it by 3 - if a number is divisible by 2, divide it by 2 - otherwise, subtract 1. So, given an input of 32, we'd go 32 -> 16 -> 4 -> 2 -> 1 -> 0 for 5 steps. and given 75, we'd go 75 -> 25 -> 5 -> 4 -> 2 -> 1 -> 0 for 6 steps. Now, what problem are you having? A quick glance over the code didn't show any obvious errors. Your checkSquareNumber() and squareNumber() functions are quite inefficient. You'd be better off googling a better square root algorithm (or just using the library sqrt() function). Ad since they are doing the same thing, they could be combined (return -1 if it is not a perfect square, and you can replace the two calls with one)

          Truth, James

          T Offline
          T Offline
          tuanguyen2000
          wrote on last edited by
          #4

          Quote:

          i am having problem with my code. where the first condition to check if the original number is a perfect square. can you point out where my error is or point me to a better direction for this problem. i am trying to count the minimum number of steps to convert any number to 0

          CPalliniC 1 Reply Last reply
          0
          • T tuanguyen2000

            Quote:

            i am having problem with my code. where the first condition to check if the original number is a perfect square. can you point out where my error is or point me to a better direction for this problem. i am trying to count the minimum number of steps to convert any number to 0

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of num), together withe the expected result. Otherwise, how could we possibly help?

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            In testa che avete, signor di Ceprano?

            T 2 Replies Last reply
            0
            • CPalliniC CPallini

              If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of num), together withe the expected result. Otherwise, how could we possibly help?

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              T Offline
              T Offline
              tuanguyen2000
              wrote on last edited by
              #6

              intput: 2 10 32 output: 4 5

              1 Reply Last reply
              0
              • CPalliniC CPallini

                If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of num), together withe the expected result. Otherwise, how could we possibly help?

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                T Offline
                T Offline
                tuanguyen2000
                wrote on last edited by
                #7

                Quote:

                the problem is I can't run my code

                CPalliniC L 2 Replies Last reply
                0
                • T tuanguyen2000

                  Quote:

                  the problem is I can't run my code

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  A quick run with the debugger would spot immediately the bug: if num becomes 1 then your code end in a infinite loop, because 1 is a perfect square of itself. For a quick fix, replace

                  Quote:

                  if(checkSquareNumber(num)== true)

                  with

                  if(num > 1 && checkSquareNumber(num)== true)

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • T tuanguyen2000

                    Quote:

                    the problem is I can't run my code

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    The two functions checkSquareNumber and squareNumber should both start their calculations at 2, rather than 0. For square roots, zero is meaningless and 1 is the square root of itself.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups