程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 九度OJ 1006 ZOJ問題

九度OJ 1006 ZOJ問題

編輯:C++入門知識

 


題目分析:

要讀懂題目裡給的3條規則,找到會Accepted的規律。我找到的規律是:

1.首字母為z:那麼不管中間有多少個o,只要最後只有1個j就結束的話,那麼全部Accepted。

2.首字母不為z,則必須為o:根據後面兩條規則,找到規律----只要a*b = c(這裡b是z和j之間o的個數),那麼全部Accepted。當然之前要進行第二條規則的判斷。

這道題倒是沒什麼難度,就是考慮條件的時候要考慮周全,還要考慮字符串的結束問題。

 


源代碼:


[cpp]  #include <iostream>  
#include <string>  
using namespace std; 
 
int main() 

    string s; 
    while (cin>>s) 
    { 
        int x = 0; 
        int a = 0, b = 0, c = 0; 
        int temp = -1; 
         
        if (s[0] == 'z') 
        { 
            int d = 1; 
            //判斷z後邊為o  
            if (s[d] != 'o') 
            { 
                cout<<"Wrong Answer"<<endl; 
            } 
            else 
            { 
                while (s[d] == 'o') 
                { 
                    d++; 
                } 
                //判斷o後邊為j  
                if (s[d] != 'j') 
                { 
                    cout<<"Wrong Answer"<<endl; 
                } 
                else 
                { 
                    d ++; 
                    //判斷j後邊是否還有字符  
                    if (d != s.length()) 
                    { 
                        cout<<"Wrong Answer"<<endl; 
                    } 
                    else 
                    { 
                        cout<<"Accepted"<<endl; 
                    } 
                } 
            } 
             
        } 
        else 
        {//首字母不為z,則必為o,計算o的個數  
            if (s[0] != 'o') 
            { 
                cout<<"Wrong Answer"<<endl; 
            } 
            else 
            { 
                int i = 0; 
                while (s[i] == 'o') 
                { 
                    i++; 
                } 
                x = i; 
                a = i; 
 
                //o判斷結束,o之後必須是z  
                if(s[i] != 'z') 
                { 
                    cout<<"Wrong Answer"<<endl; 
                } 
                else     
                {//o後邊是z  
                    i++; 
                    //判斷z後邊是否為o  
                    if (s[i] != 'o') 
                    { 
                        cout<<"Wrong Answer"<<endl; 
                    } 
                    else 
                    {//z後邊為o,計算o的個數  
                        int j = 0; 
                        while (s[i] == 'o') 
                        { 
                            i++; 
                            j++;    //o的個數  
                        } 
 
                        b = j; 
 
                        if (s[i] != 'j') 
                        { 
                            cout<<"Wrong Answer"<<endl; 
                        } 
                        else 
                        {//判斷j後邊o的個數  
                            int k = 0; 
                            i++; 
                            if (s[i] != 'o') 
                            { 
                                cout<<"Wrong Answer"<<endl; 
                            } 
                            else 
                            { 
                                while (s[i] == 'o') 
                                { 
                                    i++; 
                                    k++; 
                                } 
 
                                //判斷字符串是否結束  
                                if (i != s.length()) 
                                { 
                                    cout<<"Wrong Answer"<<endl; 
                                } 
                                else 
                                { 
                                    c = k; 
                                    //比較a與c  
                                    if (a * b == c) 
                                    { 
                                        cout<<"Accepted"<<endl; 
                                    } 
                                    else 
                                    { 
                                        cout<<"Wrong Answer"<<endl; 
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            } 
        } 
    } 
    return 0; 

#include <iostream>
#include <string>
using namespace std;

int main()
{
 string s;
 while (cin>>s)
 {
  int x = 0;
  int a = 0, b = 0, c = 0;
  int temp = -1;
  
  if (s[0] == 'z')
  {
   int d = 1;
   //判斷z後邊為o
   if (s[d] != 'o')
   {
    cout<<"Wrong Answer"<<endl;
   }
   else
   {
    while (s[d] == 'o')
    {
     d++;
    }
    //判斷o後邊為j
    if (s[d] != 'j')
    {
     cout<<"Wrong Answer"<<endl;
    }
    else
    {
     d ++;
     //判斷j後邊是否還有字符
     if (d != s.length())
     {
      cout<<"Wrong Answer"<<endl;
     }
     else
     {
      cout<<"Accepted"<<endl;
     }
    }
   }
   
  }
  else
  {//首字母不為z,則必為o,計算o的個數
   if (s[0] != 'o')
   {
    cout<<"Wrong Answer"<<endl;
   }
   else
   {
    int i = 0;
    while (s[i] == 'o')
    {
     i++;
    }
    x = i;
    a = i;

    //o判斷結束,o之後必須是z
    if(s[i] != 'z')
    {
     cout<<"Wrong Answer"<<endl;
    }
    else 
    {//o後邊是z
     i++;
     //判斷z後邊是否為o
     if (s[i] != 'o')
     {
      cout<<"Wrong Answer"<<endl;
     }
     else
     {//z後邊為o,計算o的個數
      int j = 0;
      while (s[i] == 'o')
      {
       i++;
       j++; //o的個數
      }

      b = j;

      if (s[i] != 'j')
      {
       cout<<"Wrong Answer"<<endl;
      }
      else
      {//判斷j後邊o的個數
       int k = 0;
       i++;
       if (s[i] != 'o')
       {
        cout<<"Wrong Answer"<<endl;
       }
       else
       {
        while (s[i] == 'o')
        {
         i++;
         k++;
        }

        //判斷字符串是否結束
        if (i != s.length())
        {
         cout<<"Wrong Answer"<<endl;
        }
        else
        {
         c = k;
         //比較a與c
         if (a * b == c)
         {
          cout<<"Accepted"<<endl;
         }
         else
         {
          cout<<"Wrong Answer"<<endl;
         }
        }
       }
      }
     }
    }
   }
  }
 }
 return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved