The problem is about saving James Bond. My code gets the right answer only in some special occasions. I spent so much time but I didn't find any mistake in my algorithm. I need your help. Thanks.
Here is the request of the problem.
.......................................
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (<=100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x, y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, print in a line "Yes" if James can escape, or "No" if not.
Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No
.......................................
Here is my code.
#include <stdio.h>
#include <malloc.h>
#include <math.h>
typedef struct Node{
int x;
int y;
} Position;
int result;
Position beast[1000];
int visited[1000];
int NumOfB, JumpAb;
void dfs(int i){
int j;
if (Available(i)){
visited[i]=1;
if (Save(i)) {
result = 1;
printf("Yes");
}
}
for(j = 0;j<NumOfB;j++)
if(!visited[j])
dfs(j);
}
int Available(int i){
int j;
double d_x,d_y;
for (j=0;j<NumOfB;j++){
d_x = beast[i].x - beast[j].x;
d_y = beast[i].y - beast[j].y;
if (((d_x*d_x+d_y*d_y)<JumpAb*JumpAb)&& visited[j]==1){
return 1;
}
}
return 0;
}
int Save(int i){
if ((abs(50-abs(beast[i].x))<=JumpAb)||((abs(50-abs(beast[i].y)))<=JumpAb))
return 1;
else
return 0;
}
int FirstJump(int i){
if (((beast[i].x*beast[i].x)+(beast[i].y*beast[i].y))<=JumpAb*JumpAb){
return 1;
} else {
return 0;
}
}
int main(){
result = 0;
scanf("%d %d",&NumOfB,&JumpAb);
int i;
Position* p =NULL;
for (i=0;i<NumOfB;i++){
p = (Position*)malloc(sizeof(Position));
scanf("%d %d",&p->x,&p->y);
}
for (i=0;i<NumOfB;i++){
visited[i] = 0;
}
for(i = 0;i<NumOfB;i++){
if(!visited[i] && FirstJump(i)){
visited[i] = 1;
dfs(i);
}
}
if (result==0){
printf("No");
}
return 0;
}
Related
This question already has answers here:
Pascal's Triangle in C
(4 answers)
Closed 1 year ago.
I tried to code a program that will give the pascal triangle without using arrays and keeping in mind the formula that each element of the pascal triangle can be calculated as n choose k" and written like this:
n choose k = n! / k!(n-k)!
(for both n and k starting from 0)
so I also had to define the factorial function and it worked for the first 14 lines.
but in line 15 and more my numbers started to decrease and became also negative but I don't understand why this happened.
Here is the code:
#include <stdio.h>
int factorial(int a);
int main()
{
int row, j, i, space, tot;
scanf("%d", &row);
space=row;
for(i=0; i<row; i++)
{
for(space=0; space<row-i; space++)
{ printf(" "); }
for (j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{ tot=1; }
else
{
int n=factorial(i);
int k=factorial(j);
int z=factorial(i-j);
tot= n/(k*z);
}
printf("%6d", tot);
}
printf("\n");
}
}
int factorial(int a)
{
int fact=1;
for (int m=1; m<=a; m++)
{ fact*=m; }
return fact;
}
this is my output in line 15:
1 0 1 5 14 29 44 50 44 29 14 5 1 0 1
but the actual output should be this:
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
I couldn't find the problem so I would be happy and thankful if anyone could help me.
If you don't care much about computation time then you could compute coefficient directly from their recursive definition.
int pascal(int x,int y) {
if (x==0 || y==0) return 1;
return pascal(x-1,y)+pascal(x,y-1);}
These are the instructions my professor gave me for a Lab in C
--SOLVED--
struct singlecard
{
int cardnum;
char face;
char suit;
};
Your program must have the functions named as follows:
Main() - Calls "LoadDeck()" and "DealCards()."
LoadDeck() will fill just the "cardnum" of the array with a unique number between 1 and 52. It will do this by selecting a random number and then calling the function "CheckDup()" to see if the number is a duplicate. It will then call "LoadFace()" and "LoadSuit()."
CheckDup() will receive the trial number and the deck of cards as input, and will return back a Boolean.
LoadFace() will go through the deck and put the appropriate value in "face" of the array by using just the "cardnum" the modulus operator to extract the value from the string "A23456789TJQK."
LoadSuit() will go through the deck and put the appropriate suit value in "suit" by using a method similar to "LoadFace()" where the suit string is "HDCS."
DealCards() will display the cards.
**My question is, how can I check that there are no duplicate faces in each suit without a ton of if statements? Please disregard the fact that I'm not sending the "cardnum" as a parameter to the functions LoadFace and LoadSuit, Also I'm using srand(1) for debugging purposes. So far the output has unique card number and the correct amount of faces and suits (13, 4), but I'm not sure how I can insert a nonduplicate element from here.Any advice would be helpful. Also, a simple assign and shuffle of an array isn't allowed :( **
EDIT checkDup and LoadDeck currently load the deck with a unique card number but not a unique card face and suit. It just counts the number of A faces 2 faces 3 faces King faces etc. It also makes sure that there are 13 cards in each suit. I want to insert unique faces and suits into the stuct array so that I don't have say two 7 of spades.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
struct singlecard {
int cardnum;
char face;
char suit;
};
int i, deckSize = 52;
char suits[] = {"HDCS"};
char faces[] = {"A23456789TJQK"};
void DealCards(struct singlecard Deck[]) {
//SortCards();
printf("\n\n");
for (i = 0; i < deckSize; i++) {
if ((i + 1) % 4 == 0) {
printf("[%d %c-%c] %d\n", Deck[i].cardnum, Deck[i].face, Deck[i].suit, i + 1);
} else {
printf("[%d %c-%c] ", Deck[i].cardnum, Deck[i].face, Deck[i].suit);
}
}
}
int CheckDupe(struct singlecard Deck[],int n) {
int check = 0, j;
for (j = 0; j < deckSize; j++) {
if (n == Deck[j].cardnum || n == 0) {
return check = 1;
}
}
return check;
}
void LoadSuit(struct singlecard Deck[],int n)
{
Deck[i].suit = suits[(n-1) % 4];
}
void LoadFace(struct singlecard Deck[],int n) {
Deck[i].face = faces[(n-1) % 13];
}
void LoadDeck(struct singlecard Deck[]){
srand(time(NULL));
for (i = 0; i < deckSize;) {
int random_number = rand() % 53;
if (CheckDupe(Deck,random_number) == 0) {
Deck[i].cardnum = random_number;
LoadFace(Deck,Deck[i].cardnum);
LoadSuit(Deck,Deck[i].cardnum);
i++;
}
}
}
int main(){
struct singlecard Deck[52];
LoadDeck(Deck);
DealCards(Deck);
return 0;
}
Current Output
[5 5-H] [36 T-S] [6 6-D] [29 3-H] 4
[12 Q-S] [19 6-C] [25 Q-H] [13 K-H] 8
[42 3-D] [38 Q-D] [14 A-D] [22 9-D] 12
[16 3-S] [40 A-S] [51 Q-C] [35 9-C] 16
[24 J-S] [4 4-S] [20 7-S] [43 4-C] 20
[31 5-C] [9 9-H] [11 J-C] [48 9-S] 24
[49 T-H] [18 5-D] [41 2-H] [21 8-H] 28
[50 J-D] [52 K-S] [3 3-C] [27 A-C] 32
[39 K-C] [8 8-S] [33 7-H] [23 T-C] 36
[44 5-S] [17 4-H] [32 6-S] [45 6-H] 40
[30 4-D] [28 2-S] [2 2-D] [7 7-C] 44
[26 K-D] [34 8-D] [15 2-C] [47 8-C] 48
[10 T-D] [37 J-H] [1 A-H] [46 7-D] 52
The problems you are facing is because you've not read and followed your homework instructions properly, notably this bit:
LoadFace() will go through the deck and put the appropriate value in
"face" of the array by using just the "cardnum" the modulus operator
to extract the value from the string "A23456789TJQK."
Imagine you've listed out each card in order like so with the card number.
1. Ace of Hearts
2. Two of Hearts
....
14. Ace of Diamonds
15. Two of Diamonds
Do you notice a pattern? Every 13th card has the same face. The modulus operator (aka %) can be used to find out which of those 13 faces a specific value of cardnum relates to like this. cardnum % 13 will always be between 0 and 12. Because your first card starts at 1, you need to subtract 1 first before doing getting modulus. Your LoadFace function then becomes this.
void LoadFace() {
for (i = 0; i < deckSize;) {
Deck[i].face = faces[(Deck[i].cardnum-1) % 13];
}
}
If you want to know if two cards have the same face based on their cardnum you can just compare Deck[a].cardnum % 13 and Deck[b].cardnum % 13.
So long as you don't put the same cardnum in twice, you know your deck will always contain unique cards.
CheckDup will receive the trial number and the deck of cards as input, and will return back a Boolean.
which would translate to the following declaration
bool CheckDup( const int trial, const struct singlecard deck[], const unsigned int deckSize );
And should check the deck if the trial card do not exists yet ( probably true if a duplicate is found ), probably by iterating through the deck.
This is what I understood from your question.
you want to find out, using checkDup() if that card has already been dealt
you could maintain an array (or list whatever) of the card numbers that have been dealt. Then simply search through that array every time checkDup is called.
I need to write a program which prints the conversion table from feet and inches to centimetres. The numbers printed in row i (counting from zero), column j (counting from zero) of the table should be the cm equivalent of i feet and j inches. i should go from 0 to 7, and j from 0 to 11. Each column should be five characters wide, and the cm figures should be rounded to the nearest integer.
The example of required output is given below:
0 3 5 8 10 13
30 33 36 38 41
61 64 66 69 71
91 94 97 99 102
The code I have prints only one row of inches and column of feet but I don't know how to make into table without producing lots of irrelevant repetitions.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int cm,p;
for (i=0; i<= 11; i++) {
cm =round(i * 2.54);
printf ("%5d",cm);
}
for (j=0; j<=7; j++) {
p =round(j* 12.0 * 2.54);
printf ("%5d\n",p);
}
return 0;
}
This produces:
0 3 5 8 10 13 15 18 20 23 25 28 0
30
61
91
122
152
183
213
What am I doing wrong?
You have one loop after the other. What you need to do is run through the inches loop every iteration of your feet loop. What you get is nested loops:
#include <stdio.h>
int main()
{
for (int feet = 0; feet <= 7; ++feet) {
for (int inches = 0; inches < 12; ++inches) {
int microns = (feet * 12 + inches) * 25400;
int rounded_cm = (microns + 5000) / 10000;
printf("%5d", rounded_cm);
}
puts("");
}
}
I've made some other changes in my version; you're encouraged to study it and understand why it does what it does (read the man page for puts(), for example). Don't just copy it and hand it in - it will be obvious it isn't your code.
An alternative approach is to use a single loop (in inches), and insert a newline when we reach the 11th inch in each foot:
#include <stdio.h>
int main()
{
for (int i = 0; i < 96; ++i) {
printf("%4d%s",
(i * 25400 + 5000) / 10000,
i%12==11 ? "\n" : " ");
}
}
(You'll want to give meaningful names to your constants; the above is written in a "code-golf" style).
Whatever you do, don't be tempted to avoid multiplying by instead adding 2.54 repeatedly in the loop. Floating-point numbers are not exact, and addition will accumulate the error.
OP needs to put the "inches" loop inside the "foot" loop as well answered by others. #Toby Speight #VHS
Code could do its "round to nearest" via the printf() statement by using "%5.0f" to control the output width and rounding.
Let code use foot/inch instead of i/j #KevinDTimm for clarity.
#include <stdio.h>
#define INCH_PER_FOOT 12
#define CM_PER_INCH 2.54
int main(void) {
// go from 0 to 7, and ...
for (int foot = 0; foot <= 7; foot++) {
// from 0 to 11
// for (int inch = 0; inch < INCH_PER_FOOT; inch++) { is more idiomatic
for (int inch = 0; inch <= 11; inch++) {
printf("%5.0f", (foot * INCH_PER_FOOT + inch) * CM_PER_INCH);
}
puts("");
}
}
Output
0 3 5 8 10 13 15 18 20 23 25 28
...
213 216 218 221 224 226 229 231 234 236 239 241
You are running your loops backwards. First you need to run through feet and then through inches. But you are having it the other way round. Check the following snipped and compare it with your code and try to understand what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // for rounding of a number
int main()
{
int i,j;
int cm,p;
for(i=0; i<=7;i++) {
for(j=0;j<=11;j++) {
cm = round(i*30.48 + j*2.54);
printf ("%5d",cm);
}
printf("\n");
}
return 0;
}
I have one little project where i have to program one algorithm that calculates the two palindrome of a number.
Per example : if the input is 367 the result is 367763 and 763367.
I know how to check if the number is a palindrome. But i have to create the two palindrome and it's a little different and i can't find the answer.
Please before unvote, i am a noob guy in programming that is hopping to learn something, i already tried to solve the problem and searched a lot.
Here is my code to check if number is palindrome:
#include <stdio.h>
int main()
{
int num, reverse_num=0, remainder,temp;
printf("Enter an integer: ");
scanf("%d", &num);
temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp/=10;
}
if(reverse_num==num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
return 0;
}
Your code already has both num and reverse_num. All you have to do is print them one after the other in your if else statement. That's for "instant gratification".
if(reverse_num==num) {
printf("palindrome1 = palindrome2 = %d%d\n", num, num);
}else{
printf("palindrome1 = %d%d\n",num, reverse_num);
printf("palindrome2 = %d%d\n",reverse_num, num);
}
If you really want to calculate the palindromes as actual integers instead, it is just a few more lines of code. Add one counter for keeping track of decimal places while computing reverse_num... We can easily post the code here, but it's a good and simple exercise in learning C and you'll get much more of it if you do it yourself.
Of course, there are other ways of achieving the result, as the other answer and comments suggest. This answer is based on your code input.
Please, also be aware that the numbers of form a*10^n, or more generally … + 0*10^0 may result in some "surprisingly looking" palindromes, at least in the terms how they are defined in the OP.
Here you are.
#include <stdio.h>
struct palindrome_pair
{
unsigned long long int first;
unsigned long long int second;
};
struct palindrome_pair build_palindrome( unsigned int x )
{
const unsigned int Base = 10;
struct palindrome_pair palindrome = { x, 1 };
unsigned int y = 0;
for ( unsigned int value = x; value != 0; value /= Base )
{
y = Base * y + value % Base;
palindrome.first *= Base;
palindrome.second *= Base;
}
palindrome.first += y;
palindrome.second = palindrome.second * y + x;
return palindrome;
}
int main( void )
{
unsigned int x = 367u;
struct palindrome_pair palindrome = build_palindrome( x );
printf( "%u:\t%llu\t%llu\n\n", x, palindrome.first, palindrome.second );
const unsigned int N = 20;
for ( unsigned int i = 0; i <= N; i++ )
{
palindrome = build_palindrome( i );
printf( "%u:\t%llu\t%llu\n", i, palindrome.first, palindrome.second );
}
return 0;
}
The program output is
367: 367763 763367
0: 0 0
1: 11 11
2: 22 22
3: 33 33
4: 44 44
5: 55 55
6: 66 66
7: 77 77
8: 88 88
9: 99 99
10: 1001 110
11: 1111 1111
12: 1221 2112
13: 1331 3113
14: 1441 4114
15: 1551 5115
16: 1661 6116
17: 1771 7117
18: 1881 8118
19: 1991 9119
20: 2002 220
Halloween is round the corner and it's time for trick-or-treating. You reside at the top left corner of a n-by-n town map and heading to the halloween party located at the bottom right corner. While on your trip, you decide to visit a minimal number of houses to get treats. You have a map of town with information of the amount of treats (≥ 0) available at each location. As an example, the town map for n=3 is shown below.
6 8 2
4 5 1
3 9 10
To get the maximum treats, you will start from home (6), then head east to (8), then south to (5), then south to (9), then east to (10) and end up at the party.
So the number of treats is 6+8+5+9+10=38.
Notice that to visit a minimal number of houses, it necessitates that you either travel east or south from one house to the next until you arrive at the party. To obtain the maximum treats, track the current maximum as you visit each home.
6, 14, 2+14=16
10, 5+max(10,14)=19
3+10=13
So the program needs to be choosing the maximum value to add let's say for 10 and 14, i will choose to add 14. But I have trouble with this using for loops. Anyone can help?
1 #include <stdio.h>
2
3 #define SIZE 10
4
5 int pathmax(int map[][SIZE], int n);
6 void read(int map[][SIZE], int n);
7 int max(int x, int y);
8
9 int main(void)
10 {
11 int map[SIZE][SIZE], n;
12
13 printf("Enter n: ");
14 scanf("%d", &n);
15
16 read(map,n);
17
18 printf("Maximum: %d\n", pathmax(map,n));
19
20 return 0;
21 }
22
23 int max(int x, int y)
24 {
25 if (x > y)
26 return x;
27 else
28 return y;
29 }
30
31 int pathmax(int map[][SIZE], int n)
32 {
33 int k, i, j;
34
35 for (k = 1; k < 2*n-1; k++)
36 for (i = 0; i < n; i++)
37 for (j = 0; j < n; j++)
if(i+j==k)
{
if (i==0)
map[i][j] += map[i][j-1];
else if (j == 0)
map[i][j] += map[i-1][j];
else
map[i][j] += max(map[i-1][j], map[i][j-1];
}
}
Recursive solution, will teacher believe you wrote it?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXTREAT 10
#define GRID 3
int map [GRID][GRID];
int maxtreats;
void explore(int x, int y, int treats)
{
treats += map [y][x];
if (x == GRID-1 && y == GRID-1) { // reached bottom right?
if (treats > maxtreats) // check result
maxtreats = treats; // finish recursion
} else { // recurse
if (x+1 < GRID)
explore (x+1, y, treats); // go east
if (y+1 < GRID)
explore (x, y+1, treats); // go south
}
}
int main()
{
int x, y;
srand ((unsigned int)time(NULL)); // seed random num gen
for (x=0; x<GRID; x++) { // set up random map
for (y=0; y<GRID; y++) {
map[y][x] = 1 + rand() % MAXTREAT;
printf ("%4d", map[y][x]);
}
printf ("\n");
}
explore (0, 0, 0);
printf ("Max treats %d\n", maxtreats);
return 0;
}