find a effictive way!!
-
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; }
-
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; }
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] -
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; }
-
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 savearrayIt's time for a new signature.
-
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.