Jiudu _ Question 1356: children's games (the last number left in the circle)

Source: Internet
Author: User
Description:
Every year on Children's Day, jobdu prepares some small gifts to visit children in the orphanage. This year is also the case. As a veteran of jobdu, HF has also prepared some games. Among them, there is a game like this: first, let the children form a big circle. Then, he randomly specified a number M to let the number of children numbered 1 start to report. Every time the child who shouted to M wants to sing a song, he can pick any gift in the gift box and no longer return to the circle. Starting from his next child, he will continue 1... M reports .... so proceed .... until the last child is left, you don't need to perform the performances, and you get jobdu's rare "Detective Conan" collector edition (limited places !! ^_^ ). Which of the following children will get this gift?
Input:
Multiple groups of data are input.
A row of data in each group, which contains two integers, n (0 <= n <= 1,000,000), m (1 <= m <= 1,000,000), N, M indicates the number of children (Number 1 .... n-1, n) and the number m specified by HF (as described above ). If n = 0, end the input.
Output:
Corresponding to each group of data, the number of the children who finally won the grand prize is output.
Sample input:
1 10
8 5
6 6
0
Sample output:
1
3

4

// I Feel Like I cannot analyze its internal logic at all, but I really cannot think of it.

#include <stdio.h>int main(){    int n, m, i, s = 0;    while(scanf("%d",&n)&&n!=0)    {        s=0;        scanf("%d",&m);        for (i = 2; i <= n; i++)            s = (s + m) % i;        printf ("%d\n", s+1);    }     }/**************************************************************    Problem: 1356    User: hndxztf    Language: C++    Result: Accepted    Time:290 ms    Memory:1020 kb****************************************************************/

// It is a natural idea, but the time is hard to hurt. No table header is created, and the Circular linked list is deleted... There are still a lot of gains

# Include <cstdio> # include <cstdlib> using namespace STD; typedef struct lnode {struct lnode * Next; int data ;}* linklist; linklist createlist (linklist & L, int N) {linklist q = NULL, P = NULL; L = (linklist) malloc (sizeof (linklist *); L-> DATA = 1; L-> next = L; P = L; For (INT I = 2; I <= N; I ++) {q = (linklist) malloc (sizeof (linklist *)); q-> DATA = I; q-> next = L; P-> next = Q; P = Q;} return l;} int findnum (linklist L, int m) {linklist Q = NULL, P = L; int COUNT = 1; while (p-> next! = P) {If (count <m) {P = p-> next; count ++;} else if (COUNT = m) {q = p-> next; p-> DATA = Q-> data; // assign the values of the subsequent nodes to the previous node to delete the current node // P-> next = Q-> next is feasible in the cyclic linked list; // Delete Q; // q = NULL; Count = 1 ;}} return p-> data ;}int main () {linklist L = NULL; int n, m; while (scanf ("% d", & N) & n! = 0) {scanf ("% d", & M); L = createlist (L, n); printf ("% d \ n", findnum (L, m);} return 0 ;} /*************************************** * *********************** problem: 1356 User: hndxztf language: C ++ result: time limit exceed ************************************* ***************************/



Jiudu _ Question 1356: children's games (the last number left in the circle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.