> [TOC] > [time=Sun, Dec 3, 2023 11:04 PM] # a100: uva488. Triangle wave**** > http://140.113.72.36:207/ShowProblem?problemid=a100 > 輸出f個, i從1~a 印i次(上半) ; i從a-1~1 印i次(下半) > **最後一個案例不換行if(n!=0) > 最後一個波不換行if(k!=f-1)** ```cpp= #include <iostream> using namespace std; int main() { int n,a,f; cin>>n; while(n--)//先執行 判斷n>0成立後 n-- { cin>>a>>f; for(int k=0;k<f;k++)//印出f個波 { for(int i=1;i<=a;i++)//i從1印到a { for(int j=1;j<=i;j++)//把i印i次 { cout<<i; } cout<<endl; } for(int i=a-1;i>0;i--)//i從a-1到1 { for(int j=i;j>0;j--)//把i印i次 { cout<<i; } cout<<endl; } if(k!=f-1)//最後一個波不換行*** { cout<<endl; } } if(n>0)//最後一個案例不換行*** cout<<endl; } return 0; } ``` # a090: uva.11398 The Base-1 Number System***** > http://140.113.72.36:207/ShowProblem?problemid=a090 >**一個0, flag是1;兩個0, flag是0;n個0, 把n-2個flag加入二進制字串** > **然後把二進制num轉成十進制數字** ```cpp= #include <iostream> #include <string> using namespace std; int main() { string baseone; string basetwo="";//空字串***** int num; char flag; while(cin>>baseone && baseone!="~") { if(baseone=="#")//二進制轉十進制 ***** { num=0;//重置***** for(int i=0;i<basetwo.length();i++) { if(i)//當i!=0***** { num*=2; } if(basetwo[i]=='1')//char***** { num+=1; } } basetwo="";//重置***** cout<<num<<endl; } else if(baseone.length()==1) { flag='1';//char***** } else if(baseone.length()==2) { flag='0';//char***** } else { for(int i=0;i<baseone.length()-2;i++)//加上n-2個flag***** { basetwo+=flag; } } } return 0; } ``` # a065: uva12019. Doom's Day Algorithm**** > http://140.113.72.36:207/ShowProblem?problemid=a065 > **先算出1/0 再推算1/0到每個日期有幾個禮拜 多幾天** ```cpp= //可以用array #include <iostream> #include <string> using namespace std; int main() { int n,m,d; cin>>n; while(cin>>m>>d) { m--;//月份先減一 日期當月不用算***** while(m!=0)//把月份換成天數***** { if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) { d+=31; m--; } else if(m==4||m==6||m==9||m==11) { d+=30; m--; } else if(m==2) { d+=28; m--; } } d%=7; if (d==2) { cout<<"Sunday"<<endl; } else if(d==3) { cout<<"Monday"<<endl; } else if(d==4) { cout<<"Tuesday"<<endl; } else if(d==5) { cout<<"Wednesday"<<endl; } else if(d==6) { cout<<"Thursday"<<endl; } else if(d==0) { cout<<"Friday"<<endl; } else if(d==1) { cout<<"Saturday"<<endl; } } return 0; } ``` # a092: uva.12289 One-Two-Three** > http://140.113.72.36:207/ShowProblem?problemid=a092 > 先分成 **三個字母/五個字母** > 三個字母再分成 **o,n/n,e/e,o =>one , else=> two** ```cpp= #include <iostream> #include <string> using namespace std; int main() { int n; string word; cin>>n; while(cin>>word) { if(word.length()==3)//one ,two 是三個字母 { if((word[0]=='o'&&word[1]=='n')|| (word[0]=='o'&&word[2]=='e')|| (word[1]=='n'&&word[2]=='e'))//只錯一個字母==會對兩個字母 char { cout<<"1"<<endl; } else { cout<<"2"<<endl; } } else if(word.length()==5)//three是五個字母 { cout<<"3"<<endl; } } return 0; ``` # a103: The Snail**** > http://140.113.72.36:207/ShowProblem?problemid=a103 > 用**float**計算**sum(預設是0)** 還有計算疲勞數值**tired=f*u/100** > 重複執行**while(true)** **終止條件(sum>h/sum<0)在迴圈裡面** > **白天爬u 晚上掉d 明天爬的距離u-=tired** ```cpp= #include <iostream> using namespace std; int main() { float h,u,d,f; while(cin>>h>>u>>d>>f)//深度,白天爬,晚上掉,疲勞 { if(h==0) { break; } int i=0; float sum=0;//用float 答案會有小數***** float tired=f*u/100;//用float 換算每天的疲勞數值 u會拿去運算 所以先加起來 ***** while(true)//要重複執行***** { i++;//day if(u>0)//不會爬負的 *** { sum+=u;//白天爬的 } if(sum>h)//白天成功爬出去 { cout<<"success on day "<<i<<endl; break; } sum-=d;//晚上掉的 if(sum<0)//晚上失敗掉到底 { cout<<"failure on day "<<i<<endl; break; } u-=tired;//疲累損耗 *** } } return 0; } ```