日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

谷歌2007年上交大考试最后一题解答

發布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谷歌2007年上交大考试最后一题解答 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

N個整數,求其中任意N-1個數的乘積中的最大的一個。
例如?3,2,1,則最大的是3*2=6
提示:整數包括0和負數
要求給出個比較有效率的算法 ,不能用除法,只能用乘法。

從網上找一了一個解答比較好:http://bbs.csdn.net/topics/90460367

該問題等價于:從?N?個整數中剔除一個,使余下的數乘積最大。

令這N個數依次為?a1,a2,...,an,P=a1*a2*...*an,
(1)?如果?P<0,則剔除其中最大的負整數即可;
(2)?如果?P=0,
????2.1?若這?N?個數中有且僅有一個為“0”。若其它數之積為正,則剔除“0”;否則剔除任意一個非零數;
????2.2?若這?N?個數中至少有兩個為“0”,則隨便剔除一個均可;

(3)?如果?P>0,則剔除其中最小的正整數即可。

代碼:

#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
using namespace std;
int max_subarray(int a[],int n)
{

int product=std::accumulate(a,a+n,1,std::multiplies<int>());
if (product<0)
{
int imax;
//尋找最大的負整數序號,剔除掉
for (int i=0;i<n;i++)
{
if (a[i]<0)
{
imax=i;
break;
}
}
for (int k=imax+1;k<n;k++)
{
if (a[k]<0&&a[k]>a[imax])
{
imax=k;
}
}
product=1;
cout<<"乘積最大的一組數為"<<" ";
for (int k=0;k<n;k++)
{
if (k!=imax)
{
product*=a[k];
cout<<a[k]<<" ";
}
}
cout<<"最大乘積為"<<product<<endl;
}
else if (product==0)
{
int zero_1=-1;//保存第一個0的序號
int num=0;//保存0的個數
for (int i=0;i<n;i++)
{
if (a[i]==0)
{
num++;
if(zero_1==-1)
zero_1=i;
}
}
if (num>=2)
{
cout<<"最大乘積的數組為"<<" ";//剔除掉第一個0
for (int i=0;i<n;++i)
{
if (i!=zero_1)
{
cout<<a[i]<<" ";
}

}
cout<<"最大乘積為"<<0<<endl;
}
else
{
product=1;
for (int i=0;i<n&&i!=zero_1;++i)
{
product*=a[i];
}
if (product<0)
{
cout<<"最大乘積的數組為"<<" ";
int index;
for (int i=0;i<n&&a[i]!=0;++i)
{
index=i;
break;
}//找到第一個不為0的數的序號,剔除掉
for (int i=index+1;i<n;++i)
{
cout<<a[i]<<" ";
}
cout<<"最大乘積為"<<0<<endl;
}
else//剔除掉0
{
cout<<"最大乘積的數組為"<<" ";
product=1;
for (int i=0;i<n;++i)
{
if (i!=zero_1)
{
cout<<a[i]<<" ";
product*=a[i];
}

}
cout<<"最大乘積為"<<product<<endl;

}
}
}
else
{
int imin;
//尋找最小的正整數序號,剔除掉
for (int i=0;i<n;i++)
{
if (a[i]>0)
{
imin=i;
break;
}
}
for (int k=imin+1;k<n;k++)
{
if (a[k]>0&&a[k]<a[imin])
{
imin=k;
}
}
product=1;
cout<<"乘積最大的一組數為"<<" ";
for (int k=0;k<n;k++)
{
if (k!=imin)
{
product*=a[k];
cout<<a[k]<<" ";
}

}
cout<<"最大乘積為"<<product<<endl;
}
return product;

}


// 1 2 3 4 5
void test1()
{
int a[5]={1,2,3,4,5};
cout<<"a"<<" ";
copy(a,a+5,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,5);

}
//0 1 2 3 4
void test2()
{
int a[5]={1,2,3,4,0};
cout<<"a"<<" ";
copy(a,a+5,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,5);
}
//0 0 1 2 3
void test3()
{
int a[5]={1,2,3,0,0};
cout<<"a"<<" ";
copy(a,a+5,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,5);

}
//0 0 0 -1 5 -3 -4
void test4()
{
int a[7]={0,0,0,-1,5,-3,-4};
cout<<"a"<<" ";
copy(a,a+7,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,7);

}
//-1 -2 -3 -4 -5
void test5()
{
int a[5]={-1,-2,-3,-4,-5};
cout<<"a"<<" ";
copy(a,a+5,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,5);

}
//1 -2 -3 -4 -5
void test6()
{
int a[5]={1,-2,-3,-4,-5};
cout<<"a"<<" ";
copy(a,a+5,ostream_iterator<int>(cout," "));
cout<<endl;
max_subarray(a,5);

}
void main()
{
test1();
test2();
test3();
test3();
test4();
test5();
test6();

}

總結

以上是生活随笔為你收集整理的谷歌2007年上交大考试最后一题解答的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。