返回列表 發帖

n763. 我愛偶數 (之偶數殺手)

n763. 我愛偶數 (之偶數殺手)



提示:
queue<int> q;
q.front();//q的第一筆資料
q.size();//q的尺寸
q.push();//q存取資料
q.pop();//q刪除資料
偶數pop兩次
奇數pop一次
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. queue<int> q;
  5. int main()
  6. {
  7.         cin>>n;
  8.         int a;
  9.         for(int i=0;i<n;i++)
  10.         {
  11.                 cin>>a;
  12.                 q.push(a);
  13.         }
  14.         while(q.size()>1)
  15.         {
  16.                 int t=q.front();
  17.                 if(t%2==0)
  18.                 {
  19.                         q.push(t);
  20.                         q.pop();
  21.                         q.pop();
  22.                         n--;       
  23.                 }
  24.                 else
  25.                 {
  26.                         q.push(t);
  27.                         q.pop();
  28.                 }
  29.                 /*for(int j:v)
  30.                         cout<<j<<' ';
  31.                 cout<<endl;*/
  32.         }
  33.         /*for(int i:v)
  34.                 cout<<i<<endl;*/
  35.         cout<<q.front()<<endl;
  36.         return 0;
  37. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,t;
  4. queue<int> q;
  5. int main()
  6. {
  7.     cin>>n;
  8.     for(int i=0;i<n;i++)
  9.     {
  10.         cin>>t;
  11.         q.push(t);
  12.     }
  13.     while(q.size()>1)
  14.     {
  15.         int a=q.front();
  16.         q.pop();
  17.         if(a%2==0)
  18.             q.pop();
  19.         q.push(a);
  20.     }
  21.     cout<<q.front();
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main(){
  4.     queue<int> q;
  5.     int n;
  6.     cin>>n;
  7.     for(int i=0;i<n;i++){
  8.         int a;
  9.         cin>>a;
  10.         q.push(a);
  11.     }
  12.     while(q.size()!=1){
  13.         if(q.front()%2==0){
  14.             int idx=q.front();
  15.             q.pop();
  16.             q.pop();
  17.             q.push(idx);
  18.         }
  19.         else{
  20.             int idx=q.front();
  21.             q.pop();
  22.             q.push(idx);
  23.         }
  24.     }
  25.     cout<<q.front();
  26.     return 0;
  27. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,m;
  4. queue<int> q;
  5. int main()
  6. {
  7.     cin>>n;
  8.     for(int i=0; i<n; i++)
  9.     {
  10.         cin>>m;
  11.         q.push(m);
  12.     }

  13.     while(q.size()!=1)
  14.     {
  15.         if(q.front()%2==0)
  16.         {
  17.             m=q.front();
  18.             q.pop();
  19.             q.pop();
  20.             q.push(m);
  21.         }
  22.         else
  23.         {
  24.             m=q.front();
  25.             q.pop();
  26.             q.push(m);
  27.         }
  28.     }
  29.     cout<<q.front()<<endl;
  30.     return 0;
  31. }
複製代碼

TOP

返回列表