It company pen question algorithm part (four) __ algorithm

Source: Internet
Author: User
the , now Xiao Ming's family over a bridge, when the bridge is the night, so must have lights. Now Xiao Ming Bridge to 1 seconds, Xiao Ming's brother to 3 seconds, Xiao Ming's father to 6 seconds, Xiao Ming's mother to eight seconds, Xiao Ming's grandfather to 12 seconds. The bridge can be up to two people each time, and the speed of the bridge depends on the slowest bridge, and the lamp will be extinguished 30 seconds after ignition. Ask Xiao Ming's family How to cross the bridge. (originally an intellectual problem, which is solved by a program) #include "stdafx.h" #define n    5 #define SIZE  //Number of people: xiaoming-0, brother-1, papa-2, Mom-3, Grandpa-4//Everyone's current position: 0- -On the left side of the bridge, on the right side of the bridge, an array subscript for the temporary scheme of int position[n];   //bridge, temporary scheme, minimum time scheme, int Index, tmpscheme[size], scheme[size];   //Minimum bridge time sum, initial value 100; the time required for everyone to cross the bridge int mintime=100, Time[n]={1, 3, 6, 8, 12}; //Find the best bridge scheme. Remnant: Number of people not crossing the bridge; Curtime: Current time used; Direction: Crossing direction, 0--right, left void Find (int remnant, int curtime, int Direction) {    if (remnant==0) {&nbs p;                              //Everyone has crossed the bridge, update the minimum time and plan          Mintime=curtime;         for (int i=0; i<size && tmpscheme[i]>=0; i++)          {            scheme[i]= Tmpscheme[i];        }     }else if (direction==1) {                        // Cross the direction to the right, select two people from the left of the bridge Bridge         for (int i=0; i<n; i++)                               {            if (position[i]==0 && curtime+time[i]<mintime) {                 tmpscheme[index++] = i;                 Position[i] = 1;                 for (int j=0; j<n; j+ +)           &nbsP                 {                     int Tmpmax = ( TIME[I]&GT;TIME[J]? Time[i]: time[j]);                      if (position[j]==0 && curtime+tmpmax<mintime)                      {                         tmpscheme[ index++] = j;                             Position[j] = 1;                                 Find (Remnant-2, Curtime+tmpmax,!) Direction);                          Position[j] = 0;                                  Tmpscheme[--index] =-1;                     }                }                  Position[i] = 0;                 Tmpscheme[--index] =- 1;            }        }    }else{       //bridge direction left, Pick a person from the right side of the bridge. Send lights         for (int j=0; j<n; j + +)          {            if (position[j]==1 && CURTIME+TIME[J] < Mintime)             {                 tmpscheme[index++] = j;                 Position[j] = 0;                 Find (remnant+1, CURTIME+TIME[J],! Direction);                 Position[j] = 1;                 Tmpscheme[--iNdex] =-1;             }        }    }} int main (int argc, char* argv[]) {    for (int i=0; i<size; i++)   &nbs p;   //Initial scheme content is negative, avoid and personnel label Conflict         Scheme[i] = tmpscheme[i] =-1;   Find (N, 0, 1);                   //Find best practices       printf ("mintime=%d:", mintime),  //output Best practices     for ( int i=0; I<size && scheme[i]>=0; i+=3)         printf (" %d-%d %d", Scheme[i], scheme[i+1], scheme[i+2]) ;     printf ("/b/b ");     return GetChar (); }   - , November 2005 Jinshan Pen test. The encoding completes the following processing function. The function moves the character ' * ' in the string to the front of the string, preceded by a non-' * ' character, but cannot change the order of the non-' * ' characters, and the function returns the number of characters ' * ' in the string. If the original string is: ab**cd**e*12, after processing is *****abcde12, the function and return value is 5. (requires the use of as little time and auxiliary space as possible) int change (char *str)                      /* This algorithm is not efficient, it is more efficient to search from backward to forward * * {     int count = 0;   & nbsp;                   /* Number of characters ' * ' in the string */     for (int i=0, j=0; str[i]; i++)       /* Re-string First start traversal */     {         if (str[i]== ' * ') {                 /* Encounters characters ' * ' */               for (j=i-1; str[j]!= ' * ' &&j>=0; j--)/ * Using the idea of similar insertion sorting, * * Front * *                     str[j+1]=str[j];             /* Non-* characters move back one after the other until a * character is encountered */               STR[J+1] = ' * ';               count++;         }     }      return count; } int main (int argc, char* argv[]) {     char str[] = "ab**cd**e*12";      printf ("str1=%s/n", str);      printf ("str2=%s, count=%d", str, change (str));      return GetChar (); }///Finally get a more efficient algorithm, a netizen to provide, should and Jinshan interviewer the idea of the same. The algorithm is as follows: int change (char *str) {     int i,j=strlen (str)-1;      for (i=j; j>=0; j--)      {         if (str[i]!= ' * ') {               i--;         }else if (str[j]!= ' * ') {              str[i] = str[j];               Str[j] = ' * ';               i--;         }     }      return i+1; }   - , November 15, 2005, Huawei Software development pen test. Implement a single-linked list reversal. #include "stdafx.h"   typedef char eletype;      //define data types in a linked list typedef struct LISTNO de    //Define single-linked list structure {     eletype data;      struct ListNode *next; }node;   node *create (int n)         //Create a single-linked list, n is the number of nodes {     Node *p = (node *) malloc (sizeof (node));        node *head = p;    head->data = ' A ';      for (int i= ' B '; i< ' A ' +n; i++)      {                             p = (P->next = (node *) malloc (sizeof (node)));          p->data = i;          P->next = null;            } &NBSP;&NBSP;&Nbsp;  return head; }     void print (node *head)      //list the elements in the linked list in list order {     for (; hea D Head = head->next)          printf ("%c", Head->data);        printf ("/n"); }   Node *reverse (node *head, node *pre) // reverses the single-linked list function. This is the most important function to write when writing a written test.{Node *p=head->next;      Head->next = pre;      if (p) return reverse (P, head); else return head;      } int main (int argc, char* argv[]) {Node *head = Create (6);      Print (head);      Head = reverse (head, NULL);      Print (head); return GetChar (); }   - , the code implements the function of the string to the integral type (realizes function Atoi function), is said to be the Shenzhou digital pen question. such as the string " +123 " à 123, " -0123 " à -123, "123CS45" à 123, " 123.45CS " à 123, " CS123.45 " à 0 #include "stdafx.h" int str2int (const char *STR)         //String-to-integer function { & nbsp;   int i=0, sign=1, value = 0;      if (str==null) return NULL;     //empty string directly returns null      if (str[0]== '-' | | | str[0]== ' + ') { //Determines if there is a sign bit          i = 1;          sign = (str[0]== '-'? -1:1);     }      for (; str[i]>= ' 0 ' && str[i]<= ' 9 '; i++)   & nbsp If it is a number, continue conversion          value = value * + (Str[i]-' 0 ');      return sign * value; } int main (int argc, char *argv[]) {     char *str = " -123.45CS67";      int

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.