Poj1218_THE DRUNK JAILER(水题)
生活随笔
收集整理的這篇文章主要介紹了
Poj1218_THE DRUNK JAILER(水题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Description
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey,and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the
hall locking every other cell (cells 2, 4, 6, ?). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, ?). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He
repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
Given the number of cells, determine how many prisoners escape jail.
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
二、問題分析
??????? 此題屬于小弟的消暑計劃之一,通俗點就是水題。題意通俗易懂,解題沒什么難度,要的就是這樣的效果啊。哈哈哈!
三、Java代碼
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin=new Scanner(System.in);int times=cin.nextInt();for(int i=0;i<times;i++){int n=cin.nextInt();boolean[] b=new boolean[n+1];int num=0;for(int l=1;l<=n;l++){b[l]=true;}for(int m=1;m<=n;m++){for(int j=2;j<=n;j++){if(b[m] && m%j==0){b[m]=false;}else if( !b[m] && m%j==0){b[m]=true;}}}for(int j=1;j<=n;j++){if(b[j])num++;}System.out.println(num);}} }版權聲明:本文為博主原創文章,未經博主允許不得轉載。
轉載于:https://www.cnblogs.com/AndyDai/p/4734205.html
總結
以上是生活随笔為你收集整理的Poj1218_THE DRUNK JAILER(水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从零开始学C++之继承(一):公有/私有
- 下一篇: 深度技术 GHOST XP SP3 快速