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;
}


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

No comments:

Post a Comment