The seventh session of Hunan computer Program design Contest students reported that the number of games

Source: Internet
Author: User

Game time limit: +Ms | Memory Limit:65535KB Difficulty:2
Descriptive narrative
n individuals stand in a row to play a count game. All people are numbered 1 to n from left to right. When the game started, the leftmost person reported 1. The man on his right is 2, the person with the number 3 is 3, and so on. When the person whose number is n (i.e. the right-most person) has finished n, the person whose turn is on his left (that is, the person numbered n-1) reported n+1, and then the person who was numbered N-2 reported n+2. And so on    When the leftmost person again counted off, the direction of the count turned from left to right, and so on. To prevent the game from being too boring, there is a special case when counting: Suppose that the number that should be reported includes the number 7 or a multiple of 7. He should replace the count by clapping his hands.

The following table shows the status of N=4 (X for clapping).

When the person with the number 3 is clapping for the 4th time. He actually counted to 35.

  people  1  2  3  4  3  2  1  2  3
 1  2  3  4  5  6  x  8  9
  people  4  3  2  1  2  3  4  3  2
 10  11  12  13  x  15  16  x  18
  people  1  2  3  4  3  2  1  2  3
 19  20  x   22  23  24  25  26  x
  people  4  3  2  1  2  3  4  3  2
Count X 29 30 31 32 33 34 X 36

Given N,m and K. Your task is to count the number of people who are numbered m when they clap his hands, he actually counts.
Input
The
input includes no more than 10 sets of data. One row for each group of data. Consists of three integers n,m and K (2<=n<=100, 1<=m<=n, 1<=k<=100). The input end flag is n=m=k=0.
Output
for each set of data, output one line. That is, the number of people who are numbered m, the number of the first time he actually counts.
Example input
 
 
Example output
 
 
Source

source=%e6%b9%96%e5%8d%97%e7%9c%81%e7%ac%ac%e4%b8%83%e5%b1%8a%e5%a4%a7%e5%ad%a6%e7%94%9f%e8%ae%a1%e7%ae%97%e6% 9c%ba%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e7%ab%9e%e8%b5%9b "style=" text-decoration:none; Color:rgb (55,119,188) > Hunan Seventh session of computer Program design contest for university students


A simple simulation problem: to simulate the occurrence of the situation, mainly to introduce a rule in the position of the boundary, the next count of the position is 2*n-2. (I don't know how it was introduced.)

This makes it possible to calculate the position of each number next to the count, and infer directly.

#include <cstdio> #include <cstring>bool judge (int n)//infer function, infer whether it contains 7, or a multiple of 7 {     if (!) n% 7)) return true;     while (n)    {        if (n% = = 7)            return true;        n/=;    }    return false;} int main () {    int n,m,k,i;    while (scanf ("%d%d%d", &n,&m,&k) &&n&&m&&k)    {        int count=0;        for (i=7;; i++)        {            if (judge (i))            {                int x=i% (2*n-2);//derived from the initial number                if (x==0)                 x=2*n-2;                if (X==m | | (X>=n && 2*n-x==m)) Conform to the situation of test instructions                    count++;                if (count==k) break                    ;            }        }        printf ("%d\n", I);    }    return 0;}

Also see others have a way of thinking, directly find the law, according to the rule of violence solution.

Divided into two parts to consider, the boundary situation is directly 2*n-2, the intermediate interval is two number cycle change.

#include <cstdio> #include <iostream>using namespace std;bool is7 (int n)//inferred {if (! (    n% 7)) return true;        while (n) {if (n% = = 7) return true;    n/= 10; } return false;}    int main () {int n, m, K;        while (scanf ("%d%d", &n, &m, &k)!=eof && n+m+k) {int cnt = 0, period = 2 * n-2;                    if (n = = m | | m = = 1)//Boundary condition {for (int i = m;; i + = period) if (IS7 (i)) {                    cnt++;                        if (cnt = = k) {printf ("%d\n", I);                    Break            }}} else {int next = 2-m;                for (int i = m;; Swap (Next, I))//Middle Condition {next + = period;                    if (IS7 (i)) {cnt++; if (cnt = = k) {printf ("%d\n",i);                    Break }}}}} return 0;}

A different way of thinking. Direct simulation, according to the first order, after the reverse of the simulation;

#include <cstdio> #include <cstring>bool judge (int n)//Infer {if (!     n% 7)) return true;        while (n) {if (n% = = 7) return true;    n/= 10; } return false;}    int main () {int n,m,k,i;        while (scanf ("%d%d%d", &n,&m,&k) &&n&&m&&k) {int pos=0,count=0,flag=0;            for (i=1;i<=n;i++)//sequential processing {pos++;                if (i==m) {if (Judge (POS)) count++;                   if (count==k)//search results for {printf ("%d\n", POS);                Break                }} if (I==n) {i=n-1;//in reverse processing for (; i>=1;i--)                    {pos++;                        if (i==m) {if (Judge (POS)) count++;                           if (count==k) { flag=1;                            printf ("%d\n", POS);                        Break             }}} if (flag) break; i=1;//back to Order}}} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

The seventh session of Hunan computer Program design Contest students reported that the number of games

Related Article

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.