zerojudge c561. Bert 愛搗蛋 解法大全: 思路的大方向: 1.先選答案,再翻轉 優點:省時間(只要翻轉一次) 缺點:較不直覺,較難寫 答案要先比位數,再比末位數大小 先取位數較多的,再取末位數大的 其方法有: a.排序: 用內建sort搭配自寫的cmp函式 sort語法: sort(s,s+n,cmp); 重要的是自寫cmp函式 自己寫的參考程式碼: typedef struct obj { int len; string s; char c; obj( string a){s=a,len=s.length(),c=s[len -1 ];} }OBJ; int cmp ( string s1, string s2) { OBJ obja (s1) , objb (s2) ; if (obja.len==objb.len) { if (obja.c==objb.c) return 0 ; else if (obja.c<objb.c) return 1 ; else return -1 ; } return obja.len-objb.len; } 註:在自訂測資中答案是正確的,但在正式測資時會出現NA(有時是SEGERROR,有時是答案錯誤) b.直接選: /* DATE:2020/08/20 zerojudge c561 Version:3 status:not complete yet */ #include <iostream> using namespace std; #include <string> #define RE_C 1 int cas=0,ti=1; void check() { #ifdef RE_C==1 cout<<cas<<"er"<<ti++<<endl; #endif } typedef struct obj { int len; int last_digit; int big; string s; obj(){big=1;} void setdigit(){len--;la...
Comments
Post a Comment