g875. 尋找數字
標籤 : binary search 互動題
通過比率 : 52人/69人 ( 75% ) [非即時]
評分方式:
Special

最近更新 : 2021-12-26 21:21

內容

 ※本題為互動題

Ststone參加了一個猜數字大賽,已知數字的範圍介在1~107之間,每次他猜了一個數字,主持人會回答他1或0,1代表他猜的數字大於等於答案,0則是代表他猜的數字小於答案。

由於疫情的緣故,今年的猜數字大賽改成線上舉辦,主辦單位為了減少人力,寫了一個主持程式,分別有兩個函式可以使用。

request(a):代表你猜a,也就是說你向主持人詢問數字a與答案的關係,會回傳0或1,1代表你猜的數字大於等於答案,0則是代表你猜的數字小於答案。
 
upload_answer(a):回傳你覺得是的答案,主持人會告訴你你是不是對的,只能使用一次,用在最後回答問題時。
 
請使用以上函式,寫出一個程式幫助Ststone猜到數字吧!
 
 
 
作答說明:
 
本題請依照語言先去複製下面的程式碼,依照下面寫的貼在程式中,並且使用上述提及的request(a)和upload_answer(a)函式作答,request(a)可用來詢問,upload_answer(a)則是最後回傳答案用的。
 
輸入輸出請見下方輸入輸出說明。
 
 
 
評分說明:
 
每次使用的搜尋次數≤625 50分
每次使用的搜尋次數≤75 75分
每次使用的搜尋次數≤30 100分
 
 

C++:請複製以下程式碼,貼在main外,並在main裡面,先呼叫cpp_start();函式再繼續作答。建議使用long long及cin cout作答。

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;}

 

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:請複製以下程式碼,貼在main外,並在main裡面,先呼叫c_start();函式再繼續作答。建議使用long long作答。
 
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;}
 
Java:請複製以下程式碼,貼在main外,並在main裡面,先呼叫java_start();函式再繼續作答。建議使用long作答。
 
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;}
 
輸入說明

於使用者端測試時,請輸入一個整數A(1≤A≤107),並且最後使用upload_answer(a)回傳答案,程式會幫你判斷你所回傳的答案是否為輸入的數字。

輸出說明

若輸入的數字與upload_answer(a)回傳的數字相等時:

AC
request: X times.

不相等時:

WA
Your answer is: OOO.
request: X times.

OOO與X會依照實際情形變化

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

回傳101:
WA
Your answer is: 101.
request: 24 times.
範例輸入 #2
87
範例輸出 #2
回傳87:
AC
request: 24 times.

回傳78:
WA
Your answer is: 78.
request: 24 times.
測資資訊:
記憶體限制: 64 MB
公開 測資點#0 (5%): 0.5s , <1K
公開 測資點#1 (5%): 0.5s , <1K
公開 測資點#2 (5%): 0.5s , <1K
公開 測資點#3 (5%): 0.5s , <1K
公開 測資點#4 (5%): 0.5s , <1K
公開 測資點#5 (5%): 0.5s , <1K
公開 測資點#6 (5%): 0.5s , <1K
公開 測資點#7 (5%): 0.5s , <1K
公開 測資點#8 (5%): 0.5s , <1K
公開 測資點#9 (5%): 0.5s , <1K
公開 測資點#10 (5%): 0.5s , <1K
公開 測資點#11 (5%): 0.5s , <1K
公開 測資點#12 (5%): 0.5s , <1K
公開 測資點#13 (5%): 0.5s , <1K
公開 測資點#14 (5%): 0.5s , <1K
公開 測資點#15 (5%): 0.5s , <1K
公開 測資點#16 (5%): 0.5s , <1K
公開 測資點#17 (5%): 0.5s , <1K
公開 測資點#18 (5%): 0.5s , <1K
公開 測資點#19 (5%): 0.5s , <1K
提示 :

目前公開測試中。

有問題歡迎詢問或來信。

標籤:
binary search 互動題
出處:
經典互動題 [管理者: Ststone1687 (Ststone) ]

本題狀況 本題討論 排行

編號 身分 題目 主題 人氣 發表日期
沒有發現任何「解題報告」