1 条题解

  • 0
    @ 2025-9-9 23:54:53

    C :

    #include<stdio.h>
    #include<stdlib.h>
    #define N 10
    
    int main()
    {
    	int n;
    	int count[N] = {0};
    	int count_n = 0;
    	int i, j;
    	int temp;
    	int b; 
    	do
    	{
    		scanf("%d", &n);
    		if(n == 0) break;
    		int *a = (int *)malloc(n*sizeof(int));
    		for(i = 0; i < n; i++)
    		{
    			scanf("%d", &a[i]);
    		}
    		do
    		{
    			for(i = 0; i < n-1; i++)
    			{
    				if(a[i] > a[i+1])
    				{
    					count[count_n]++;
    					temp = a[i];
    					a[i] = a[i+1];
    					a[i+1] = temp;
    				}
    			}
    			for(i = 0; i < n-1; i++)
    			{
    				if(a[i] < a[i+1])
    				{
    					b = 1;
    					continue;
    				}
    				else 
    				{
    					b = 0;
    					break;
    				}
    			}
    		}while(!b);
    		count_n++;
    	}while(n);
    	for(i = 0; i < count_n; i++)
    	{
    		printf("%d\n", count[i]);
    	}
    	return 0;
    } 
    
    • 1

    【设计型】第11章:指针和数组 最少的交换

    信息

    ID
    1984
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者