POJ 2359 Questions (约瑟夫问题)

本文探讨了约瑟夫问题在编程竞赛中的实际应用,通过具体实例讲解了如何使用约瑟夫算法来解决特定类型的问题。文章详细解释了算法流程,并提供了一个C++代码示例,用于解答编程竞赛中的一道题目。
Questions
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1208 Accepted: 441

Description

Holding a collegiate programming contest is a very exhausting work. There is a well-known proverb that one fool can ask so many questions that a hundred clever men will not answer. And during a collegiate programming contest questions are asked by one hundred clever people. 

The jury of the Third Urals Collegiate Programming Contest being clever enough has found a simple way to make its work easier. We have invented a simple algorithm that will help us answer ALL your numerous questions! Moreover, this algorithm guarantees that the same questions will have the same answers (this would be hardly possible, if we would undertook such a task ourselves). According to this algorithm a member of the jury starts to delete characters of the question in the following order: 
  1. Starting from the first character he or she counts out N-1 characters (spaces, punctuation marks etc. are considered to be characters too) and deletes the Nth character. 
  2. If a string ends the count continues from the beginning of the string. 
  3. After deleting a character the count restarts from the character that would be the (N+1)-st in the previous count. 
  4. If the last remaining character is a question-mark ("?") then the answer to the question is "Yes". If it is a space then the answer is "No". Any other character will lead to "No comments" answer.

You should help the jury and write a program that will do a hard work of answering your questions tomorrow. The number N is secret and will not be announced even after the end of the contest. Your program should use N=1999. 

For example, taking a string "Is it a good question?" (its length is 22) the characters will be counted in the following way: "Is it a good question?Is it ... quest" and "i" will be deleted. Then the count restarts from "on?Is it..." etc., until "s" will be left (thus the answer is "No comments", as usual).

Input

The input is a question, that is any text file containing at least one character (end of line is not a character). Each character of the input (excepting the ends of lines) is a part of the question. 
The size of the input file is not more than 30000.

Output

The answer.

Sample Input

Sample input #1
Does the jury of this programming contest use the
algorithm described in this problem to answer my questions?

Sample input #2
At least, will anybody READ my question?

Sample input #3
This is
UNFAIR!

Sample Output

Sample output #1
Yes

Sample output #2
No

Sample output #3
No comments

Hint

there are no spaces in the sample inputs except for those between words in one line. Thus the first question contain 108 characters, the second — 40 and the third — 14.

Source

 
 
题意 :纯粹的约瑟夫水题。对输入的字符串,按1999报数,若最后的胜利者为cnt,根据cnt对应的字符输出不同的结果。
算法 :约瑟夫问题。
 
#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

const int N=1999;
const int maxn=30010;
char ch[maxn];

int main(){

    //freopen("input.txt","r",stdin);

    string s;
    int res=0;
    while(gets(ch)){     // get(ch):直到遇到新行(\n)或到达EOF
        s+=ch;
    }
    int len=s.length();
    for(int i=2;i<=len;i++)
        res=(res+N)%i;
    if(s[res]=='?')
        printf("Yes\n");
    else if(s[res]==' ')
        printf("No\n");
    else
        printf("No comments\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/jackge/archive/2013/04/30/3052371.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值