| |

VerySource

 Forgot password?
 Register
Search
View: 782|Reply: 2

Find algorithm (delete a number every 2)

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-31 12:20:01
| Show all posts |Read mode
A circle reads 1 to 100, starting from 1, and deleting a number every 2 in a clockwise direction. How many times can I delete the number 1? What was the last deletion?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-11 23:00:01
| Show all posts
#include <stdio.h>
int main ()
{
typedef struct Node
{
int data;
Node * next;
} Node;

Node * head, * tail;

head = new Node;
head-> data = 1;
head-> next = NULL;
tail = head;

for (int i = 2; i <= 100; i ++)
{
Node * temp = new Node;
temp-> data = i;
temp-> next = NULL;
tail-> next = temp;
tail = temp;
}
tail-> next = head;

Node * temp = head;
Node * temp1, * temp2;
int count = 0;
while (1)
{
temp1 = temp-> next-> next;
temp2 = temp1-> next;
temp1-> next = temp2-> next;
temp = temp1;
count ++;

if (temp2-> data == 1)
{
printf ("Delete 1\n after% d times", count);
// break;
}
if (count == 100)
{
printf ("The last deleted is:% d\n", temp2-> data);
break;
}
delete temp2;
}
delete temp2;
return 0;
}
 The result is: after 98 deletions of 1, the last deleted is 92.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-12 16:15:01
| Show all posts
Exercise the logical thinking ability of beginners
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list