前往
大廳
主題

ZeroJudge - g875: 尋找數字 解題心得

Not In My Back Yard | 2022-01-16 00:00:05 | 巴幣 0 | 人氣 173

題目連結:


題目大意:
(本題是一個互動題,範例輸出入僅供參考)
本題是猜數字遊戲,數字範圍為 [1, 10 ^ 7]。

請先輸入一個數字 A,代表「答案」。接著可以利用 request(a) 函數,而系統會告訴你猜的數字 a 與答案的大小關係。當 a 大於等於答案時,會回傳 1;小於的話則回傳 0。

最後可以呼叫 upload_answer(a),來上傳可能的答案(只能呼叫一次)。系統會判斷是否為答案。

請根據使用的語言複製下列程式碼:
C++:
long long st_ans1=0,st_ans2=0,st_com=0,st_cnt=0;template<typename stst>stst request(stst a){st_cnt++;if(st_com==1)std::cout << "request=qwertyuiopasdfghjklzxcvbnm;check=ok;waiting_for_the_answer;next_line=check;reading=false;check_point=false\n";if(a>=st_ans2-st_ans1) return 1ll;else return 0ll;}template<typename stst>void upload_answer(stst a){if(st_com==1){std::cout<<"request=mnbvcxzlkjhgfdsapoiuytrewq;check=ok;waiting_for_the_answer;next_line=answer;reading=true;check_point=true\n";std::cout <<1000000007+5*(4*a+3) << '\n';}if(a==st_ans2-st_ans1){std::cout << "AC\n";std::cout << "request: "<<st_cnt<<" times.";}else{std::cout << "WA\n";std::cout << "Your answer is: "<<a<<".\n";std::cout << "request: "<<st_cnt<<" times.";}}void cpp_start(){long long st_cc;std::cin>>st_cc;if(st_cc==-87) st_com=1;if(st_com==1){long long Do_not_change_it_plz,ststone,st_zerojudge,st_IDK,st_hard,st_mouse,st_check;std::cin>>Do_not_change_it_plz>>ststone>>st_zerojudge>>st_IDK>>st_hard>>st_mouse>>st_check;st_ans1=Do_not_change_it_plz+ststone-st_zerojudge-st_IDK+st_hard-st_mouse;st_ans2=st_ans1*st_check;}else st_ans2 = st_cc;}
在開始猜測前,請先呼叫 cpp_start();

Python:
st_cc = int(input());st_com=0;st_cnt=0;st_com=1 if st_cc==-87 else 0;Do_not_change_it_plz = int(input()) if st_com else 0;ststone = int(input()) if st_com else 0;st_zerojudge = int(input()) if st_com else 0;st_IDK = int(input()) if st_com else 0;st_hard = int(input()) if st_com else 0;st_mouse = int(input()) if st_com else 0;st_check = int(input()) if st_com else 0;st_ans1=Do_not_change_it_plz+ststone-st_zerojudge-st_IDK+st_hard-st_mouse;st_ans2=st_ans1*st_check if st_com else st_cc;st_qwertyuiopasdfghjklzxcvbnm_st=st_ans2-st_ans1
def request(a:int):global st_cnt,st_com;st_cnt +=1;print("request=qwertyuiopasdfghjklzxcvbnm;check=ok;waiting_for_the_answer;next_line=check;reading=false;check_point=false")if st_com else print("",end="");    return a>=int(st_ans2-st_ans1) if  1 else  0
def upload_answer(a:int):global st_cnt,st_com;print("request=mnbvcxzlkjhgfdsapoiuytrewq;check=ok;waiting_for_the_answer;next_line=answer;reading=true;check_point=true")if st_com else print("",end="");print(1000000007+5*(4*a+3)) if st_com else print("",end="");print("AC\nrequest: "+str(st_cnt)+" times.") if int(a)==int(st_ans2-st_ans1) else print("WA\nYour answer is: "+str(a)+".\nrequest: "+str(st_cnt)+" times.")

C:
long long st_ans1=0,st_ans2=0,st_com=0,st_cnt=0,st_cc;long long request(long long a){st_cnt++;if(st_com==1)printf("%s\n","request=qwertyuiopasdfghjklzxcvbnm;check=ok;waiting_for_the_answer;next_line=check;reading=false;check_point=false");if(a>=st_ans2-st_ans1) return 1;else return 0;}void upload_answer(long long a){if(st_com==1){printf("%s\n","request=mnbvcxzlkjhgfdsapoiuytrewq;check=ok;waiting_for_the_answer;next_line=answer;reading=true;check_point=true");printf("%lld\n",1000000007+5*(4*a+3));}if(a==st_ans2-st_ans1){printf("%s\n","AC");printf("%s %lld %s\n","request: ",st_cnt," times.");}else{printf("%s\n","WA");printf("%s %lld %s\n","Your answer is: ",a,".");printf("%s %lld %s\n","request: ",st_cnt," times.");}}void c_start(){scanf("%lld",&st_cc);if(st_cc==-87){st_com=1;}if(st_com==1){long long Do_not_change_it_plz,ststone,st_zerojudge,st_IDK,st_hard,st_mouse,st_check;scanf("%lld%lld%lld%lld%lld%lld%lld",&Do_not_change_it_plz,&ststone,&st_zerojudge,&st_IDK,&st_hard,&st_mouse,&st_check);st_ans1=Do_not_change_it_plz+ststone-st_zerojudge-st_IDK+st_hard-st_mouse;st_ans2=st_ans1*st_check;}else st_ans2 = st_cc;}
在開始猜測前,請先呼叫 c_start();

Java:
static long st_ans1=0,st_ans2=0,st_com=0,st_cnt=0;static Scanner scanner = new Scanner(System.in);public static long request(long a){st_cnt++;if(st_com==1)System.out.println("request=qwertyuiopasdfghjklzxcvbnm;check=ok;waiting_for_the_answer;next_line=check;reading=false;check_point=false");if(a>=st_ans2-st_ans1) return 1;else return 0;}public static void upload_answer(long a){if(st_com==1){System.out.println("request=mnbvcxzlkjhgfdsapoiuytrewq;check=ok;waiting_for_the_answer;next_line=answer;reading=true;check_point=true");System.out.println(1000000007+5*(4*a+3));}if(a==st_ans2-st_ans1){System.out.println("AC");System.out.println("request: "+st_cnt+" times.");}else{System.out.println("WA");System.out.println("Your answer is: "+a+".");System.out.println("request: "+st_cnt+" times.");}}public static void java_start(){long st_cc;st_cc = scanner.nextLong();if(st_cc==-87)st_com=1;if(st_com==1){long Do_not_change_it_plz = scanner.nextLong(),ststone = scanner.nextLong(),st_zerojudge = scanner.nextLong(),st_IDK = scanner.nextLong(),st_hard = scanner.nextLong(),st_mouse = scanner.nextLong(),st_check = scanner.nextLong();st_ans1=Do_not_change_it_plz+ststone-st_zerojudge-st_IDK+st_hard-st_mouse;st_ans2=st_ans1*st_check;}else st_ans2 = st_cc;}
在開始猜測前,請先呼叫 java_start();

註:本題會長這樣是因為 ZeroJudge 系統之限制。所以才會有給了答案又要猜答案這種情況出現。



範例輸入:
範例輸入 #1
100

範例輸入 #2
87


範例輸出:
範例輸出 #1
回傳100:
AC
request: 24 times.

回傳101:
WA
Your answer is: 101.
request: 24 times.

範例輸出 #2
回傳87:
AC
request: 24 times.

回傳78:
WA
Your answer is: 78.
request: 24 times.


解題思維:
可以用來練習二分搜尋法(Binary Search)的題目,如同這題




此次分享到此為止,如有任何更加簡潔的想法或是有說明不清楚之地方,也煩請各位大大撥冗討論。

創作回應

更多創作