C++基礎(chǔ)(lockerdoors鎖門(mén)的問(wèn)題)
時(shí)間:2008-10-04 18:54:00 來(lái)源:無(wú)憂考網(wǎng) [字體:小 中 大]
There are n lockers in a hallway numbered sequentially from 1 to n. Initially, all the locker doors are closed. You make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2, ..., n, you toggle the door of every ith locker: if the door is closed, you open it, if it is open, you close it. For example, after the first pass every door is open; on the second pass you only toggle the even-numbered lockers (#2, #4, ...) so that after the second pass the even doors are closed and the odd ones are opened; the third time through you close the door of locker #3 (opened from the first pass), open the door of locker #6 (closed from the second pass), and so on. After the last pass, which locker doors are open and which are closed? How many of them are open?
Your task is write a program to output How many doors are open after the last pass? Assumptions all doors are closed at first.
Input
a positive numbers n, total doors. n<=100000
Output
a positive numbers ,the total of doors opened after the last pass.
#include
using namespace std;
int locker_doors(int n)
{
int num_open=0;
bool *array=new bool [n];
for(int i=1;i<=n;i++)
array[i]=false;
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if (j%i==0) array[j]= (array[j]==true)?false:true;
}
}
for(i=1;i<=n;i++)
if(array[i]==true) num_open++;
return num_open;
}
int main()
{
int n;
//cout<<"enter n:"< cin>>n;
cout<<"open doors'number:"< return 0;
}
Your task is write a program to output How many doors are open after the last pass? Assumptions all doors are closed at first.
Input
a positive numbers n, total doors. n<=100000
Output
a positive numbers ,the total of doors opened after the last pass.
#include
using namespace std;
int locker_doors(int n)
{
int num_open=0;
bool *array=new bool [n];
for(int i=1;i<=n;i++)
array[i]=false;
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if (j%i==0) array[j]= (array[j]==true)?false:true;
}
}
for(i=1;i<=n;i++)
if(array[i]==true) num_open++;
return num_open;
}
int main()
{
int n;
//cout<<"enter n:"< cin>>n;
cout<<"open doors'number:"< return 0;
}
- 2009年9月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2009年3月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2008年9月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2008年4月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2011年3月全國(guó)計(jì)算機(jī)二級(jí)Java考試真題及答案
- 2010年9月全國(guó)計(jì)算機(jī)二級(jí)Java考試真題及答案
- 查看計(jì)算機(jī)等級(jí)考試全部真題>>
- 2024年9月上海計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月全國(guó)計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月全國(guó)計(jì)算機(jī)一級(jí)考試成績(jī)查詢?nèi)肟冢ㄒ验_(kāi)
- 2024年9月江蘇計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月浙江省全國(guó)計(jì)算機(jī)等級(jí)考試(NCRE)
- 2024年9月浙江計(jì)算機(jī)二級(jí)考試準(zhǔn)考證打印入口(9
- 2024年9月江蘇計(jì)算機(jī)等級(jí)考試成績(jī)查詢時(shí)間及查分
- 2024年9月浙江計(jì)算機(jī)二級(jí)成績(jī)查詢時(shí)間及查分入口