Thursday, March 31, 2011

CPP_Camp_การบ้านการเขียนโปรแกรมวันที่-29 และ 31-มีนาคม-2554

1.Number Borrowing :

Ex:********************************************************************
Input :
3
15 12
16 9
101 9            
Output :
0
1
2
     
****************************************************************

Code:
//===============================================================//
#include<iostream>
#define x 9
using namespace std;

int main(){
    int num ;       // number of minusion <10
    int m[x],n[x];  // pair number minusion m-n ; m>n
    int mm[x],nn[x],mmm;
    int count[]={0,0,0,0,0,0,0,0,0};      // counter of borrowing
    int i,j;        // loop
    /*Get value of num , m , n */
   get_num:
    cin>>num;
    if(num>9){
        cout<<"Number invalid ! must <10 !"<<endl;
        num=0;
        goto get_num;
    }
    for(i=0;i<num;i++){
       get_m_n:
        cin>>m[i]>>n[i];
        mm[i]=m[i];
        nn[i]=n[i];
        if(m[i]<n[i]){
            cout<<"Input invalid ! m > n !"<<endl;
            goto get_m_n;
        }
    }

    /* Condition unit of pair minusion
    // if digit m < digit n --> add 1 to counter
    // and after add if digit after n = 0 then
    // consider of digit m = 0? if =0 add 1 to counter*/
    for(i=0;i<num;i++){
        while(m[i]!=0){
            int mdig=m[i]%10;
            int ndig=n[i]%10;
            if(mdig<ndig){
                count[i]+=1;
                m[i]/=10;
                n[i]/=10;
                if((m[i]%10)!=0){
                    m[i]-=1;
                }
                else{
                    mmm=m[i];
                    while((mmm%10)==0){
                        count[i]+=1;
                        mmm/=10;
                    }
                }



            }
            else{
                m[i]/=10;
                n[i]/=10;
            }
        }
        cout<<mm[i]<<"-"<<nn[i]<<"\t"<<"happend borrowing : ";
        cout<<count[i]<<endl;
    }
    return 0;
}


//===============================================================//
2.Diffy

Ex:********************************************************************
Input :
200  2   1   3
Output :
200  2   1   3
198  1   2   197
197  1   195 1
196  1   195 1
196  194 194 196
2    0   2   0
2    2   2   2
0    0   0   0
6
****************************************************************

Code:
//===============================================================//
#include<iostream>
#include<math.h>
#define N 4
using namespace std;

int main(){
    int n[N],m[N];     // number of 4 number
    int count = 0;  // counter
    int i;          // loop
    /*Get number of 4 number*/
    for(i=0;i<4;i++){
        cin>>n[i];
    }
    /*Consider Game*/
    for(i=0;i<4;i++){
        cout<<n[i]<<"\t";
    }
    cout<<endl;
    while((n[0]+n[1]+n[2]+n[3])!=0){
        count+=1;
        for(i=0;i<4;i++){
            m[i]=abs(n[i]-n[(i+1)%4]);
            cout<<m[i]<<"\t";
        }
        for(i=0;i<4;i++){
            n[i]=m[i];
        }
        cout<<endl;
    }
    cout<<count;
    return 0;
}


//===============================================================//
 

Monday, March 28, 2011

CPP_Camp_การบ้านการเขียนโปรแกรมวันที่-28-มีนาคม-2554

1.Parking fee :
     Concept : จงเขียนผังงาน เพื่อทำการคำนวนค่าจดรถตามอัตรานี้้ คือ 1 ชั่่วโมงละ 20 บาท ชั.......

Ex:********************************************************************
Input :
             Enter hour : 1
      Enter minute : 10
Output :
      Parking fee =20 baht
****************************************************************

Code:
//===============================================================//


#include<iostream>  // Parking fee : under 1h : 20 after 10,upper than 15min = 1hour
using namespace std;
int main(){
    int h,m,c=0;
    loop:
        cout<<"\nEnter hour(<=72) : ";cin>>h;
        cout<<"\nEnter minute : ";cin>>m;
        if(h<0||h>72||m<0||m>59){
            cout<<"\nEnter again!\n\n";
            goto loop;
        }
        if(h==0){
            if(m>15)
                c+=20;
            else
                c+=0;
        }
        if(h>0){
            if(m>15)
                c+=10;
            c+=20;
            while(h>1){
                c+=10;
                h--;
            }
        }
        cout<<"\n\nParking fee = "<<c<<" baht";
        cout<<"\n\nDo it again or not y/n : ";
        char o;
        cin>>o;
        if(o=='y')
            {c=0;goto loop;}
    return 0;
}


//===============================================================//
2.Histogram :
     Concept : 

Ex:********************************************************************
Input :
             Enter n : 5
      Enter 5 number :
      3
      5
      1
      0
      6
Output :
      Histogram :
      3     ***
      5     *****
      1     *
      0     
      6     ****** 
****************************************************************


Code:
//===============================================================//
#include<iostream> // Histogram
using namespace std;
int main(){
    int n , X[10];
    loop1:
        cout<<"\nEnter n : ";cin>>n;
        if(n<=0||n>10){
            cout<<"\nEnter n again ! (0<n<=10) ";
            goto loop1;
        }
        cout<<"\nEnter "<<n<<" number : \n";
        for(int i=0;i<n;i++){
            loop2:
                cin>>X[i];
                if(X[i]<0||X[i]>25){
                    cout<<"\nEnter that number again (0<=X<=25)\n\n";
                    goto loop2;
                }
        }
        cout<<"\n\nHistogram : \n";
        for(int i=0;i<n;i++){
            cout<<X[i]<<"\t";
            for(int j=0;j<X[i];j++)
                cout<<"*";
            cout<<endl;
        }
        return 0;
}
//===============================================================//
3.Reverse and multiply :
     Concept : 

Ex:********************************************************************
Input :
             321
Output :
      246 
****************************************************************

Code:
//===============================================================//
#include<iostream>  // Reverse and multiply
using namespace std;
int main(){
    int n,m,reverse = 0; // n:number
    loop1:
        cout<<"\nEnter number : ";cin>>n;
        if(n<=0||n>9999){
            cout<<"\nPlease Enter number again !(0<number<=9999)\n";
            goto loop1;
        }
        while(n>0){
            m = n%10;
            n/=10;
            reverse = reverse*10+m;
        }
        cout<<"\nResult : "<<reverse*2;
    return 0;
}


//===============================================================//

4.Anagram :
     Concept : 

Ex:********************************************************************
Input :
             Enter first word : khmers
           Enter second word : merskh
Output :
     Yes
****************************************************************

Code:
//===============================================================//
#include<iostream>
#include<string.h>
using namespace std;

int main(){
   /*set 1st word , 2nd word ,
    location of order , length of word */
   char w1[10],w2[10];
   int loca=0,len=0;
   char charator;
   string answer="Yes";
 w1:
   cout<<"Enter first word : ";
   cin>>w1;
   /*Length of both must <=10 charator*/
   if(strlen(w1)>10){
    cout<<w1<<" is so long\nTry that again!(<=10charator)\n";
        goto w1;
   }
 w2:
   cout<<"\nEnter second word : ";
   cin>>w2;
   if(strlen(w2)>10){
    cout<<w2<<" is so long\nTry that again!(<=10charator)\n";
        goto w2;
   }
   /*condition of 2word's length equaltion*/
   if(strlen(w1)!=strlen(w2)){
        cout<<"\nThat 2words have no the same length ! try them again!\n";
        goto w1;
   }
   /*Copy length both word to len*/
   len=strlen(w1);
   /*order charactor of both word to compare , */
   for(int i=0;i<len;i++){
        charator=w1[i];
        loca=i;
    for(int j=i+1;j<strlen(w1);j++){
        if(charator>w1[j]){
            charator=w1[j];
            loca=j;
                }
        }
    /*Swape charator when order charator*/
    char temp;
    temp=w1[i];
       w1[i]=w1[loca];
        w1[loca]=temp;

   }
   for(int i=0;i<len;i++){
       charator=w2[i];
        loca=i;
        for(int j=i+1;j<strlen(w2);j++){
        if(charator>w2[j]){
            charator=w2[j];
        loca=j;
        }
        }
    char temp;
        temp=w2[i];
    w2[i]=w2[loca];
        w2[loca]=temp;

   }
   /*compare both word that was ordered
   and condition if unequal even one charactor
   give a word No to answer*/
   for(int i=0;i<len;i++){
           if(w1[i]!=w2[i])
           answer="No";
   }
    /*Display answer !!*/
   cout<<endl<<answer<<endl;
   return 0;
}

//===============================================================//