CODY Contest 2020 Basics - Rounding 全10题

第一题 Problem 42641. MATLAB Basic: rounding

Do rounding near to zero

Example: -8.8, answer -8

+8.1 answer 8

想0四舍五入,即如果是正值,就向下取整(floor),如果是负值,就向上取整(ceil)

function y = round_zero(x)
    if x<0
        y=ceil(x);
    else
        y=floor(x);
    end
end

第二题  Problem 42642. MATLAB Basic: rounding II

Do rounding nearest integer.

Example: -8.8, answer -9

+8.1 answer 8

+8.50 answer 9

返回最近整数,直接用round

function y = round_x(x)
    y=round(x);
end

第三题 Problem 42643. MATLAB Basic: rounding III

Do rounding towards minus infinity.

Example: -8.8, answer -9

+8.1 answer 8

+8.50 answer 8

向下取整

function y = round_x(x)
  y = floor(x);
end

第四题 Problem 42644. MATLAB Basic: rounding IV

Do rounding towards plus infinity.

Example: -8.8, answer -8

+8.1 answer 9

+8.50 answer 9

向上取整

function y = round_x(x)
  y = ceil(x);
end

第五题 Problem 2559. Check that number is whole number

Check that number is whole number Say x=15, then answer is 1. x=15.2 , then answer is 0. http://en.wikipedia.org/wiki/Whole_number

检查这个数是不是整数,查看除以1的余数。

function y = your_fcn_name(x)
  y = mod(x,1)==0;
end

第六题 Problem 2866. Matlab Basics - Rounding II

Write a script to round a variable x to 3 decimal places:

e.g. x = 2.3456 --> y = 2.346

对x保留小数点后三位数字(而不是有效数字,所以对于1的话应该是1.000),这个在之前本科毕设的时候用到过:

有效数字可以用vpa,直接是三个数字的话可以用sprintf('%.3f',x)返回一个字符串格式,然后转为数值即可。fprintf()会在命令行直接显示,而不会返回任何结果。

function y = round_3_d_p(x)
  y = str2num(sprintf('%.3f',x));
end

第七题 Problem 2867. Matlab Basics - Rounding III

Write a script to round a large number to the nearest 10,000

e.g. x = 12,358,466,243 --> y = 12,358,470,000

相当于四舍五入到10000的倍数,可以除以10000然后四舍五入再乘以10000即可。

function y = round_ten_thou(x)
  y = round(x/10000)*10000;
end

第八题 Problem 2120. Rounding off numbers to n decimals

Inspired by a mistake in one of the problems I created, I created this problem where you have to round off a floating point number to n decimals. There are 2 inputs to the function: x: floating point number and n: number of decimals which need to match with the given solutions.

将浮点数四舍五入为n个小数,使用round(x,n)即可,但这个问题存在缺点是如果n小于4,他的末尾会补充0,使得小数点后为4个数,比如:

但是因为不是返回字符串,所以加几个0值是相同的,不影响测试。

function y = myround(x,n)
  y = round(x,n);
end

第九题 Problem 43278. Make roundn function

Make roundn function using round.

x=0.55555

y=function(x,1)

y=1

y=function(x,2)

y=0.6

y=function(x,3)

y=0.56

让写一个函数,实现round(x,n) 他给的文案里的function应该改为它指定的函数名myroundn,第一种方法就是不讲武德的直接调用round(x,n),第二种就自己写咯。但是没有给太多测试样例,但是他的测试样例输入n返回的数的小数点后只有n-1位,应该调用的话是round(x,n-1)。

不用字符串的话可以将x乘以10^(n-1)次方,然后四舍五入(或者判断小数部分是否大于0.5 大于则去掉小数后加1 否则去掉小数),再除以10^(n-1)次方。字符串操作的话找到小数点后计数,找到第n-1位,判断是否大于‘5’,如果大于则上一位+1(如果上一位为小数点的话对上一位的上一位操作),并将这个位置以及之后所有的都变为'0' 。最后转为数字然后返回即可。

function y = myroundn(x,n)
    x=num2str(x);
    j=0;%记录小数点后位数
    for i=1:length(x)
        if x(i)=='.'%
            j=j+1;%遍历到小数点后开始计数j
        end
        if j%j大于0说明已经到小数部分
            if j==n+1
                if(x(i)>='5')%入
                    if x(i-1)=='.'
                        x(i-2)=x(i-2)+1;
                    else
                        x(i-1)=x(i-1)+1;
                    end
                end
                x(i:end)='0';
                y=str2num(x);
                return
            end
            j=j+1;
        end
    end
end

 不用字符串:

function y = myroundn(x,n)
    x=x*10^(n-1);
    if mod(x,1)>=0.5
        x=x-mod(x,1)+1;
    else
        x=x-mod(x,1);
    end
    y=x/10^(n-1);
end

第十题 Problem 713. Find the maximum number of decimal places in a set of numbers

Given a vector or matrix of values, calculate the maximum number of decimal places within the input. Trailing zeros do not count as significant. No significant digits will result in an answer of 0.

For example:

x = [0.01 0.888 1.0 40.151 39.1244];

y = 4;

The maximum number of significant decimal places from the input is 4 (39.1244).

终于来了一个不是easy的题了

给定一个矩阵或向量,求其中最长的小数位数(最后的0不算)

首先对x(:)展开为向量,求四舍五入后,用x-round()得到仅小数,但是在matlab中由于存储的原因,实际值和理想值差别在10^(-12) 具体是负多少我也不清楚,比如0.5这个值他在matlab中可能是0.49999999999999999999999999这种,这样直接用累乘10然后判断余是否为0这样就不对了,如果不用字符串的话,需要用round(x,n)对x四舍五入到n个小数位,n应该小于精度,大于测试用例的最大值,否则结果出错。

在这里设round(x,10)。字符串就不写了,思想是转为字符串后,倒着找到第一个不为‘0’的位置,然后再找到小数点的位置,位置的相对距离+1就得到了。

function y = find_max_sigdec(x)
    x=round((x(:)-round(x(:))),10);
    y=0;
    for i=1:length(x)
        j=0;
        while mod(x(i)*10^j,1)~=0
            j=j+1;
        end
        y=max(y,j);
    end
end

猜你喜欢

转载自blog.csdn.net/qq_36614557/article/details/110676033