二分之三


私信TA

用户名:uq_82105682649

访问量:419

签 名:

等  级
排  名 22782
经  验 604
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

公差不是我们简单的认为最小的差值。而是差值的最大公约数。


举个例子

6

2 4 7 8 12 15


等于14


参考代码:

#include

using namespace std;

int a[100005];

int gcd(int a,int b)

{

if(b==0)

return a;

else

return gcd(b,a%b);

}

int main()

{

int n;

cin>>n;

int i;

for(i=0;i<n;i++)

{

cin>>a[i];

}

sort(a,a+n);

int temp=a[1]-a[0];


for(i=2;i<n;i++)

{

int temp1=a[i]-a[i-1];

temp=gcd(temp,temp1);

}

if(temp==0)

{

cout<<n<<endl;

return 0; 

}

int sum=0;

sum=(a[n-1]-a[0])/temp;

cout<<sum+1<<endl;

 } 


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区