"Learning Journey in C #" Linq _2015 Programming Beauty Challenge Preliminary Codehunt Division

Source: Internet
Author: User

Preface

2015 Programming Beauty Challenge • The preliminary round is over, and the little friends seem to be playing happily on the hihocoder ...

I am the only one who silently in code Hunt ...

Ah, the silent full title of the 3SP ak yo ...

To write down the problem and the source of the puzzle ... (P.s There is something bad or wrong place please tell me oh, I will correct it immediately ~ ('? ') ))



Section xx BAIYUN00.01 Tutorial tutorial

Dot in there will be a small animation, kindly tell you how to play,

Beginners can look at this oh ~ ~

00.02 find the maximum value in Math there is a function that returns the maximum value between A and B: Math.max (A, A, a)

00.02using System;public class Program {public  static int Puzzle (int a, int b) {    return Math.max (A, b);}  }

00.03 Finding patterns

Gave two number pairs, careful observation to know (well actually I did so have experience) the answer is <A1B2+A2B1, a2b2>

00.03using System;public class Program {    //each array contains-elements public    static int[] Puzzle (int[] A, Int[] b) {      int[] t=new int[2];      T[0]=A[0]*B[1]+A[1]*B[0];      T[1]=A[1]*B[1];      return t;    }}

00.04 binary conversion (decimal to binary) binary conversion, at least decimal is the source number of the case is very convenient to have the corresponding function: The previous article has also mentioned: here and also wrote a C + + template: here

00.04using System;public class Program {public  static string Puzzle (int n) {    string j = convert.tostring (n, 2);    return J;  }}

00.05 element Move forward

This is a string separated by a space, each part as an element, the loop left n bits (of course equivalent to the n% element digits), the resulting new string.

Where the separation is implemented by the split function, the cyclic left shift can be achieved by newarr[i]=oldarr[(I+n)/len].

When the string array is finally linked together with a space, you need to use a join (A, B) function, the number in the array is connected together, 22 is separated by a.

00.05using System;public class Program {public    static string Puzzle (string s, int n)     {      string[] sarr=new St RING[100];      Sarr=s.split (");       int len=sarr.length;      String[] Ss=new String[len];      for (int i=0;i<len;i++) ss[i]=sarr[(n+i)%len];      Return String.Join ("", ss);    }}



Section BAOBAO01.01 determine if the elements are exactly the same

In order to determine whether the elements are exactly the same, I used the set operation in LINQ, simply to find (the non-intersecting part of a set) and the set of (the non-intersecting part of the set of B), if there is nothing in this set, it is exactly equal.

01.01using system;using System.linq;public class Program {public  static bool Puzzle (int[] A, int[] b)   {    in T[] Result=a.except (b). Concat (B.except (a)). ToArray ();    return result. length==0;}  }

01.02 ISBN Check Yes, see the vast number of this thing ... My first reaction was like everyone else: it's a god horse thing! But it's a very good conscience. There is a "978-" in front of each string to remind everyone, that is, to give a complete ISBN. We need to judge the True/false, in fact: this is not a qualified ISBN, judging standard is, from the front to look back, the first 12 bits, odd digits multiplied by 1, even the number of bits multiplied by 3, the result of 10 modulo, if the checksum 13th digit and this modulo add up just 10 or 0, This ISBN check is set up. (There are examples below) in order to test whether this ISBN is given in the topic, but also specifically wrote a C + + program to make judgments:
#include <iostream> #include <cstring> #include <string>using namespace Std;int n[13]={0};int main () { String S;while (Cin>>s && s.length ()) {int pos=0,ans=0;for (int i=0;i<s.length (); i++) {if (s[i]!= '-') n[ pos++]=s[i]-' 0 '; } for (int i=0;i<12;i++) {ans+=n[i]* (i%2?3:1);} Ans%=10;ans=10-ans;cout<<ans<<endl;} return 0;}

For more information about ISBN, please refer to Wikipedia: Here for students who have difficulty with ladders, or to stick a picture here

01.02using System;public class Program {public    static bool Puzzle (string s)     {      int ans=0;      int pl=0;      Char a= ' 0 ';      S=s.replace ("-", "" ");      for (int i=0;i<s.length-1;i++)      {        if (i%2==0) pl=1;        else pl=3;        ans+= ((int) s[i]-(int) a) *pl;      }      ans%=10; if (ans!=0) Ans=10-ans;      return ans== (int) s[s.length-1]-(int) A;    }}

01.03 binary conversion (decimal to quad)

When we find that all the numbers are hovering around 0-3, it is possible to feel a four-digit number, and the implementation of the conversion is not difficult:

01.03using System;public class Program {public    static string Puzzle (int n)     {      string s= "";      while (true)      {        int r=n/4,m=n%4;        S=m.tostring () +s;        N=r;        if (n==0) break;      }      return s;    }}

01.04 Convergence and intersection here also uses the function in LINQ, to find the intersection of sets, the place to note is that the direct intersection and cannot return int[] type, we need to add toarray ()
01.04using system;using System.linq;public class Program {public    static int[] Puzzle (int[] A, int[] b)     {
   return A.intersect (b). ToArray ();    }}


At this point, the problem solving report and complete code description is complete, if you do not understand or I have a wrong, or you have any better algorithm, please be sure to tell me in the following comments O (^▽^) o ~ ~

We all work together, progress together ~

by okcd00 (Candy King)


Topic

You may want to look at the data of the topic or check if it is really 3SP or something, the 3SP of each problem is as follows:

Section 00




Section 01






Scary 01.02 test Data


"Learning Journey in C #" Linq _2015 Programming Beauty Challenge Preliminary Codehunt Division

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.