第五章-算法入门

5.1 equation

#include<iostream> 
#include<stdio.h> 
#include<string> 
using namespace std;  

double calcul(double x)  
{  
    return 8*x*x*x*x + 7*x*x*x + 2*x*x +3*x + 6;  
}  

int main()  
{  

    double y;  
    cin >> y;  

    if(y < 6 || y > calcul(100))  
    {  
        cout << "No solution!" <<endl;  
    }  
    else  
        {   
        double x;  
        double left = 0;  
        double right = 100;  
        x = (left + right) / 2;  

        while(calcul(x) != y || (right - left) > 1e-8)  
        {  
            if(calcul(x) == y || (right - left) < 1e-8)  
            {  
                printf("%.4lf",x);  
                return 0;  
            }  
            else if(calcul(x) > y)  
            {  
                right = x;  
            }  
            else if(calcul(x) < y)  
            {  
                left = x;  
            }  
            x = (left + right) / 2;  
        }  

    printf("%.4lf\n",x);  

    }  

    return 0;  
}  

5.2 sequences
第五章-算法入门_第1张图片
(使用64位整型)

#include<iostream> 
#include<stdio.h> 
#include<string> 
using namespace std;  

int main()  
{  

    __int64 a1,a2,a3;  
    int k;  
    scanf("%I64d %I64d %I64d %d",&a1,&a2,&a3,&k);  

    a1 = a1;  
    a2 = a2;  
    a3 = a3;  

    __int64 d,q1,q2;  
    d = a2 - a1;  
    if(d == (a3-a2))  
    {  
        cout << "arithmetic sequences. "<<endl;  
        printf("%I64d\n",(a1 + (k-1) * d) % 1000000007);  
        return 0;  
    }  


    __int64 q = a2 / a1;  
    k--;  
    cout << "geometric sequences. " << endl;  

    //快速幂 
    __int64 res = a1;  
    while(k>0)  
    {  
        if(k&1)  
            res = (res*q)% 1000000007;  
        q = (q*q) % 1000000007;  
        k>>=1;  
     }   

    printf("%I64d\n",res % 1000000007);  

    return 0;  
}  

5.3 Game_Prediction
第五章-算法入门_第2张图片

#include<iostream> 
#include<stdio.h> 
#include<string> 
using namespace std;    

bool arr[10086];    
int main()    
{    
    int count = 0;  
    int m,n;    
    cin >> m >> n;    
    int a;    
    int i;    
    for(i =0; i < n ;i++)    
    {    
        cin >> a;    
        arr[a] = true;    
    }    


    int max = m * n;    
    while(arr[max])    
    {    
        count++;    
        max--;    
    }    
    //max为对方最大牌 

    i = 1;    
    while(arr[i])    
    {    
        i++;    
    }    
    //i为对方最小牌 

    while(!arr[i])    
    {    
        i++;    
    }     
    //i为我方有效最小牌 


    int neg = 0; //相连0的数量 
    int pos = 0; //相连1的数量 
    for(int j = max; j >= i; j--)    
    {    
        while(!arr[j])    
        {  
            neg++;   
            j--;  
        }  

        while(arr[j])      
        {  
            pos++;       
            j--;  
        }  

        if(neg >= pos)   
        {  
            neg -= pos;  
            pos = 0;  
        }  
        else   
        {  
            count += neg;  
            pos -= neg;  
            neg = 0;  
        }  
    }    

    cout << count <<endl;    

    return 0;    
}    

5.4 Painter
第五章-算法入门_第3张图片

#include<iostream> 
#include<stdio.h> 
#include<string> 
#include<algorithm> 
using namespace std;  

double arr[15];  
double minu[15];  
int main()  
{  

    int i,j;  
    int n;  
    cin >> n;  
    for(i = 1; i <= n;i++)  
        cin >> arr[i];  
    cin >> arr[0]; //0为需要的灰色颜料 

    double ml = 0;  
    bool flag = false;  
    bool flag2= true;  
    double count = 0;  

    for(i = 1; i<= 40; i++)  
    {  
        if(flag) break;  

        ml = i*50; //买到颜料体积 

        flag2 = true;  
        for(j = 1; j <= n; j++)  
        {  
            if(arr[j] > ml) //需要超过买到 
            {  
                flag2 = false;  
                break;  
            }  
            else  
                minu[j] = ml - arr[j];  
        }  

        count = 0;  
        while(flag2)  
        {  
            sort(minu+1,minu+n+1);  
            if(!minu[n-2]) break;  

            count++;  

            if(count >= arr[0])  
            {  
                flag = true;  
                break;  
            }             

            minu[n]--;  
            minu[n-1]--;  
            minu[n-2]--;  
        /// cout <<minu[12]<<" "<<minu[11]<<" "<<minu[10]<<endl; 
        }  
    }  

    cout << i-1;   

    return 0;  
}  

5.5 Best_Cow_Line

#include<iostream> 
#include<stdio.h> 
#include<string> 
#include<algorithm> 
using namespace std;  

int main()  
{  

    int n;  
    cin >> n;  
    string s;  
    cin >> s;  

    int left = 0;  
    int right = n-1;  
    bool flag;  

    while(left <= right)  
    {  
        flag = false;   

        //确定left与right较小的一个,相同时比较下一位 
        for(int i = 0; i+left < right; i++)  
        {  
            if(s[i+left] < s[right-i])   
            {  
                flag = true;  
                break;  
            }  
            else if(s[i+left] > s[right-i])  
            {  
                flag = false;  
                break;  
            }  
        }  

        if(flag)  
        {  
            cout << s[left];  
            left++;  
        }  
        else  
        {  
            cout << s[right];  
            right--;  
        }  
    }     
    return 0;  
}  

你可能感兴趣的:(第五章-算法入门)