Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
https://www.hackerrank.com/challenges/chessboard-game-again-1
I have tried the above question in the following manner but the answers are evaluated as wrong.(I am not asking for a solution, but I am asking for the defects in the approach);
my code (please ignore the c99 errors)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int numofmov = 0;
int issafe(int a,int b){
if(a>=0 && a<15 && b>=0 && b<15)
return 1;
return 0;
}
void move(int board[][15]){
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
if(board[i][j]>0){
board[i][j]--;
if(issafe(board,i-2,j+1)==1) {
numofmov++;
board[i-2][j+1]++;
}
if(issafe(board,i-2,j-1)==1) {
numofmov++;
board[i-2][j-1]++;
}
if(issafe(board,i+1,j-2)==1) {
numofmov++;
board[i+1][j-2]++;
}
if(issafe(board,i-1,j-2)==1) {
numofmov++;
board[i-1][j-2]++;
}
}
}
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
scanf("%d",&t);
while(t--){
int k;
scanf("%d",&k);
int board[15][15];
for(int j=0;j<15;j++){
for(int h=0;h<15;h++){
board[j][h]=0;
}
}
for(int i=0;i<k;i++){
int x,y;
scanf("%d %d",&x,&y);
board[x-1][y-1]++;
}
int bro=0,mov=numofmov;
while(bro==0){
move(board);
if(numofmov==mov){
bro++;
printf("Second\n");
break;
}
mov = numofmov;
move(board);
if(numofmov==mov){
bro++;
printf("First\n");
break;
}
mov = numofmov;
}
}
return 0;
}
My approach is to go on making all the moves possible for all the coins until we come to a point when no moves are possible. But this is giving wrong answers in some test cases.
You are asking what is wrong with this approach ?
"My approach is to go on making all the moves possible for all the
coins until we come to a point when no moves are possible. But this is
giving wrong answers in some test cases."
I didn't read your code, but I can say that the main issue is your approach itself. You are thinking of this problem as a brute-force (make all possible move paths, and see who is winning). The number of possible moves can be arbitrarily large, checking is moves lead to a win is infinitely slow. In reality it is either dynamic-programming, or even more relevant game-theory problem.
Think about it this way. Does the starting positions uniquely identifies the winner of this game? What if I change the initial position of a single coin, will the winner change too?
Best way to approach this kind of problems is to simplify it. Assume that there is only a single board with a single coin, positioned at (x,y). Now notice, that after each move of a coin from position (x,y) to position (a,b), the following is true a+b<x+y. So if (x,y) is one of (1,1),(1,2),(2,1),(2,2), player making the move already lost. So my goal is to make sure my opponent will be making a move from already lost position, and if I can do it, I am in winning position. If u follow the same logic, you will realise that this approach will uniquely identify if position is winning or losing. So for any position we can answer if it is winning or losing simply building the grid of answers by going backwards from (1,1) to (15,15).
Now what would u do if the number of boards is more than one? You need to dig into the game theory, in particular Grundy numbers and how they relate to Nim games. I would suggest you to check the following links for further information:
https://en.wikipedia.org/wiki/Nim
https://en.wikipedia.org/wiki/Nimber
https://www.topcoder.com/community/data-science/data-science-tutorials/algorithm-games/
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define size 20000000
int prim[size];
int i, zahl, zaehler, erg;
int sieve(int zahl, int prim[], int zaehler) {
if(zahl == 2000000)
return 1;
for(i=0; i<=zaehler; i++) {
erg = zahl%prim[i];
if(erg==0) {
zahl++;
return sieve(zahl, prim, zaehler);
}
}
zaehler++;
prim[zaehler]=zahl;
zahl++;
printf("%d\n", prim[zaehler]);
return sieve(zahl, prim, zaehler);
}
int main(){
zaehler = 0;
zahl = 2;
for(i=0;i<size;i++)
prim[i]=2;
sieve(zahl, prim, zaehler);
}
When trying to calculate prime numbers, when i run this code, it always crushes at the number 64901.
What might be the problem?
Ironically, this is literally a stack overflow due to recursion. You can make your stack large (which will only delay the issue), or change from a recursive solution to an iterative one.
(and for what it's worth, some debuggers won't be able to help you in this situation. And it's very difficult to beginners in C to understand what is going wrong until the first time they hit this problem. So congrats! You're leveling up in C)
A cheap way to verify it's indeed a stack overflow is to create extra memory on your stack in the recursive function and see if the number it crashes on changes from 64901. My guess is if you put like char dummy[2048] in there, it will crash much sooner.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have for loop which makes falling effect in matrix. I am generating numbers in the first row of matrix and the fall.
#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>
void hra(){
.....
do {
for(int i = V; i > 0; i--) {
for(int j = 0; j < S; j++) {
mat1[i][j] = mat1[i - 1][j];
}
}....}
}
But now its too quick. When i use Sleep() it will slow down everything (user input etc..)
Is there a way to slow down only this loop (and gradually make it quicker)?
//
I am sorry I should have mentioned that the user sees the falling numbers and have to interact with them (the numbers fall at the bottom of the matrix where they get stored or they get erased by user). So I want them to "fall" slowly so the user can see them and decide which ones he want or dont. And V is 10 and S 4.
The solution you are looking for is multi-threading. Have the main thread do the calculations, have another thread do the graphics and a third thread handle to user input.
usleep() takes a time in microseconds as parameter, you could put a variable as value and then decrement it. Per example if you want to slow down this for:
for(int i = V; i > 0; i--)
you could add:
usleep(x * i);
in it. (Where x is whatever you want.) Feel free to use any equation you want to choose the way it will slow down.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I had to write a piece of code which would search a given value in an array.
I made this piece of code, which works:
#include <stdio.h>
int index_van(int searchedValue, int array[], int lengthArray)
{
int i ;
for (i = 0; i < lengthArray; i++)
{
if (array[i] == searchedValue)
{
return i;
}
}
return -1;
}
int main()
{
int array2 [] = {0, 1, 3, 4, 5, 2};
printf("%i", index_van(2, array2, 6));
}
With the correction (the teacher put up online) of this exercise the notes of my teacher were:
You have to quit the moment you have found your value ,so you can't search through the entire table if you have found your value already. A for-loop therefore isn't tolerated.
Even if the for-loop has an extra built-in condition, THIS ISN'T STYLISH!
// One small note ,she was talking in general . She hasn't seen my version of the exercise.
So my question to you guys is, is my code really 'not done' towards professionalism and 'style' ?
I think she's implying that you should use a while loop because you don't know how many iterations it will take to get you what you're looking for. It may be an issue of her wanting you to understand the difference of when to use for and while loops.
"...Even if the for-loop has an extra built-in condition..."
I think this right here explains her intentions. A for loop would need a built-in condition to exit once it's found what it's looking for. a while loop already is required to have the condition.
There is nothing wrong with your code. I have no idea if using a for loop is less stylish than using another, but stylish is a very subjective attribute.
That being said, don't go to your teacher and tell her this. Do what she says, a matter like this is not worth contradicting your teacher for. Most likely this is just a way to teach you how while loops work.
After accept answer:
I've posted this to point out sometimes there is so much discussion of "style", that when a classic algorithmic improvement is at hand, it is ignored.
Normally a search should work with a const array and proceed as OP suggest using some loop that stops on 2 conditions: if the value was found or the entire array was searched.
int index_van(int searchedValue, const int array[], int lengthArray)
But if OP can get by with a non-const array, as posted, then the loop is very simple and faster.
#include <stdlib.h>
int index_van(int searchedValue, int array[], int lengthArray) {
if (lengthArray <= 0) {
return -1;
}
int OldEnd = array[lengthArray - 1];
// Set last value to match
array[lengthArray - 1] = searchedValue;
int i = 0;
while (array[i] != searchedValue) i++;
// Restore last value
array[lengthArray - 1] = OldEnd;
// If last value matched, was it due to the original array value?
if (i == (lengthArray - 1)) {
if (OldEnd != searchedValue) {
return -1;
}
}
return i;
}
BTW: Consider using size_t for lengthArray.