64,640
社区成员
发帖
与我相关
我的任务
分享
int main(void)
{
int first, sec;
int test[] = {129, 45, 23, 0, 1, 3, 65, 123}; //确保test至少2个成员
int i;
//初始化
if(test[0] > test[1]) {
first = test[0];
sec = test[1];
} else {
first = test[1];
sec = test[0];
}
for(i = 2; i < 8; ++i) { //迭代
if(first < test[i]) {
sec = first;
first = test[i];
}
}
printf("Max_f: %d\nMax_s: %d\n", first, sec);
return 0;
}
[/quote]
嗯,是的。你说的对!尽快解决
int main(void)
{
int first, sec;
int test[] = {129, 45, 23, 0, 1, 3, 65, 123}; //确保test至少2个成员
int i;
//初始化
if(test[0] > test[1]) {
first = test[0];
sec = test[1];
} else {
first = test[1];
sec = test[0];
}
for(i = 2; i < 8; ++i) { //迭代
if(first < test[i]) {
sec = first;
first = test[i];
}
}
printf("Max_f: %d\nMax_s: %d\n", first, sec);
return 0;
}
[/quote]
补丁:for循环改成下面的语句即可
for(i = 2; i < 8; ++i) { //迭代
if(first < test[i]) {
sec = first;
first = test[i];
}
else if(sec < test[i]){
sec = test[i];
}
}
int main(void)
{
int first, sec;
int test[] = {12, 45, 23, 0, 1, 3, 65, 123}; //确保test至少2个成员
int i;
//初始化
if(test[0] > test[1]) {
first = test[0];
sec = test[1];
} else {
first = test[1];
sec = test[0];
}
for(i = 2; i < 8; ++i) { //迭代
if(first < test[i]) {
sec = first;
first = test[i];
}
}
printf("Max_f: %d\nMax_s: %d\n", first, sec);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a[3];
int i,c,d;
c=d=0; //c, d赋初值
for(i=0;i<=3;i++)
{
cin>>a[i];
//c=d=a[0]; //不能在循环中重复赋初值
if(d<=a[i])
{
c = d;
d=a[i];
}
else if( c<=a[i] )
{
c = a[i];
}
}
cout<<c<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a[3];
int i,c,d;
c=d=0; //c, d赋初值
for(i=0;i<=3;i++)
{
cin>>a[i];
//c=d=a[0]; //不能在循环中重复赋初值
if(d<=a[i])
{
c=d;
d=a[i];
}
}
cout<<c<<endl;
return 0;
}
/*************************************************************************
> File Name: second.cpp
> Author: Jukay
> Mail: hellojukay@gmail.com
> Created Time: 2014年11月06日 星期四 18时05分42秒
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
int second(vector<int> &v)
{
int tmp1,tmp2;
int max1 = v[0], max2 = v[v.size() -1];
for(int i = 0, j = v.size() - 1; i < v.size(); ++i, --j)
{
if(v[i] > max1)
{
tmp1 = max1;
max1 = v[i];
}
if(v[j] > max2)
{
tmp2 = max2;
max2 = v[j];
}
}
if(tmp1 >= tmp2)
return tmp2;
else
return tmp1;
}
int main()
{
vector<int> v = {45,5,6,5,23,2,5,12,4,0};
int sec;
sec = second(v);
cout << sec << endl;
return 0;
}
int main(void)
{
int first, sec;
int test[] = {129, 45, 23, 0, 1, 3, 65, 123}; //确保test至少2个成员
int i;
//初始化
if(test[0] > test[1]) {
first = test[0];
sec = test[1];
} else {
first = test[1];
sec = test[0];
}
for(i = 2; i < 8; ++i) { //迭代
if(first < test[i]) {
sec = first;
first = test[i];
}
}
printf("Max_f: %d\nMax_s: %d\n", first, sec);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a[3];
int i,c,d;
cin>>a[i];
c=d=a[0];
for(i=1;i<=3;i++)
{
cin>>a[i];
if(d<=a[i])
{
c=d;
d=a[i];
}
}
cout<<c<<endl;
return 0;
}