算法竞赛入门经典(第二版)【第二章】习题答案

这里只讲2道烧脑的题,习题2-5和习题2-6,其他请自行百度。

 

习题2-5 分数化小数(decimal)

参考链接:1.刘汝佳-算法竞赛入门-分数化小数

                 2.github《算法竞赛入门经典》第二章练习题

                 3.我的做法:

                    我的做法参考了第一个csdner,第二个人思路基本相同,但是最后一步用了round(x)函数,即四舍五入函数,方便最后一步操作。

                 建议:先看参考链接1,讲的很详细,如果最后一步看不懂,m = m*10/b ,round(x) 四舍五入得到最后一位。

(习题2-5  我的代码如下)

2-5方法一

#include <stdio.h>
#include <math.h>
int main()
{
    int a,b,c;
    int kase = 0;
    while(scanf("%d%d%d",&a,&b,&c) && !(a==0 && b==0 && c==0))
    {
        printf("Case %d: %d.",++kase,a/b);
        for(int i=1;i<c;i++)
        {
            a = a * 10;
            printf("%d",a/b);
            a = a % b;
        }
        a = a*10;
        //如果保留4位小数,则看第5位小数的值,四舍五入
        printf("%d\n",a*10/b%10 >= 5? a/b+1 : a/b);
    }
    return 0;


    /*
        test
        printf("%f",round(40/6.0));
    */
}

2-5方法二  

#include <stdio.h>
#include <math.h>
int main()
{
    int a,b,c;
    int kase = 0;
    while(scanf("%d%d%d",&a,&b,&c) && !(a==0 && b==0 && c==0))
    {
        printf("Case %d: %d.",++kase,a/b);
        for(int i=1;i<c;i++)
        {
            a = a * 10;
            printf("%d",a/b);
            a = a % b;
        }
        a = a*10;
        printf("%d\n",(int)round((double)a/b)); 
        
        //double必不可少,否则 a/b直接就是integer,round也没有用了。
    }





    /*
        test
        printf("%f",round(40/6.0));
    */
}

 

 

习题2-6 排列(permutation)

题意:将1,2,3,---9每个数字恰好使用一次,表示成abc:def:ghi的形式。

思路:

        1.首先考虑范围:第一个最小的3个数字不相等的数为123,最大的数为329(1000/3=333,故从333往下排,第一个数字不同的数为329)。

        2.分离3个数字的值,存到a[9]数组中,即分离每个数的个位,十位,百位,如123,   a[0] = i /100   ,    a[1]  =i / 10%10    ,       a[2] = i % 10.       存为a[0] = 1,a[1] = 2, a[2] = 3.一直到a[9]. 

        3.如何分辨得出来的这9个数是不同的,在这里我介绍两种办法。

           ①。(在第二步存数组时,对分离出的每个位设为1,(若有重复的值必定<9,) 如果最后将这9个数加起来=9则,说明9个数不同,可以打印。)       

           ②。(先对这9个数排序,然后依次比较看是否后一个数比前一个数大一)

        4.如果符合情况就打印出来,下面是我的代码,并附上2种方法和结果。

方法一 :

/*
    author:wuki

    date: 2019/4/25

    solution 1:compare the value of adjacent number to ensure their difference is 1.
*/
#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,k;
    int cnt;
    int a[9];
    for(i=123;i<=329;i++)
    {
        j = i * 2;
        k = i * 3;
        a[0] = i/100;a[1] = i/10%10;a[2] = i%10;
        a[3] = j/100;a[4] = j/10%10;a[5] = j%10;
        a[6] = k/100;a[7] = k/10%10;a[8] = k%10;

        //bubble sort
        for(int x = 0;x<9-1;x++)
        {
            for(int y = 0;y<9-1-x;y++)
            {
                if(a[y] > a[y+1])
                {
                    int temp = a[y];
                    a[y] = a[y+1];
                    a[y+1] = temp;
                }
            }
        }
        cnt = 1;
        for(int x = 0;x<8;x++)
            if(a[x+1]-a[x] == 1 && a[x]!=0)
                cnt++;
        if(cnt == 9)
            printf("%d %d %d\n",i,j,k);

    }
	return 0;
}

方法二:

#include<stdio.h>
#include<math.h>
/*
    author:wuki

    date: 2019/4/25

    solution 2:With the number is emerging,the array [number] assigns integer 1.
               then add these values .If their total is 9,it is right,print it.
*/
int main()
{
    int i,j,k;
    int cnt;
    for(i=123;i<=329;i++)
    {
        int a[10] = {0};
        //我这里第一次写成了a[9]从0到9,发现答案只有一个,才发现,
        //因为下边是a【x】,x存的是每个数的一位,肯定没有0,只有从1~9,
        //这里与下边的计数也有关系。

        j = i * 2;
        k = i * 3;
        //设置了标记,因此如果有重复,他们加起来的值不会超过9
        a[i/100] = a[i/10%10] = a[i%10] = 1;
        a[j/100] = a[j/10%10] = a[j%10] = 1;
        a[k/100] = a[k/10%10] = a[k%10] = 1;


        cnt = 0;
        for(int x = 1;x <= 9; x++)
            if(a[x]==1)cnt++;
        if(cnt == 9)
            printf("%d %d %d\n",i,j,k);
    }
	return 0;
}

以下是测试结果。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值