#3231: C++ 怎麼改答案都只有大吉==


jeremy83125 (OX)

學校 : 臺北市立中正高級中學
編號 : 10545
來源 : [124.11.142.185]
最後登入時間 :
2010-07-11 10:43:28
a003. 兩光法師占卜術 | From: [124.11.189.149] | 發表日期 : 2010-01-10 00:10

#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std; 
int main(int argc,char *argv[])
{    
      int M,D,S;    
      S=((M*2+D)%3);     
      while(cin>>M,cin>>D)
      {          
          if (S==0)
          {
              cout << "普通"<< endl;             
          } 
          else if (S==1)         
          {
              cout << "吉"<< endl;             
          }   
          else if (S==2)
          {
              cout << "大吉"<< endl;
          }       
      }
      system("PAUSE");  
      return 0;
#3234: Re:C++ 怎麼改答案都只有大吉==


johnchiang8324 (建中資訊百一級猩猩)

學校 : 臺北市立建國高級中學
編號 : 9625
來源 : [101.137.42.142]
最後登入時間 :
2023-03-03 02:58:24
a003. 兩光法師占卜術 | From: [123.50.52.229] | 發表日期 : 2010-01-10 09:57

#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std; 
int main(int argc,char *argv[])
{    
      int M,D,S;    
      S=((M*2+D)%3);     
      while(cin>>M,cin>>D)
      {          
          if (S==0)
          {
              cout << "普通"<< endl;             
          } 
          else if (S==1)         
          {
              cout << "吉"<< endl;             
          }   
          else if (S==2)
          {
              cout << "大吉"<< endl;
          }       
      }
      system("PAUSE");  
      return 0;
}


你應該是先做輸入的動作,然後才在做運算。
而且你的變數S之後必未隨著M,D變動。
以下是我依據你的程式而修正的程式碼:
#include<iostream>
#include<cstdlib>
#include<math.h>//基本運算可以不用引入math.h
using namespace std; 
int main(int argc,char *argv[])
{    
      int M,D,S;     
      while(cin>>M,cin>>D)
      { 

          S=((M*2+D)%3);      
                
          if (S==0)
          {
              cout << "普通"<< endl;             
          } 
          else if (S==1)         
          {
              cout << "吉"<< endl;             
          }   
          else if (S==2)
          {
              cout << "大吉"<< endl;
          }       
      }
      system("PAUSE");//上傳記得抹掉   
      return 0;
}

 
#3235: Re:C++ 怎麼改答案都只有大吉==


jeremy83125 (OX)

學校 : 臺北市立中正高級中學
編號 : 10545
來源 : [124.11.142.185]
最後登入時間 :
2010-07-11 10:43:28
a003. 兩光法師占卜術 | From: [124.11.143.3] | 發表日期 : 2010-01-10 10:14

#include
#include
#include
using namespace std; 
int main(int argc,char *argv[])
{    
      int M,D,S;    
      S=((M*2+D)%3);     
      while(cin>>M,cin>>D)
      {          
          if (S==0)
          {
              cout << "普通"<< endl;             
          } 
          else if (S==1)         
          {
              cout << "吉"<< endl;             
          }   
          else if (S==2)
          {
              cout << "大吉"<< endl;
          }       
      }
      system("PAUSE");  
      return 0;
}


你應該是先做輸入的動作,然後才在做運算。
而且你的變數S之後必未隨著M,D變動。
以下是我依據你的程式而修正的程式碼:
#include
#include
#include//基本運算可以不用引入math.h
using namespace std; 
int main(int argc,char *argv[])
{    
      int M,D,S;     
      while(cin>>M,cin>>D)
      { 

          S=((M*2+D)%3);      
                
          if (S==0)
          {
              cout << "普通"<< endl;             
          } 
          else if (S==1)         
          {
              cout << "吉"<< endl;             
          }   
          else if (S==2)
          {
              cout << "大吉"<< endl;
          }       
      }
      system("PAUSE");//上傳記得抹掉   
      return 0;
}

 

 

謝謝大大喔~

 
ZeroJudge Forum