給定一句英語,要求你編寫程序,將句中全部單詞的順序顛倒輸出。 測試輸入包含一個測試用例,在一行內給出總長度不超過500 000的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用若干個空格分開。
char op[500001];//定義最大狀況 int i,j,len; int count=0;//定義計算每一個單詞的長度 int flag=1;//控制空格輸出 gets(op);//獲得字符串 len=strlen(op);//得字符串的長度 for i=len-1 to 0 //遍歷字符串 if op[i]!=' ' then count++ end if //計算每一個單詞的長度 if op[i]>='A'&&op[i]<='Z' then //控制空格輸出 if flag==1 then flag=0; end if else 輸出空格 for j=i to i+count 輸出單詞 end for count=0//從新計算下一個單詞的長度 end if //對最大的狀況進行輸出 if count>0 then 輸出空格 for i=0 to count 輸出單詞 end for end if
Q1:第一次提交格式出錯,有看了看題目,才發現結尾不能夠有空格。
A1:後來定義flag來控制空格的輸出,第一個單詞前不輸出空格,後面的單詞前都須要輸出空格。
Q2:沒有考慮到最後一個測試點,就是最大的時候可能沒有開頭的大寫字母就結束了,這樣沒遇到大寫字母就無法輸出。
A2:後來繼續去考慮count是否大於0,若是大於0就說明還有單詞沒有輸出,再把最後那個輸出便可。算法
begin://以防輸入非法字符,能夠從新輸入 int i=0,j,answer; double totalTime;//計算每題的答題時間 char op[4]={'+','-','*','/'};定義字符串來隨機生成符號 if level=1 then 生成一步一位數加減乘除計算式,並儲存 end if else if level==2 orlevel==3 then 生成兩步2/3位加減計算式,並儲存 end if if strlen(exp)==3 then//判斷是否能夠整除,不能夠則從新生成表達式 if !IsExp(exp) then goto begin end if end if
int flag=1//控制輸出 if 運算符是除號時 then if 結果不能被整除時 flag=0; end if end if if 運算符是減號時 then if 前一個數大於後面一個數時 flag=0; end if end if return flag;
int m,sum=0,i,answer; char oldc='+'; if strlen(exp)==3 then//若是是第一級別的狀況 if 中間的符號是+ then answer=前面的數+後面的數 end if if 中間的符號是- then answer=前面的數-後面的數 end if if 中間的符號是* then answer=前面的數*後面的數 end if if 中間的符號是/ then answer=前面的數/後面的數 end if end if else//若是是第二,三級別的狀況 for i=0 to strlen(exp) if 字符在0到9之間 then m=10*m+(*(exp+i)-'0');//求每一個數的數值 end if else //字符不在0到9之間 if oldc=='+' then sum=sum+m; end if else sum=sum-m; end else m=0; oldc=*(exp+i) end else end for answer=sum; end else return answer;