100085G GCD Guessing Game

传送门

题目大意

给定一个数N,现在又一个数x,在1~N之间,现在每次可以猜一个数a,返回gcd(x,a),问说最少猜几次可以确定x。

分析

这个题应该可以算是贪心,但是没人知道这样为啥是对的(雾),我们现在来感性认识一下,我们知道对于任意一个数都可以写p1e1p2e2 ... 的形式,所以我们在每一次询问都可以确定有些p是否存在,如果存在我们在来确定它对应的e,这样只需要两次,而我们感性思考可以猜出没猜到一个p并确认一个e用去两次却可以直接否定很多其它的p,所以猜的次数一定小于等于确认所有p的次数,所以便可以按照下面的代码进行求解。

代码

#include<bits/stdc++.h>
using namespace std;
int dq[11000],L=1,R=0;
inline bool is(int x){
      int i;
      for(i=2;i*i<=x;i++)
        if(x%i==0)return 0;
      return 1;
}
int main(){
      freopen("gcd.in","r",stdin);
      freopen("gcd.out","w",stdout);
      int n,i,ans=0;
      scanf("%d",&n);
      for(i=2;i<=n;i++)
        if(is(i))
          dq[++R]=i;
      while(L<=R){
          while(dq[L]*dq[R]<=n){
            L++;
            dq[R]*=dq[L];
          }
          R--;
          ans++;
      }
      cout<<ans<<endl;
      return 0;
}

转载于:https://www.cnblogs.com/yzxverygood/p/9406556.html

Sure, I can help you with that! Here's an example implementation of the GuessingGame in Java: ```java // GuessingGame.java import java.util.Scanner; public class GuessingGame { private String secretWord; private int guessesLeft; public GuessingGame(String secretWord, int maxGuesses) { this.secretWord = secretWord; this.guessesLeft = maxGuesses; } public void play() { String guessedWord = ""; for (int i = 0; i < secretWord.length(); i++) { guessedWord += "*"; } Scanner scanner = new Scanner(System.in); while (guessesLeft > 0) { System.out.println("Guess a letter (" + guessesLeft + " guesses left):"); String guess = scanner.nextLine(); // Check if guess is correct boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == guess.charAt(0)) { guessedWord = guessedWord.substring(0, i) + guess + guessedWord.substring(i + 1); correctGuess = true; } } if (!correctGuess) { guessesLeft--; } System.out.println("Guessed word: " + guessedWord); if (guessedWord.equals(secretWord)) { System.out.println("Congratulations, you guessed the word!"); return; } } System.out.println("Sorry, you ran out of guesses. The word was " + secretWord); } } ``` And here's an example usage in a main class: ```java // Main.java public class Main { public static void main(String[] args) { GuessingGame game = new GuessingGame("house", 10); game.play(); } } ``` In this implementation, the `GuessingGame` class takes two parameters: the secret word to guess, and the maximum number of guesses allowed. The `play` method runs the game loop, prompting the user for guesses and updating the guessed word accordingly. If the guessed word matches the secret word, the game is won; if the user runs out of guesses, the game is lost.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值