Algorithm Exercise (i)

Source: Internet
Author: User

Yesterday is a memorable day, I took the mathematical modeling of the recommended national first prize, hope that the final can be successfully won the COUNTRY. Now the junior School is one months OLD. This one months because the social practice appraising thing really very busy, fortunately finally got all the prizes that may have Won. The results of their own research assistants this matter to delay, today to find Ma teacher, the results of MA Teacher's Laboratory people are Full. So there is no way, I may have to find another teacher.

There is no me in this Year's national scholarship, just forget it. hardships, study hard, This semester is very important. So I'm going to start preparing for the trial, now the training is very important, anyway, this is the future of their own way. I am the most important now is level six, learning, Machine test, Scientific research, these four indispensable. Come on, come on! So today is the basic part of C-language algorithm: Top-down programming Method.

1.uva1339 Old Code.

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;intcnt1[ -],cnt2[ -];Chars1[ the],s2[ the];intmain () { while(SCANF ("%s%s", S1,s2) = =2){        intLen1 =strlen (s1); intLen2 =strlen (s2); Memset (cnt1,0,sizeof(cnt1)); Memset (cnt2,0,sizeof(cnt2));  for(inti =0; i<len1;++I) {             for(intj =0;j< -;++J) {                if(s1[i]- $==j) {cnt1[j]++; break;} }        }         for(inti =0; i<len2;++I) {             for(intj =0;j< -;++J) {                if(s2[i]- $==j) {cnt2[j]++; break;} }} Sort (cnt1,cnt1+ -); Sort (cnt2,cnt2+ -); BOOLEQ =1;  for(inti =0;i< -;++I) {            //printf ("%d%d\n", cnt1[i],cnt2[i]);            if(cnt1[i]!=cnt2[i]) {eq =0; break;} }        if(eq) {printf ("yes\n"); }Else{printf ("no\n"); }    }    return 0;}

2.uva489 Hangman Judge

#include <stdio.h>#include<string.h>#defineMAX 100Chars1[max],s2[max];BOOLWin =0, lose =0;intleft,chance;voidGuessCharC) {    intLen =strlen (s1); BOOLFind =0;  for(inti =0; i<len;++I) {        if(s1[i]==C) { left--; s1[i]=' '; Find=1; }    }    if(!find) chance--; if(!chance) lose =1; Else if(!left) win =1;}intmain () {intrd;  while(SCANF ("%d%s%s", &rd,s1,s2) = =3&&rd!=-1) {win=0; lose =0; printf ("Round%d\n", rd); Chance=7; left=strlen (s1); intLen =strlen (s2);  for(inti =0; i<len;++I)            {guess (s2[i]); if(win| | Lose) break; }        if(win) {printf ("you win.\n"); }Else if(lose) {printf ("you lose.\n"); }Else{printf ("you chickened out!\n"); }    }    return 0;}

3.uva133 the Dole Queue

#include <stdio.h>#include<string.h>intn,k,m;//people themselves are sorted by counter-clockwiseintGointPintDintTintA[]) {     while(t--){         do{p= (p-1+d+n)%n+1; } while(a[p]==1); }    returnp;}intmain () { while(SCANF ("%d%d%d", &n,&k,&m) = =3&&N) {            inta[n+1]; Memset (a,0,sizeof(a)); intP1 =n; intP2 =1; intleft =n;  while(left) {//the number has surplusP1 = Go (p1,1, k,a); P2= Go (p2,-1, m,a); a[p1]=1; a[p2]=1; printf ("%3d", p1); --left ; if(p2!=p1) {printf ("%3d", p2);--left ;} if(left) printf (","); } printf ("\ n"); }    return 0;}

4.uva213 Message Decoding

#include <stdio.h>#include<string.h>intcode[8][1<<8];intReadchar () { for(;;) {        intCH =GetChar (); if(ch!='\ r'&&ch! ='\ n')returnch; }}intReadintintC) {    intv =0;  while(c--) {v= v*2+readchar ()-'0'; }    returnv;}intreadcodes () {memset (code,0,sizeof(code)); code[1][0] =Readchar (); intch;  for(intLen =2;len<8;++Len) {         for(inti =0;i< (1<<len)-1;++I) {ch=GetChar (); if(ch==eof)return 0; if(ch = ='\ n'|| ch = ='\ r')return 1; code[len][i]=ch; }    }    return 1;}intmain () { while(readcodes ()) { for(;;) {            intLen = Readint (3); if(len==0) break;  for(;;) {                intv =Readint (len); if(v = = (1<<len)-1) break;            Putchar (code[len][v]); }} printf ("\ n"); }    return 0;}

Summary: each of the above Small program code volume is not more than 50 lines, but are very classic small program, using Top-down programming method, making the logic of the program is very clear.

Algorithm Exercise (i)

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.