C#练习题答案: 连续整数的和【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

连续整数的和【难度:1级】:

答案1:

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        // TODO: return consecutive integer at requested position
        return (2*y-x*x -x)/(2*x) + n+1; 
    }
}

答案2:

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        var xd = (double) x;
        var yd = (double) y;
        return (int) (yd / xd - (xd - 1) / 2 + n);
    }
}

答案3:

public class SumOfConsecutiveIntegers
{
    public static int Position(int n, int S, int p)
    {
        return (2*S/n - (n - 1))/2 + p;
    }
}

答案4:

using System;
public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        return y>=0 ? y/x-(x-1)/2+n : y/x+(x-1)/2-x+n+1;
    }
}

答案5:

using System;

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        double avg = (double)y / (double)x;
        int half = x / 2 - 1 + (x % 2);
        int startNum = (int)avg;
        
        int decRemainder = (int)Math.Ceiling(((int)avg - avg));

        return startNum + (n - half - decRemainder);

        
    }
}

答案6:

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
      //int start;
      //numbers: start, start+1, ..., start+x-1; result: start+n
      //y = start * x + x * (x-1) / 2;
      //start = (y-x*(x-1)/2)/x
      
      return (y - ((x * x - x)/2))/x + n;
        // TODO: return consecutive integer at requested position
    }
}

答案7:

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        return (int)System.Math.Ceiling((double)y / x) - (x/2) + n;
    }
}

答案8:

public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
        int a = ((x - 1) * x) / 2;
        int b = (y - a) / x;
        
        return b + n;
    }
}

答案9:

using System;
public class SumOfConsecutiveIntegers
{
    public static int Position(int x, int y, int n)
    {
      if (x%2==0 && y > 0) { return ((y/x)-(x/2-1))+n; }
      else {return ((y/x)-x/2)+n;}
    }
}

答案10:

using System;

public class SumOfConsecutiveIntegers {
        public static int Position( int x, int y, int n ) {
            double average = (double)y / x;
            int first = (int) Math.Ceiling((double) average - (double)x/2);
            return first + n;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值