TC问题求救!万分感谢!

来源:百度知道 编辑:UC知道 时间:2024/06/28 06:13:55
各位仁兄帮帮忙!小弟万分感谢!
输入10个整数,将其中最小的数与第1个数对换,把最大的数与最后一个数对换,编程实现。(注:不要用指针)

#include <stdio.h>
int main()
{
int a[10];
int minIndex=0, maxIndex= 9;
int t;
for (int i=0; i < 10; i++ )
{
scanf( "%d", &a[i]);
}
for ( int i=0; i < 10; i++ )
{
if ( a[minIndex] > a[i] ) minIndex = i;
if ( a[maxIndex] < a[i] ) maxIndex= i;
}
if ( minIndex != 0 )
{
t=a[0];a[0]=a[minIndex];a[minIndex]=t;

}
if ( maxIndex !=9 )
{
t=a[9];a[9]=a[maxIndex];a[maxIndex]=t;
}

return 0;
}

用vector,string
vector<int> v;
string s;
const char* p = s.c_str();
for (int i = 0;i < s.length();i ++)v.push_back(p[ i ] - '0');
int _max = max_element(v.begin(),v.end()) - v.begin();
int _min = min_element(v.begin(),v.end()) - v.begin();
swap(v[ 0 ],v[ _min ]);
swap(v[v.size() - 1],v[ _max ]);

#include<stdio.h>
int i