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. find a effictive way!!

find a effictive way!!

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstutorial
5 Posts 3 Posters 0 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.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    for example there is a four bits integer 1122, if someone guess and inout 1234, because 1 and 2 are in 1122, and the position is same, the computer will tell you that you are right with two numbers and they are 1 and 2; as follows is my program, I thinnk they are too complex , could some tell me a simple algorithm, thinks in advantage #include <stdio.h> #include <stdlib.h> int main() { int data1 = 1234; int data2; int gewei1, shiwei1, baiwei1, qianwei1; int gewei2, shiwei2, baiwei2, qianwei2; int count = 0; gewei1 = data1%10; shiwei1 = (data1%100)/10; baiwei1 = (data1%1000)/100; qianwei1 = data1 /1000; int arr1[4] = {qianwei1, baiwei1, shiwei1, gewei1}; printf("Please Enter a interger:"); scanf("%d", &data2); gewei2 = data2%10; shiwei2 = (data2%100)/10; baiwei2 = (data2%1000)/100; qianwei2 = data2 /1000; int arr2[4] = {qianwei2, baiwei2, shiwei2, gewei2}; for (int i=0; i<4; i++) { if(arr1[i] == arr2[i]) { printf("%d ", arr1[i]); count++; } else continue; } printf("the same number is %d ", count); return 0; }

    C L 2 Replies Last reply
    0
    • W wbgxx

      for example there is a four bits integer 1122, if someone guess and inout 1234, because 1 and 2 are in 1122, and the position is same, the computer will tell you that you are right with two numbers and they are 1 and 2; as follows is my program, I thinnk they are too complex , could some tell me a simple algorithm, thinks in advantage #include <stdio.h> #include <stdlib.h> int main() { int data1 = 1234; int data2; int gewei1, shiwei1, baiwei1, qianwei1; int gewei2, shiwei2, baiwei2, qianwei2; int count = 0; gewei1 = data1%10; shiwei1 = (data1%100)/10; baiwei1 = (data1%1000)/100; qianwei1 = data1 /1000; int arr1[4] = {qianwei1, baiwei1, shiwei1, gewei1}; printf("Please Enter a interger:"); scanf("%d", &data2); gewei2 = data2%10; shiwei2 = (data2%100)/10; baiwei2 = (data2%1000)/100; qianwei2 = data2 /1000; int arr2[4] = {qianwei2, baiwei2, shiwei2, gewei2}; for (int i=0; i<4; i++) { if(arr1[i] == arr2[i]) { printf("%d ", arr1[i]); count++; } else continue; } printf("the same number is %d ", count); return 0; }

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      wbgxx wrote:

      for example there is a four bits integer 1122, if someone guess and inout 1234, because 1 and 2 are in 1122, and the position is same

      The position is NOT the same, actually.

      wbgxx wrote:

      I thinnk they are too complex , could some tell me a simple algorithm

      You may make it more concise, anyway, since the current logic looks correct I doubt it would be simpler. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • W wbgxx

        for example there is a four bits integer 1122, if someone guess and inout 1234, because 1 and 2 are in 1122, and the position is same, the computer will tell you that you are right with two numbers and they are 1 and 2; as follows is my program, I thinnk they are too complex , could some tell me a simple algorithm, thinks in advantage #include <stdio.h> #include <stdlib.h> int main() { int data1 = 1234; int data2; int gewei1, shiwei1, baiwei1, qianwei1; int gewei2, shiwei2, baiwei2, qianwei2; int count = 0; gewei1 = data1%10; shiwei1 = (data1%100)/10; baiwei1 = (data1%1000)/100; qianwei1 = data1 /1000; int arr1[4] = {qianwei1, baiwei1, shiwei1, gewei1}; printf("Please Enter a interger:"); scanf("%d", &data2); gewei2 = data2%10; shiwei2 = (data2%100)/10; baiwei2 = (data2%1000)/100; qianwei2 = data2 /1000; int arr2[4] = {qianwei2, baiwei2, shiwei2, gewei2}; for (int i=0; i<4; i++) { if(arr1[i] == arr2[i]) { printf("%d ", arr1[i]); count++; } else continue; } printf("the same number is %d ", count); return 0; }

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

        Why not just use straight character comparisons rather than this complex mathematics:

        foreach character c in usersguess
        begin
        if c in secretnumber
        then
        save c in savearray
        end
        // report the contents of savearray

        It's time for a new signature.

        W 1 Reply Last reply
        0
        • L Lost User

          Why not just use straight character comparisons rather than this complex mathematics:

          foreach character c in usersguess
          begin
          if c in secretnumber
          then
          save c in savearray
          end
          // report the contents of savearray

          It's time for a new signature.

          W Offline
          W Offline
          wbgxx
          wrote on last edited by
          #4

          could you give me detail??Thanks

          L 1 Reply Last reply
          0
          • W wbgxx

            could you give me detail??Thanks

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

            wbgxx wrote:

            could you give me detail?

            It's in my previous message; a simple loop comparing each character in one string with the characters of another string. If you are not able to convert that to C++ then I suggest you spend a little more time with your study guides.

            It's time for a new signature.

            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