PKU Helper 2017 Development Group Recruit new Topic answer
T1
Implement a SQRT function yourself, accept a floating-point variable, return the square root of its arithmetic, and return the result to be equal within the error range of 1e-6. The language native SQRT library function must not be invoked.
Using Newton iterative method to implement SQRT function, compared to the general binary search method is more efficient, the basic idea is to give the solution of the equation x^2=a; To do this, take a point in the x-axis, the parallel line of y-axis, in the intersection with the function image as a function image tangent, get the x-axis intersection to obtain a new X value Repeat the above steps until the error is less than 1e-6.
The code is as follows:
#include <iostream>
using namespace std;
Double sqrt (double x) {
if (x < 0) {
cout << "require an input that greater than zero";
return-1;
}
Double result = x + 0.5;//takes the initial value
while (true) { //iterate
Double temp = result-1e-6;
if (Temp*temp < x) break;
result = (Result + X/result)/2;
}
return result;
}
int main () {
double A;
Cin >> A;
cout << sqrt (a);
return 0;
}
The reason for using the initial value x+0.5 here is to ensure that the first obtained intersection is above the x-axis, thus guaranteeing that the function value of each iteration is greater than 0, so that when the result of satisfying the accuracy condition is judged, it is simply to determine whether the value of result-1e-6 is less than 0.
Results conform to accuracy. T2
Please analyze the PKU portal (portal.pku.edu.cn) via HTTP capture, Chrome debug mode, access to HTML source code or any other reasonable means (even search for previous blogs or decompile PKU Helper apps), Any one or more of the login behaviors of the Gateway (its.pku.edu.cn), the Elective Network (elective.pku.edu.cn), or the educational Network (dean.pku.edu.cn). According to your analysis results, the school interface (IAAA, portal, etc.) is invoked in the appropriate order to implement a function of inputting the number, password, and returning the corresponding name.
If you have Android, IOS, or WEB development capabilities, it is recommended that you implement it as a GUI, with a simple two-column input and login button, and display your name when the login is successful.
If you are unable to implement the GUI form, you can enter the user name, password, and command line output name for the command line. Or whatever you think is a convenient way to show it.
First, the login behavior analysis
This paper mainly analyzes the login behavior on the portal and its two websites.
Because it is relatively simple, first of all, the analysis of its login behavior, the method is to use Fiddler to PKU Helper application of the login behavior to grab packets. Its behavior is relatively simple, to the HTTPS://ITS.PKU.EDU.CN:5428/IPGATEWAYOFPKU Post has the x-www-form-urlencoded format entity, where: UID is the school user name (that is, the student number) Password is the campus password operation is the code of conduct, such as connect on behalf of the Gateway, disconnect on behalf of the disconnection, ' Disconnectall ' on behalf of disconnect all connection range is valid when the behavior is connect. Where range is 1 for connection billing address, range for 2 for connection free address timeout, and 1 for value, should be the connection timeout option
Then analyze the login behavior on the portal, this behavior is more complicated, because it also uses IAAA for on-campus login, this analysis mainly uses the reading portal and iaaa two Web site source form, but also uses the Chrome debugging mode to confirm the login behavior; login behavior is as follows: The user enters the portal homepage and clicks the login button. The user will be navigated to the IAAA page for IAAA login, and after entering the user name and password, the browser will go to https://iaaa.pku.edu.cn/iaaa/oauthlogin.do Post has x-www-form-urlencoded format entities, the more important attributes in the entity are: AppID, which is the application that represents the delegate Iaaa login, in the context of the portal, this value is Portal;user_name, the user name (that is, the school number) Password, that is, the password, Redirurl, that is, after the successful login iaaa need to jump to the page, in the context of the portal, the URL is http://portal.pku.edu.cn/portal2013/ssoLogin.do, Used to do after the successful login verification and jump and other aftercare work. If the user name password is correct, IAAA will return the form {\ "success\": True,\ "token\": \ "A754f5394819f1f50201d4aebb67a3c9\"} in JSON format data; The Success property value naturally indicates whether the login was successful, and if successful, the JSON contains a string format data called token, which can be used to verify the login success. After the login is successful, iaaa then jumps to the URL indicated in the second step, and adds two querystring to the URL: Rand, which is a 0-1 random double-precision floating-point number; token, the token that just returned To verify that the login was successful. On the jump page, the login verification action is performed, and then automatically jumps to the portal home page of the logged in state. It is important to note that there is an error in the case where there is no token in the QueryString or the GET request header that is sent when the jump occurs without a start in the portal set cookie, so you cannot skip get when using code to impersonate the login behavior The steps of the portal homepage directly simulate the iaaa login, as the first step is to get the portal cookie. After the successful login can get the real name of the user, I use this is in the Portal JS file index.js found in the interface/portal2013/isuserloggEd.do Direct get this interface to get JSON format data containing the user's real name. Similarly, the absence of a login and the absence of a cookie will result in an error.
The above is the main step of the portal login, in fact, elective login process is very similar, because its use of the form is also commissioned Iaaa to log in, and with token jump back, here is no longer elaborated, because of time, Dean's login behavior is not analyzed, Therefore, it is no longer elaborated. Analysis of the process of the main reference to the portal and the IAAA site of the two-segment JS code, two more important functions are pasted as follows for reference:
1.IAAA Primary login Behavior function Oauthlogon, located in the Oauthlogin.js file of the IAAA Web site
function Oauthlogon () {if ($ ("#user_name"). val () = = "" | | $ ("#user_name"). val () = = "School Number/employee number/North Mail") {//$ ("#msg"). T
EXT ("Account cannot be empty");
$ ("#msg"). HTML ("<i class=\" fa fa-minus-circle\ "></i> account cannot be empty");
$ ("#user_name"). focus (); }else if ($ ("#password"). val () = = "" | |
$ ("#password"). val () = = "Password") {//$ ("#msg"). Text ("Password cannot be empty");
$ ("#msg"). HTML ("<i class=\" fa fa-minus-circle\ "></i> password cannot be empty");
$ ("#password"). focus (); The Else if ($ ("#otp_area: Visible"). Length>0 && ($ ("#otp_code"). val () = = "" | | $ ("#otp_code"). val () = "Dynamic Password"
) {$ ("#msg"). HTML ("<i class=\" fa fa-minus-circle\ "></i> dynamic password cannot be empty");
$ ("#otp_code"). focus (); The Else if ($ ("#sms_area: Visible"). Length>0 && ($ ("#sms_code"). val () = = "" | | $ ("#sms_code"). val () = = "SMS Verification Code
")) {$ (" #msg "). HTML (" <i class=\ "fa fa-minus-circle\" ></i> SMS Verification code cannot be empty ");
$ ("#sms_code"). focus (); } else if ($ ("#code_areA:visible "). Length>0 && ($ (" #valid_code "). val () = =" "| | $ (" #valid_code "). val () = =" Captcha ") {//$ (" #m
SG "). Text (" Verification code cannot be null ");
$ ("#msg"). HTML ("<i class=\" FA fa-minus-circle\ "></i> verification code cannot be empty");
$ ("#valid_code"). focus ();
} else {//document.myform.submit ();
if ($ ("#remember_check") [0].checked==true] {Setcookie ("UserName", $ ("#user_name"). Val ());
Setcookie ("Remember", "true");
} else{Delcookie ("UserName");
Delcookie ("Remember");
} $ ("#msg"). Text ("Logging in ...");
$.ajax ('/iaaa/oauthlogin.do ', {type: ' POST ', Data:{appid: $ ("#appid"). Val (),
UserName: $ ("#user_name"). Val (), Password: $ ("#password"). Val (), Randcode: $ ("#valid_code"). Val (), smscode:$ ("#sms_code"). Val (), otpcode:$ ("#otp_
Code "). Val (), Redirurl:redirecturl}, DataType: "JSON", Success:function (Data,status,
XHR) {var json = data;
if (true = = json.success) {//If a weak password is displayed #msg prompt if (json.isflag) {
$ ("#msg"). Text ("Password strength is insufficient, please login to the portal to modify as soon as possible"); SetTimeout (function () {if (Redirecturl.indexof ("?") >0) window.location.href = redirecturl+ "&rand=" +math.random () + "&token=" +json.to
Ken
else window.location.href = redirecturl+ "? rand=" +math.random () + "&token=" +json.token;
},2000); }else{if (Redirecturl.indexof ("?") >0) window.location.href = redirecturl+ "&rand=" +math.random () + "&token=" +json.to
Ken else window.location.href = redirecturl+ "? rand=" +math.random () + "&A
Mp;token= "+json.token; }} else{$ ("#msg"). HTML ("<i class=\" FA fa-minus-circle
\ "></i>" +json.errors.msg); $ ("#code_img") [0].src=]/iaaa/servlet/drawservlet?
Rand= "+math.random (); if ("Account not activated" ==json.errors.msg) {window.location.href = "https://iaaa.pku.edu.cn/iaaa/activateAcc Ount.jsp?
Rand= "+math.random () +" &activecode= "+json.activecode;
} else if ("User name error" ==json.errors.msg) {$ ("#user_name"). Select ();
if (True==json.showcode) {$ ("#code_area"). Show ();
}} else if ("Password error" ==json.errors.msg) { $ ("#password"). Select ();
} else if ("Authenticode error" ==json.errors.msg) {$ ("#code_area"). Show ();
$ ("#valid_code"). Select ();
} else if ("SMS Authenticode error or expired" ==json.errors.msg) {$ ("#sms_code"). Select (); } else if ("Dynamic password error or expired" ==json.errors.msg) {$ ("#o
Tp_code "). Select (); }}, Error:function (Xhr,status,error) {$ ("#ms
G "). html (" <i class=\ "fa fa-minus-circle\" ></i> query Exception "); $ ("#code_img"). attr ("src", "/iaaa/servlet/drawservlet?").
Rand= "+math.random ());
}
}); }
}
This function takes the 2nd, 3-step action in the preceding sign-in step.
2.portal Gets whether the user is logged in to the islogin of the user's real name information, in the JS file index.js that is referenced in the Portal home page:
function IsLogin () {$.ajax ("/portal2013/isuserlogged.do", {method: "POST", DataType: "JSON", err
Or:function (Xhr,status,error) {$ ("#login"). Text ("Hello, please login");
$ ("#login"). attr ("href", "/portal2013/login.jsp");
$ ("#login"). CSS ("width", "120px"); }, Success:function (DATA,STATUS,XHR) {if (data.success==true) {$ ("#login"). Text ("Hello," +d
Ata.username+ "[Exit]");
$ ("#login"). attr ("href", "/portal2013/logout.do");
$ ("#login"). CSS ("width", "160px");
var candidate=["Li Ting Yan", "Chen Guang", "Ouyang Rongbin", "Long Neu", "Wang Qian Yi", "Li Li", "Liu Yunfeng", "Jia Xiaoxun"; if (Candidate.indexof (data.username) >=0) {$ ("#login"). After ("<a href=\"/portal2013/portal.jsp\ "&G
t; my portal </a><a href=\ "/portal2013/mobile/mp.html\" > Simplified version </a> "); } else $ ("#login"). After ("<a href=\"/portal2013/portal.jsp\ "> My portal </a>"); /* $ ("Div.menu"). Append (' <a href= "/portal2013/util/appsysredir.do?appid=xjh" target= "_blank" style= " Background-color: #dda813; "
> Xiaojiahe </a> '); */} else{$ ("#login"). Text ("Hello, please login");
$ ("#login"). attr ("href", "/portal2013/login.jsp");
$ ("#login"). CSS ("width", "120px");
}
}
}); }
This function is primarily responsible for the 5th step above. Second, the name query function realization
This time for the required name query function, the main use of the portal and the interface provided by IAAA, the details of the interface used and the login query steps have been described in the previous section for the portal login behavior analysis, in this section only need to use code to simulate this login process.
First of all, I chose the method to develop a simple Android application, as required, its interface contains a simple two-column input and login button, and after the successful login will invoke the query name interface, and the user name displayed under the button.
The preview image is as follows:
This application is directly in the Android studio default template changes, so still have the default template shadow, beautiful degree is very general, but the required functionality has been fully implemented.
The complete code of the project I have uploaded to my github repository, here is the main code for the project:
Content_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "an Droid:layout_width= "Match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_ Vertical_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingright=" @dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "app:layout_behavior=" @string/appbar_ Scrolling_view_behavior "tools:context=" com.example.asus.yh1.MainActivity "tools:showin=" @layout/activity_main "&
Gt <button android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@ String/loginstr "android:id=" @+id/btn1 "android:nestedscrollingenabled=" false "android:onclick=" Fu C "Android:layout_alignparentBottom= "true" android:layout_marginbottom= "188DP" android:layout_alignright= "@+id/txtpswd" Android : layout_alignend= "@+id/txtpswd" android:layout_alignleft= "@+id/txtpswd" android:layout_alignstart= "@+id/tx
tpswd "/> <edittext android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:inputtype= "Textpassword" android:ems= "android:id=" @+id/txtpswd "Android:layout_a" Bove= "@+id/btn1" android:layout_centerhorizontal= "true" android:layout_marginbottom= "30DP" Android : hint= "password"/> <edittext android:layout_width= "wrap_content" android:layout_height= "wrap_content "android:inputtype=" number "android:ems=" android:id= "@+id/txtusername" android:layout _centerhorizontal= "true" android:layout_above= "@+id/txtpswd" android:layout_marginbottom= "30DP" an
droid:hint= "User name"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "scheduled display name" android:id= "@+id/rnameview" android:layout_aligntop= "@+id/btn1" Android:layout_cente Rhorizontal= "true" android:layout_below= "@id/btn1" android:layout_margintop= "60DP"/> </relativelay Out>
This is the main interface design of the application, you can see the layout is very simple, in a relativelayout in order to put the required components, here you can see the button btn1 is bound to a named fuc onclick function.
Mainactivity.java
Package com.example.asus.yh1;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.os.Bundle;
Import Android.support.design.widget.FloatingActionButton;
Import Android.support.design.widget.Snackbar;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.Toolbar;
Import Android.view.View;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import org.json.*;
Import Com.example.asus.yh1.lib.Parameter;
Import com.example.asus.yh1.lib.WebConnection;
Import java.util.ArrayList;
Import Java.util.Random;
public class Mainactivity extends appcompatactivity {public static final int show_name=0;
public static final int show_error=1;
Private Handler handler=new Handler () {public void Handlemessage (Message msg) {switch (msg.what) { Case Show_name: {string name= (String)Msg.obj;
TextView rnameview= (TextView) Findviewbyid (R.id.rnameview);
Rnameview.settext ("Your real name is" +name);
}break;
Case show_error:{String errormsg= (String) msg.obj;
Toast.maketext (Getapplicationcontext (), ErrorMsg, Toast.length_short). Show ();
}
}
}
};
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Setsupportactionbar (toolbar);
Floatingactionbutton fab = (Floatingactionbutton) Findviewbyid (R.id.fab);
Fab.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { Snackbar.make (View, "Replace with your own action", Snackbar.length_long). setaction("Action", null). Show ();
}
});
} public void Fuc (View v) {EditText txtuser= (EditText) Findviewbyid (r.id.txtusername);
EditText txtpswd= (EditText) Findviewbyid (R.ID.TXTPSWD);
String Username=txtuser.gettext (). toString ();
String Pswd=txtpswd.gettext (). toString (); If