Blue Bridge Cup notes

I, on the header files

// Universal header files #include <bits / stdc ++. H >

1、#include<cstring>: strlen,  strcmp,  strcat,  strcpy, memset ,substr

(. 1) Memset (DP, 0 , the sizeof (DP)) ; // generally used to clear the array

Function prototype: void * memset (void * s, int ch, size_t n);

(2) take the substrings: substr ( Start [, length] )

2、#include<cmath>: double fabs()

3、#include<algorithm>: max, min, int  abs, swap, reverse, sort,

lower_bound( begin,end,num),  upper_bound( begin,end,num)

(1)reverse( v.begin() , v.end() )

(2) sort (start, end, sorted) 

   In small to large sorted array: first with the sort order, from small to large default sort (a, a + 10) ; // The second is the next address to the last sort of address

#include<algorithm>

int main () {

  int a[10]={9,6,3,8,5,2,7,4,1,0};

  for(int i=0;i<10;i++)

  cout<<a[i]<<endl;

  sort (a, a + 10) ;} // end is the second address ( the next address to a last address sort )

Descending: sort (a, a + 10, compare);

#include<algorithm>

bool compare(int a,int b)

{

return a>b;

}

int main ()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for(int i=0;i<10;i++)

cout<<a[i]<<endl;

sort (a, a + 10, compare); // here do not need to pass parameters to the compare function,

// this is the rule

(3)

lower_bound (begin, end, num) : begin from a position in the array to the end-1 position of the first binary search num equal to or greater than a number, the number returned to find the address end does not exist is returned. Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.

upper_bound, (begin, end, num) : begin from a position in the array to the end-1 position of the binary search num greater than the first number, and returns this number to find the address does not exist end returns. Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.

4、#include<stdlib.h>malloc()、rand()、free()、system()

Second, the kinds of questions

Results fill in the blank  subject description solution having a problem is determined. Solution requires players to fill in the blank questions.  does not require problem solving process, problem-solving means no limit (you can use any development language or tool, or even a manual calculation) is only required to fill out the final result

Problem solving :

1. (1) to the date: Usually hands were calculated using Excel, on (leap year, average year)

(2) calculate the numbers: generally real problem, to buy things like, pay attention to the type of precision

Examples: Beer 2.3 yuan per pot, drinks 1.9 yuan per pot. Xiao Ming bought some beer and beverages, spent a total of 82.3 yuan.

We also know that he bought beer less than the number of drinks, you calculate he bought a few cans of beer.

Ideas: Loop through, attention to accuracy problems, !! money by 10 the number of calculations.

example:

Pirates comparison than the drinker float

A group of pirates (not more than 20 people), Competition drinker on board. As follows: Open a bottle of wine, all those present split the drink, there are several people fell. Then open a bottle of wine equally, have fallen, repeated again ...... until opened the fourth bottle of wine sitting has been depleted , Corsair longer among them. When the first split the 4 bottles of wine to drink, we all fall down.

The captain wrote: "...... Yesterday, I just drank a bottle .. 

Based on this information you infer how many people start each round drink down left many people did not fall.

For example, one might be: 20,5,4,2,0

Multiple answers the order is not important.

Write your answer "answer .txt" Do not write here!

Answer:

18,9,3,2,0 (1 min)

15,10,3,2,0 (2 minutes)

20,5,4,2,0 (0)

12,6,4,2,0 (2 minutes)

The core problem is that pirate captain just drank a bottle of wine, assuming that the number of each round is n, a, b, c, then the captain of drinking wine is 1 / n + 1 / a + 1 / b + 1 / c if the result is 1, then it may be a set of solutions, but also to ensure because some additional conditions, first n> a> b> c, and when the determination is 1, is not used as a double score, and is a decimal, so accuracy will definitely fall, so long as sum-1.0 <0.0000001 on it. Or common denominator (i.e. molecules equal denominator)

int main()

{

    int n,a,b,c;

    for(n=1;n<=20;n++)  {

       for(a=1;a<=20;a++)  {

           if(a<n)

           for(b=1;b<=20;b++)  {

              if(b<a)

              for(c=1;c<=20;c++) {

                  if(c<b) {

                     //double sum=1.0/n+1.0/a+1.0/b+1.0/c;

                     //if(abs(sum-1.0)<0.0000001)  或

                     if(n*a*b*c==a*b*c + n*b*c + n*a*c + n*a*b)  {

                         cout<<n<<' '<<a<<' '<<b<<' '<<c<<endl;    }

 

2、3

General: the number of phases on a plurality of unequal: Priority && and || plus continue

example:   

int a,b,c,d,e,f,g,h;

       for(a=1;a<=9;a++)

        for(b=0;b<=9;b++) {

             if(a!=b)

             for(c=0;c<=9;c++) {

                    if(c!=a&&c!=b)

                    for(d=0;d<=9;d++) {

                           if(d!=a&&d!=b&&d!=c)

                           for(e=1;e<=9;e++) {

                                  if( e!=a && e!=b && e!=c && e!=d)

                                  for( f=0; f<=9; f++) {

                                         if(f!=a && f!=b && f!=c && f!=d && f!=e)

                                         for(g=0; g<=9; g++) {

                                                if(g!=a && g!=b && g!=c && g!=d && g!=e && g!=f)

                                                for(h=0; h<=9; h++) {

                                                       if(h!=a && h!=b && h!=c && h!=d && h!=e && h!=f && h!=g && (1000*a+100*b+10*c+d+1000*e+100*f+10*g+b)==(10000*e+1000*f+100*c+10*b+h))

                                                       cout<<e<<' '<<f<<' '<<g<<' '<<b<<endl;

                                                }  

Arithmetic sequence Sn = [n * (a1 + an)] / 2 is usually 2,3 ... compared n * (n + 1) / 2   

 

Code fill in the blank only fill vacant part, do not fill in complete sentences . Do not write a note about content or other topics not required. Universal code should be filled, not just specific examples of the problems given effective surface.

Problem solving :

First directly pasted code to run, the results of observation operation .

In fact, generally comprises a recursive dynamic programming, to a function name or the name of the array do [+ -1 i] [+ -1 j] or other changes a; back: the ships may want to swap exchange

Answers to some of the topics:

14:(1)if(r>0)return i    (2) f(a,rank-1,row, col + w/2)

15:(1)(width-strlen(s)-2)/2,"",buf,(width-strlen(s)-1)/2,"" 

(2){t=x[k]; x[k]=x[i]; x[i]=t;}

16:(1)swap(a,p,j)  (2)f(a,k+1,m-i,b)

17:a[i][j]=a[i-1][j-1]+1    18: quick_select(a, i+1, r, k-(i-l+1))

Dynamic Programming:

Digital Triangles

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

How to go from the top makes the weight go down, you can only go below or lower right, the whole journey, ask your greatest

dp [i] [j]: representatives optimal value obtained i-th row and j are listed from

dp【i】【j】=maxdp【i+1】【j】,dp【i+1】【j+1】+a【i】【j】

 

Programming big issue      subject to the general use of standard input and output. Title typically will give an example of data. The difficulty of general topics focused on organization and logic design of algorithms. Method player analysis should be universal, the sample data is not only applicable to the subject (of course, at least sample data should be applied to the subject ). To test the performance of the players given the solution, with the score when the test case may contain a large amount of data Pressure test cases , as much as possible to consider when selecting players algorithm feasibility and efficiency issues

Problem solving :

The first channel will usually simple.

Enumeration, search, simulation is the largest number of Blue Bridge Cup title categories

//输入两个整数a和b,输出这两个整数的和。a和b都不超过100位。

    // #include<iostream>

   // #include<stdio.h>

   // #include<string.h>

    #include<bits/stdc++.h>

    using namespace std;

    int main()

    {

       int i,r=0,max,lena,lenb;

       char s1[100],s2[100];

        int a[100],b[100];

        gets(s1);gets(s2);

       lena=strlen(s1);lenb=strlen(s2);

        for(i=0;i<lena;i++) a[i]=s1[lena-i-1]-'0';

        for(i=0;i<lenb;i++) b[i]=s2[lenb-i-1]-'0';//将s1,s2分别倒序赋给a,b(高精度加法是从末位开始往前加)

        if(lena>=lenb) max=lena;

        else max=lenb;

        if(lena>lenb) for(i=lenb;i<lena;i++) b[i]=0;

        if(lena<lenb) for(i=lena;i<lenb;i++) a[i]=0;//当a,b位数不同时,给位数少的赋0,保证后面的运算

        int c[102];

       

        for(i=0;i<max;i++)

       {   c[i]=r+a[i]+b[i];

           c[i]=c[i]%10;

           r=c[i]/10;

        }

       while(r!=0)

        {max++;

        c[max-1]=r%10;

        r=r/10;  

       }

        for(i=max-1;i>=0;i--)  cout<<c[i];

        cout<<endl;

    return 0;

}

Base conversion:

// decimal convert hexadecimal 

 // Ideas: constant modulo (on paper before writing stepped formula, taking up the last number from the next)

#include <iostream>

using namespace std;

int main()

{

   long long a;char s[10];int i=0,c=0,temp;//temp是int,s[]是char!!!

   cin>>a;

   if(a==0) cout<<0<<endl;       

   while(a!=0)

   {

      temp=a%16;c++;

      if(temp>=10) s[i++]=temp-10+'A';

      else s[i++]=temp+'0';//要看什么类型相加减,有int与char相加减,char会自动转成数字,输出的则看定义的是什么类型,再转化

      a=a/16;

   }

   for(i=c-1;i>=0;i--)  cout<<s[i];    

cout<<endl;

return 0;

}

// hex decimal turn

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <cmath>

using namespace std;

int  main( )

{

   char a[8]; gets(a);

   int i,c=strlen(a);long long t=0;//注意输出的是否够空间     

   for(i=0;i<c;i++)

   {

   if(a[i]>='0'&&a[i]<='9') a[i]=a[i]-'0';//不能省,因为如果没有这步,当a[i]=0时,下面会转成48与之相乘

   else a[i]=10+(a[i]-'A');//字符型的比较要注意,注意必须是要用到才能写,不能提前写

                         //这里的if else 不能放在前面!!!

   t=t+a[i]*pow(16,c-i-1);

   }    

   cout<<t<<endl;

return 0;}

 

// octal, hexadecimal turn

#include<iostream>

#include<cstring>

using namespace std;

 int main()

 { string s1,s2;//s1存输入的十六进制,s2存二进制

  int n; int i,j,k;

   cin>>n;

   for(i=0;i<n;i++)

   { cin>>s1; s2="";//对s2初始化

    for(j=0;j<s1.length();j++)

   { switch(s1[j])

    {    case '0':s2+="0000";break;

         case '1':s2+="0001";break;

         case '2':s2+="0010";break;

         case '3':s2+="0011";break;

         case '4':s2+="0100";break;

         case '5':s2+="0101";break;

         case '6':s2+="0110";break;

         case '7':s2+="0111";break;

         case '8':s2+="1000";break;

         case '9':s2+="1001";break;

         case 'A':s2+="1010";break;

         case 'B':s2+="1011";break;

         case 'C':s2+="1100";break;

         case 'D':s2+="1101";break;

         case 'E':s2+="1110";break;

         case 'F':s2+="1111";break;

         default:break; }

    }

         if(s2.length()%3==1)

         s2="00"+s2;

         if(s2.length()%3==2)

         s2="0"+s2;

         int flag=0;

         for(k=0;k<s2.length()-2;k+=3)

         { int p=4*(s2[k]-'0')+2*(s2[k+1]-'0')+s2[k+2]-'0';

         if(p) flag=1; //去掉前导,而且如果后面有0,那么前面已经使得 flag=1,所以能够输出

         if(flag) cout<<p; //如果只有 if(p) cout<<p;那么后面有0就输不出

         }

         cout<<endl;

  }

 return 0;

 }

Note:

const int MAXN = 100005;

int a[MAXN],b[MAXN],c[MAXN];

int n,sum;

int main()    //当数组比较大时定义在main函数之外

return;  role corresponds  break;  for interrupting the circulation action, and return 0; then return is another use, dedicated to the non-void return value of the function return value.

 

Guess you like

Origin blog.csdn.net/qq_42458302/article/details/92834897
Recommended