少妇AV一区二区三区无|久久AV电影一区三|日本一级片黄色毛片|亚洲久久成人av在线久操|黄色视频在线免费看呀一区二区|综合精品视频精品久久久浪朝|亚洲午夜成人资源|欧美黄色一级片黑寡妇|内射无毛少妇特写|无码农村夜晚偷拍啪啪

C++基礎(chǔ)(lockerdoors鎖門的問題)

時間:2008-10-04 18:54:00   來源:無憂考網(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;
  }