The beauty of programming in the first round of the 2014 preliminary round

Source: Internet
Author: User
Tags cmath

First go to the code...

Question Analysis and Test cases. The official Microsoft analysis will be provided later.

1. Focal Length (string processing ):

/** Main. cpp ** Created on: 2014-4-19 * Author: zy */# include <iostream> # include <string> # include <cstring> # include <vector> # include <cstdlib> # include <iomanip> // setprecision controls the number of decimal points using namespace std; double change (string s, int length) {char a [100] = {0}; double mul; // use switch to process much less code than if statements ,,, switch (s [length-2]) {case 'D': mul = 1e8; s [length-2] = 0; break; case 'C': mul = 1e7; s [length-2] = 0; break; case 'M': mul = 1e6; s [length-2] = 0; break; case 'U': mul = 1e3; s [length-2] = 0; break; case 'N': case 'p': mul = 1; s [length-2] = 0; break; default: mul = 1e9; s [length-1] = 0;} s. copy (a, s. size (), 0); // do not use this sentence. The following string to double type reports an error, or the double ret = atof (a) is forcibly converted from string to char ); return ret * mul;} int main () {int T; cin> T; double result = 0; int count = 1; string s1, s2, s3; while (T --) {// Replace the input data: 5.4mm 5.27 pxcin> s1> s2> s3; double x, y, z; // obtain the unit in the string and convert it (nm, Division not required) x = change (s1, s1.size (); y = change (s2, s2.size ()); z = change (s3, s3.size (); result = z * x/y; // output result cout <"Case" <count <":"; cout <fixed <setprecision (2) <result <"px" <endl; count ++;} result = 0 ;}

2. Tree (my classmate wrote this, And cainiao said it was not very good. I added the annotation. You should not read the annotation first, but directly read the code. I have not understood the code yet ):

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <string> # include <map> # include <iostream> # include <sstream> # include <queue> using namespace std; # define ll long longconst ll mod = rj00007ll, MG = 12316ll; int T, cas = 0, N, st [100005], e, h [100005]; vector <int> L [100005], R [100005], del [100005]; ll tr [100005], num [100005]; struct EG {int v, nx ;} eg [100005]; void add (int u, int v) {eg [e]. v = v; eg [e]. Nx = st [u]; st [u] = e ++;} void update (int k, int v) {v % = mod; for (; k <= N; k + = k &-k) {tr [k] + = v; if (tr [k]> = mod) tr [k]-= mod ;}} ll read (int k) {ll ret = 0; for (; k-= k &-k) {ret + = tr [k]; if (ret> = mod) ret-= mod;} return ret;} // calculate the final output result ll cal () based on the degree of each node and a formula () {ll ans = 0; for (int I = 1; I <= N; I ++) {ans = (ans * MG + num [I]) % mod ;} return ans;} // dfs -- depth priority algorithm, u: node, hh: Depth void dfs (int u, int hh) {h [u] = hh; int sz = L [u]. size (); for (int I = 0; I <sz; I ++) {update (L [u] [I], del [u] [I]); update (R [u] [I] + 1,-del [u] [I]); // printf ("% d-% d \ n ", u, L [u] [I], R [u] [I], del [u] [I]);} // printf ("% d \ n", u, h [u]); num [u] = read (h [u]); for (int I = st [u]; ~ I; I = eg [I]. nx) {int v = eg [I]. v; dfs (v, hh + 1) ;}for (int I = 0; I <sz; I ++) {update (L [u] [I], -del [u] [I]); update (R [u] [I] + 1, del [u] [I]);} int main () {scanf ("% d", & T); while (T --) {// scanf ("% d", & N); e = 0; // node status.-1 indicates that memset (st,-1, sizeof (st) has not been accessed; int fa; // enter the parent node information of the node, and store for (int I = 2; I <= N; I ++) {scanf ("% d", & fa); add (fa, I );} // initialize the following struct, with the left and right ranges of node degrees/for (int I = 1; I <= N; I ++) {L [I]. clear (); R [I]. clear (); del [I]. clear () ;}int Q, u, l, r, d; // number of input operations scanf ("% d", & Q); while (Q --) {scanf ("% d", & u, & l, & r, & d ); // store the depth range of user input L [u]. push_back (l); R [u]. push_back (r); del [u]. push_back (d);} memset (tr, 0, sizeof (tr); // core algorithm, dfs-depth first search traversal dfs ); printf ("Case % d: % lld \ n", ++ cas, cal ();}/* 10121 1 2 2 2 2 3 3 3 10 11 */

3. Activity Center (similar to a previous Assembly issue, the three-way method is used ):

/** Main. cpp ** Created on: 2014-4-19 * Author: zy */# include <iostream> # include <string> # include <cstring> # include <vector> # include <cmath> // required by the pow Function header file # include <iomanip> // setprecision controls the number of decimal points using namespace std; long x [500000], y [500000]; const double eps = 1e-6; // precision int N; double cal (double m) {double ans = 0; for (int I = 0; I <N; I ++) {double len = sqrt (x [I]-m) * (x [I]-m) + y [I] * y [I]); ans = ans + len;} return ans;} int main () {int T; cin> T; double result; // output result int count = 1; while (T --) {// input data, replace cin> N; for (int I = 0; I <N; I ++) {cin> x [I]> y [I] ;}// core algorithm double left =-1000000; double right = 1000000; // three-point search algorithm while (1) {double mid = (left + right)/2; double midMid = (mid + right)/2; double retMid = cal (mid ); double retMidMid = cal (midMid); if (retMid <retMidMid) right = midMid; else left = mid; if (right-left <eps) {result = right; break ;}} // output result, which controls the double decimal point cout <"Case" <count <":"; cout <fixed <setprecision (6) <result <endl; count ++;} result = 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.