😂😂😂 int main() {
int a[10], s, m, y, x;
srand(time(NULL));
for(int i=0; i<10; i++) {
a[i] = rand() % 100 + 2;
printf("%d ", a[i]);
}
printf("\n");
m = a[0];
for(int i=1; i<10; i++) {
if(m < a[i]) {
m =a[i];
}
}
y = a[0];
for(int i=1; i<10; i++) {
if(y > a[i]) {
y = a[i];
}
}
for(int i=0; i<10; i++) {
if(a[i] == m) {
a[i] = y;
s = m;
}
}
for(int i=0; i<10; i++) {
if(a[i] == y) {
a[i] = s;
}
}
for(int i=0; i<10; i++) {
printf("%d ", a[i]);
}
return 0;
}
这段代码中最大值和最小值交换并没有实现,这是因为在第一个for循环中,随机生成的数字是从2到101,而不是从1到100。这将导致最大值不一定为100,也不能保证最小值为1。
如果想要所有元素都在1到100范围内,可以将代码修改为:
int a[10], s, m, y, x;
srand(time(NULL));
for(int i=0; i<10; i++) {
a[i] = rand() % 100 + 1; // 将 2 修改为 1
printf("%d ", a[i]);
}
printf("\n");
...
这样,随机生成的数字就在1到100之间了,就可以正确交换最大值和最小值了。
最大值和最小值的更换在最后两个for循环里不能实现,主要是倒数第二个for循环找到了最大值,用最小值替换了;到最后一个for循环找到最小值时,就会有两个最小值,造成可能上个循环修改的最小值,被替换成了最大值,最终所有的值并没有交换。因此最后的两个for循环修改如下所示:
for(int i=0; i<10; i++) {
if(a[i] == m) {
a[i] = y;
s = m;
}
}
for(int i=0; i<10; i++) {
if(a[i] == y) {
a[i] = s;
}
}
修改如下所示
for(int i=0; i<10; i++) {
if(a[i] == m)
{
s = i;
}
if(a[i]==y)
{
x=i;
}
}
a[s]=y;
a[s]=m;
不知道你这个问题是否已经解决, 如果还没有解决的话:#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool isunique_1(const string &str)
{
int a[8];
memset(a,0,sizeof(a));
int length=str.length();
for(int i=0;i<length;i++)
{
int index_of_array=((int)str[i])/32;
int bit=((int)str[i])%32;
if(bit==0)
index_of_array-=1;
else
bit-=1;
if(a[index_of_array]&(1<<bit))
return false;
a[index_of_array]|=(1<<bit);
}
return true;
}
bool isunique_2(const string &str)
{
bool a[256];
memset(a,0,sizeof(a));
int length=str.length();
for(int i=0;i<length;i++)
{
int index=(int)str[i];
if(a[index])
return false;
a[index]=true;
}
return true;
}
int main()
{
string s1="@#$dwfbytj\)(";
string s2="@#ghjkl^*)(@@";
bool unique=isunique_1(s1);
if(unique)
cout<<"方法一:s1不存在重复的字符"<<endl;
else
cout<<"方法一:s1存在重复的字符"<<endl;
unique=isunique_2(s1);
if(unique)
cout<<"方法二:s1不存在重复的字符"<<endl;
else
cout<<"方法二:s1存在重复的字符"<<endl;
unique=isunique_1(s2);
if(unique)
cout<<"方法一:s2不存在重复的字符"<<endl;
else
cout<<"方法一:s2存在重复的字符"<<endl;
unique=isunique_2(s2);
if(unique)
cout<<"方法二:s2不存在重复的字符"<<endl;
else
cout<<"方法二:s2存在重复的字符"<<endl;
return 0;
}
这段代码中出现问题的可能性有以下几种情况:
针对这些情况,可以分别进行如下处理:
def swap_min_max(arr):
if len(arr) < 2:
return arr
# 进行元素交换操作的代码
return arr
def swap_min_max(arr):
if len(arr) < 2:
return arr
min_index = 0
max_index = 0
for i in range(1, len(arr)):
if arr[i] < arr[min_index]:
min_index = i
elif arr[i] > arr[max_index]:
max_index = i
if min_index != max_index:
arr[min_index], arr[max_index] = arr[max_index], arr[min_index]
return arr
完整代码如下:
def swap_min_max(arr):
if len(arr) < 2:
return arr
if not isinstance(arr[0], (int, float)):
raise TypeError('All elements of input array should be numeric.')
min_index = 0
max_index = 0
for i in range(1, len(arr)):
if arr[i] < arr[min_index]:
min_index = i
elif arr[i] > arr[max_index]:
max_index = i
if min_index != max_index:
arr[min_index], arr[max_index] = arr[max_index], arr[min_index]
return arr
如果以上处理方式都不能解决问题,可能需要进一步检查数组元素的情况,例如是否存在空值、特殊字符等情况。