为什额这串代码中最大值没有交换成最小值

       😂😂😂   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;

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/835171
  • 你也可以参考下这篇文章:用指针找出数组中最大值和最小值
  • 除此之外, 这篇博客: 如何判断一个字符串中没有重复字符中的 两种方法的实例 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    #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;
    }
    
    

  • 您还可以看一下 Chris老师的大厂算法面试真题解析课程中的 复制带随机指针的链表小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    这段代码中出现问题的可能性有以下几种情况:

    1. 数组中只有一个元素,或者没有元素,导致函数返回原数组,没有进行交换操作。
    2. 数组中的最小值和最大值相等,导致交换操作并没有产生任何改变。
    3. 输入的数组并不是数字类型的数组,导致无法进行比较和交换操作。

    针对这些情况,可以分别进行如下处理:

    1. 添加判断条件,在数组元素小于2时直接返回原数组,如下所示:
    def swap_min_max(arr):
        if len(arr) < 2:
            return arr
        # 进行元素交换操作的代码
        return arr
    
    1. 在最大值和最小值相等时,无需进行交换操作,如下所示:
    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
    
    1. 在输入的数组不是数字类型的数组时,可以添加类型检查。

    完整代码如下:

    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
    

    如果以上处理方式都不能解决问题,可能需要进一步检查数组元素的情况,例如是否存在空值、特殊字符等情况。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^