iOS面試常見題

來源:互聯網
上載者:User

標籤:arc   i+1   define   post   第一個   排除   data   ios   count   

1.耶穌有13個門徒,當中有一個就是出賣耶穌的叛徒,請用排除法找出這位叛徒:13個人圍坐一圈,從第一個人開始迴圈報數,數到三排除,最後剩下的人就是叛徒

int people[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

int count = 0;//用來記錄報數 

int number = 13;//記錄活著的人數 

int i = 0;//記錄第幾個人正在報數

while (number > 1) {     

if (people[i] != 0) {        

count++; //假設a[i ]沒有被剔除掉,報數有效    

} if (count == 3) {       

    people[i] = 0;//剔除掉,      

     count = 0;//報數清零        

   number--;//存活人數減一     

  }    

   i++;//記錄報數的下一個人    

   if (i == 13) {        

   i = 0;// 當超出範圍時,馬上將報數人數置為第一人    

   }  

}    for (int i = 0; i < 13; i++) {     

  if (people[i] > 0) {         

  printf("叛徒是第%d人 ", people[i]);    

   } 

  }




2.有1000000個數,每一個數的取值範圍都是0~999999,找出當中反覆的數
#define COUNT 1000000

int main(void)
{
            inta[COUNT] = {0};
   for (int i = 0; i < COUNT; i++) {
       int number = arc4random()%COUNT;//把隨機數作為下標,值作為出現次數
       a[number - 1] ++;
    }
   //輸出反覆的數字以及反覆次數
   for(int i = 0 ;i < COUNT;i++){
       if (a[i] > 1) {
           printf("%d repeats %d times\n",i+1,a[i]);
       }
    }
            return0;
}

iOS面試常見題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.