1. 程式人生 > >演算法競賽入門經典(第二版)習題解答參考(第二章)

演算法競賽入門經典(第二版)習題解答參考(第二章)

習題2-1 水仙花數

#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
    int a = 0,b = 0,c = 0;
    for(int i = 100;i<1000;i++)
    {
        a = i%10;
        b = i/10%10;
        c = i/100;
        if( (a*a*a + b*b*b + c*c*c) == i)
        {
            cout<<i<<endl;
        }
    }   
}

習題2-2 韓信點兵

#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
    int a = 0,b = 0,c = 0;
    int num = 0;
    while(cin>>a>>b>>c)
    {
        bool flag = false;
        num++;
        for(int i = 10;i<=100;i++)
        {
            if(i%3 == 2 && i%5 == b && i%7 == c)
            {
                flag = true;
                cout<<"Case "<<num<<": "<<i<<endl;
                break;
            }
        }
        if(flag == false)
        {
            cout<<"Case "<<num<<": No answer"<<endl;
        }
    }
}

習題2-3 倒三角形

#include <iostream>
#include<cstdio>
using namespace std;

int arr[20];

void printstar(int n)
{
    for(int i = 1;i<=arr[n-1];i++)
    {
        cout<<'*';
    }
    cout<<endl;
}

void printspace(int n)
{
    for(int i = 1;i<=n;i++)
    {
        cout<<' ';
    }
}

int main()
{
    for(int i = 0;i<20;i++)
    {
        arr[i] = 2*i+1;
    }
    int n;
    while(cin>>n)
    {
        int num = n;
        while(n>=1)
        {
            printspace(num-n);
            printstar(n);
            n--;
        }
        
    }
}

習題2-4 子序列的和

#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
    int n,m;
    int k = 0;
    while(cin>>n>>m && n!= 0 && m!=0)
    {
        k++;
        double sum = 0.0;
        for(int i = n;i<=m;i++)
        {
            double temp = 1.0/(i*i);
            sum += temp;
        }
        printf("Case %d: %.5f\n",k,sum);
    }
}

習題2-5 分數化小數

#include <iostream>
#include<cstdio>
using namespace std;
int arr[110];
void pintfarr(int c)
{
  //   cout<<endl<<"c= "<<c<<endl<<endl;
    for(int i = 0;i<c;i++)
    {
        cout<<arr[i];
    }
    cout<<endl;
}
int main()
{
    int a , b, c;
    int number = 0;
    while(cin>>a>>b>>c)
    {
        memset(arr,0,sizeof(arr));
        number++;
        cout<<"Case "<<number<<": "<<a/b<<".";
        if(a%b!=0)
        {
            int num = c+1;
            int getnum = a%b;
            while(num)
            {
                getnum*=10;
                while(getnum<b)
                {
                    getnum*=10;
                    num--;
                }
                int temp = getnum/b;
                getnum%=b;
                arr[c-num+1] = temp;
                num--;
            }
        }
       if(arr[c]>=5)
        {
            arr[c-1]++;
        }
       
        pintfarr(c);
    }
}

習題2-6 排列

#include <iostream>
#include<cstdio>
using namespace std;
int arr[10];
int main()
{  
    for(int i = 123;i<=333;i++)
    {
        memset(arr, 0, sizeof(arr));
        int i1 = i ;
        int i2 = 2*i;
        int i3 = 3*i;
        arr[i1%10] = i1%10;
        arr[i1/10%10] = i1/10%10;
        arr[i1/100] = i1/100;
        
        arr[i2%10] = i2%10;
        arr[i2/10%10] = i2/10%10;
        arr[i2/100] = i2/100;
        
        arr[i3%10] = i3%10;
        arr[i3/10%10] = i3/10%10;
        arr[i3/100] = i3/100;
        
        bool flag = true;
        for(int i = 1;i<=9;i++)
        {
            if(arr[i] != i)
            {
                flag = false;
            }
        }
        if(flag)
        {
            cout<<i1<<" "<<i2<<" "<<i3<<endl;
        }
    }
}