Programming little Practice OJ Question 05

Source: Internet
Author: User
Tags define null

Duplicate phone number

Look for a duplicate phone number in a file, all uppercase and lowercase letters are mapped to numbers according to the cell phone nine, the remaining characters are not considered, the duplicate number is restored after the output to the interface file, the number is less than 12 bits.

#include <stdlib.h> #include "PhoneBookProcess.h" #include <map> #include <string> #include < fstream> #include <iostream> #include <stdio.h>/* function: Check if two or more companies have the same phone number and statistics output input parameters: Infilename-       Contains the number of personalized phone number and list of the source file name [format See topic Description ppt] Outfilename-Output statistics Duplicate number results of the target file name [format See topic description PPT] Output parameter: No return: 0-Success 1-Other miscellaneous errors, such as file does not exist */long long str2num (std::string str) {int dict[] = { 2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};long long ago = 0;int T = 0;for (unsigned i = 0; i < str.length (); ++i) {if (Str[i] >= ' 0 ' && str[i] <= ' 9 ') T = str[i]-' 0 '; else if (Str[i] >= ' A ' && str[i] <= ' Z ') t = dict[str[i]-' a '];else if (str[i] >= ' A ' && str[i] <= ' Z ') t = dict[str[i]-' a '];elsecontinue;ago = ago * Ten + t;} return ago;} int phonebookprocess (const char *infilename,const char *outfilename) {std::ifstream inf (infilename); Std::ofstream Outf (outfilename); if (!inf | |!outf) return 1;std::map<long long,int> m;std::string Str;long long ago;inf >> str; Skip first line while (INF) {inf >> Str;ago = Str2Num (str), if (ago = = 0) continue;if (m.count (ago) = = 0) {M[ago] = 1;} else {m[ ago]++;} str = "";} Std::map<long long, int>::iterator it;bool isdup = false;for (it = M.begin (); It! = M.end (); ++it) {if (It->second & Gt 1) {Outf << it->first << "" << it->second << "\ n"; isdup = True;}} if (!isdup) {outf << "No duplicates.\n";} return 0;}

BYTE Stream parsing

For a char byte array, specify the position, specify the length, and parse out the number.

#include "OJ.h" #include <stdio.h> #include <stdlib.h> #include <string.h>void tobitchars (unsigned    Char a[], char *p, unsigned int len) {int I, j, K, length;    Char temp[10] = {'} '};        for (i = 0;i < Len; i++) {itoa ((int) a[i],temp,2);        Length = strlen (temp);            if (length < 8) {k = 7;            for (j = length-1; J >= 0;--j) temp[k--] = Temp[j];        for (j = 0; j < 8-length; ++j) temp[j] = ' 0 ';        } j = 0;    while (temp[j]) *p++ = temp[j++];    }}unsigned int Calvalue (char *p, int len) {unsigned int sum = 0;    for (int i = 0; i < len; ++i) sum = sum*2 + p[i]-' 0 '; return sum;} void Decode (unsigned int uiiutputlen, unsigned char ainputbyte[], unsigned int uielementnum, Element_stru astelemen    T[]) {if (Uiiutputlen = = 0 | | uielementnum = = 0 | |!ainputbyte | |!astelement) return;    Char buffer[1000]; Tobitchars (ainputbyte, buffer, UiiutPutlen);    int offset = 0;        for (int i = 0; i < Uielementnum; ++i) {int len = astelement[i].uielementlength;        Astelement[i].uielementvalue = calvalue (buffer + offset, len);    Offset + = Len; } return;

English Melody Contest

Get the score from the input string, remove the highest minimum score after the average score, output in accordance with the established format.

#include "OJ.h" #include <stdio.h> #include <string.h> #include <algorithm>void getresult (char* pinput[], int Num, char *presult) {int A[7]={0};char name[30]= ""; char line[40]= ""; Float sum = 9, avg=0;int sumhe=0,max,min; for      (int i=0;i<num;i++)  {  sscanf pinput[i], "%d%d%d%d%d%d%d%s", &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6 ],name);  Std::sort (A, a+7);  sum = 0;  for (int j = 1; j < 6; ++j) {sum + = A[j];  }  avg = SUM/5;  sprintf (line, "%s%4.2f", name,avg);  sprintf (PResult, "%s%s\n\0", Presult,line);  }    int Length=strlen (pResult);        

Deep Horse racing

Determine whether the yuan son can win, known that his horse can run past the number of enemy horses, similar to the field bogey horse racing, reasonable arrangements after the judge whether can win.

#include "OJ.h" #include <algorithm>/* function: Judging Yuanzi's horse will win? Yuanzi's horse wins and returns YES. Otherwise, no input parameters are returned: unsigned int num: number of horse races;   (1<= num <=1000) unsigned int * Speed_yz:yuanzi horse speed; unsigned int * speed_ OP: The speed of the opponent's horse; return value: char * type string, Yuanzi's horse wins, returns YES. Otherwise return No;*/char * ISYUANZIWIN (unsigned int num, unsigned int * speed_yz, unsigned int * speed_op) {std::sort (Speed_yz, speed _yz + num); Std::sort (speed_op, Speed_op + num), int i = 0, j = 0, Count = 0;while (i < num && J < num) {if (SPE Ed_yz[i] <= Speed_op[j]) {++i;} else {++i; ++j;++count;}} if (Count > Num/2) return "YES"; Elsereturn "NO";}

"Intermediate" Double-linked list basic operation

Insert Delete read, plus a large number to add.

#include <stdlib.h> #define NULL 0#define MAXSIZE 50struct strlnode{int data;struct strlnode *plast;struct Strlnode *pnext;}; void Create (struct strlnode **p, int x)/* Create a doubly linked list (table header node) */{struct strlnode *q;q = (struct Strlnode *) malloc (sizeof (struct ST Rlnode)); q->data = X;q->plast = Null;q->pnext = Null;*p = Q;return;} void Insertnode (struct strlnode **p, int i, int x)/* The node where the data equals x is inserted in the list I of the link */{/* code is implemented here */if (*p = null) return;struct Strl Node *a = *p;int len = 1;while (A->pnext! = null) {A=a->pnext;++len;} if (i > Len | | i <-1) return;struct strlnode* v = (struct strlnode*) malloc (sizeof (struct strlnode)); v->data = X;i F (i = = 0) {v->pnext = *p;v->plast = null; (*p)->plast = V;*p = v;} else if (i = = len) {a->pnext = V;v->plast = A;v->pnext = null;} else {a = *p;for (int j = 0; j < i-1; ++j) {a = a ->pnext;} V->pnext = A->pnext;v->plast = A;a->pnext = V;v->pnext->plast = V;}} void Deletenode (struct strlnode **p, int i)/* Delete list I nodes in list */The {/* code implements */IF here (*p = = null) return;struct Strlnode *a = *p, *tmp;int len = 1;while (A->pnext! = null) {A=a->pnext;++l En;} if (i > len-1 | | i <-1) return;if (i = = 0) {tmp = (*p)->pnext;tmp->plast = Null;free (*p); *p = tmp;} else if (i = = len-1) {A->plast->pnext = Null;free (a);} else {a = *p;for (int j = 0; J < i; ++j) {a = A->pnext;} A->plast->pnext = A->pnext;a->pnext->plast = A->plast;free (a);}} int getnodenum (struct strlnode **p)/* Gets the number of nodes in the list */{int Nodenum = 0;/* code is implemented here */struct strlnode *a = *p;while (a) {a = A-&G T;pnext;++nodenum;} return nodenum;} void Bignumberplus (struct strlnode **plus, struct strlnode **p, struct strlnode **q)/* Use linked list to implement large integer add */{/* code implemented here */int le NP = Getnodenum (p); int lenq = Getnodenum (q); int lenbig = LENP > Lenq?  Lenp:lenq;struct Strlnode *pt = *p, *QT = *q;int a[1000] = {0}, b[1000] = {0};for (int i = lenp-1; I >= 0;-I.) {A[I] = Pt->data;pt = Pt->pnext;} for (int i = lenq-1; I >= 0; i.) {B[i] = QT-&GT;DATA;QT = Qt->pnext;} for (int i = 0; i < Lenbig; ++i) {A[i] + b[i];if (A[i] >=) {a[i+1] + = A[i]/10;a[i]%= 10;}} if (A[lenbig] > 0) {++lenbig;} (*plus)->data = A[lenbig-1];int i = lenBig-2, j = 1;while (i >= 0) {Insertnode (plus, j + +, a[i--]);}} void Readtolnode (struct strlnode **p, int *a, int size)/* Writes an array to the list, the order of the data in the linked list is consistent with the sequence in the arrays */{int j = 0;int data = 0;str  UCT Strlnode *s = *p;s->data = * (A + (size-1)); for (j = 2; J < (Size+1); j + +) {data = * (A + (size-j)); Insertnode (p, 0, data);} return;} void writetosqlist (int *a, struct strlnode *p)/* Writes the list to the array, the order of the data in the array and the order in the list are consistent */{int j = 0;struct Strlnode *s = P;wh Ile (s! = null) {* (A + j) = S->data;s = s->pnext;j++;} return;}

Matrix multiplication 2

Finding the result of multiplying a matrix

#include "oj.h"/* Function: Matrix multiplication input: MATRIXA,MATRIXB output: Matrixc return: 0*/int matrix (int **matrixa, int **matrixb, int **matrixc, int N) {int *a = (int *) Matrixa, *b= (int *) MATRIXB, *c= (int *) matrixc;for (int i = 0; i < N; ++i) {for (int j=0; j < N; ++j {C[i * n + j] = 0; for (int k = 0; k < n; ++k) {C[i * n + j] + = A[i * n + K] * b[k * n + j];}}} return 0;}

Home+work

Each of the two elements in each row is the time required to complete the test paper, the value of the test, and the maximum value that can be obtained.

#include "OJ.h" #include <algorithm> #include <vector>/* input: Npapers indicates the number of quiz papers (1≤papers≤20), Nremain indicates the time remaining ( 1≤nremain≤10000), paper[][2] is an array of papers*2, and the two elements of each row are followed by the time required to complete the test paper, the value output of the test paper: *pmaxvalue for the maximum value to get back: 0: Exception 1: Calculation successfully returned */struct pertimevalue {double per;int time;int value;bool operator < (const pertimevalue &k) const{return PE R > K.per;}};    int getmaxvalue (int npapers, int nremain, int paper[][2], double* pmaxvalue) {std::vector<pertimevalue> v;    Pertimevalue A;        for (int i = 0; i < npapers; ++i) {a.per = paper[i][1] * 1.0/paper[i][0];        A.time = paper[i][0];        A.value = paper[i][1];    V.push_back (a);    }std::sort (V.begin (), V.end ()); *pmaxvalue = 0;std::vector<pertimevalue>::iterator it;for (it = V.begin (); It! = V.end (); ++it) {if (NRemain > it-& Gt;time) {*pmaxvalue + = it->value;nremain-= It->time;} else {*pmaxvalue + = It->per * nremain;break;}} return 0;}


Programming little Practice OJ Question 05

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.