
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n,j=1;
int max,min,temp;
scanf("%d", &n);
int *a = (int *)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
min=max =0;
while (j < n) {
if (a[j] > a[max])
max = j;
if (a[j] < a[min])
min = j;
j++;
}
temp = a[0];
a[0] = a[min];
a[min] = temp;
temp = a[n - 1];
a[n - 1] = a[max];
a[max] = temp;
for (int i = 0; i < n-1; i++) //然后用改良后用這個輸出,但是結果確實wrong answer,我真是服了
printf("%d ", a[i]);
printf("%d", a[n-1]);
printf("\n"); //這個換行區分下結果,便于區分,上傳代碼的時候是沒有的
for (int i = 0; i < n ; i++) //用這個輸出就是PE錯誤,我想大致原因是最后一個元素后面有空格
printf("%d ", a[i]);
return 0;
}
uj5u.com熱心網友回復:
代碼沒問題,注意在最后加一句free(a);//釋放空間有問題的地方可能是你的編譯器或者服務器上的編譯器是C語言的,像 for (int i = 0; i < n-1; i++)這樣的在for回圈里面定義區域變數的,一般的C語言編譯器不支持,是C++的特性。
鑒于上面已經定義了j,后面的回圈都可以用j作為回圈變數。
uj5u.com熱心網友回復:
最后輸出修改試試:for(int i = 0;i < n; i++)
printf("%d%c",a[i],(i<n-1)?'z':'\n');
uj5u.com熱心網友回復:
上面多打了z,修改:for(int i = 0;i < n; i++)
printf("%d%c",a[i],(i<n-1)?' ':'\n');
uj5u.com熱心網友回復:
不行啊哥!還是Wrong Answer#include <stdio.h>
#include<stdlib.h>
int main()
{
int n, j = 1;
int max, min, temp;
scanf("%d", &n);
int *a = (int *)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
min = max = 0;
while (j < n) {
if (a[j] > a[max])
max = j;
if (a[j] < a[min])
min = j;
j++;
}
temp = a[0];
a[0] = a[min];
a[min] = temp;
temp = a[n - 1];
a[n - 1] = a[max];
a[max] = temp;
for (j = 0; j< n - 1; j++)
printf("%d ", a[j]);
printf("%d\n", a[n - 1]); //這里/n我加沒加也都是通過不了
free(a);
return 0;
}
uj5u.com熱心網友回復:
還是不行,通過不了,我快吐了uj5u.com熱心網友回復:
這樣改下,再試試:#include <stdio.h>
#include<stdlib.h>
int main()
{
int n,j;
int max,min,temp;
scanf("%d", &n);
int *a = (int *)malloc(sizeof(int)*n);
for ( j = 0; j < n; j++) {
scanf("%d", &a[j]);
}
min=max =0;
j=1;
while (j < n) {
if (a[j] > a[max])
max = j;
if (a[j] < a[min])
min = j;
j++;
}
temp = a[0];
a[0] = a[min];
a[min] = temp;
temp = a[n - 1];
a[n - 1] = a[max];
a[max] = temp;
for ( j = 0; j < n ; j++){
if(j==0)
printf("%d", a[j]);
else printf(" %d", a[j]);
}
free(a);
return 0;
}
uj5u.com熱心網友回復:
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n, j = 1;
int max, min, temp;
int *a = NULL;
scanf("%d", &n);
//int *a = (int *)malloc(sizeof(int)*n);
a = (int *)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
min = max = 0;
while (j < n) {
if (a[j] > a[max])
max = j;
if (a[j] < a[min])
min = j;
j++;
}
temp = a[0];
a[0] = a[min];
a[min] = temp;
temp = a[n - 1];
a[n - 1] = a[max];
a[max] = temp;
for (j = 0; j< n - 1; j++)
printf("%d ", a[j]);
printf("%d\n", a[n - 1]); //這里/n我加沒加也都是通過不了
free(a);
return 0;
}
再試一下,懷疑你提交的服務器的編譯問題,編譯不通過;
uj5u.com熱心網友回復:
不行還是通不過uj5u.com熱心網友回復:
不會吧,要是最后輸出不做最后一個元素特殊處理,就都是元素+空格的話題是PE錯誤,就應該大致都是對的。之前有個問題我也是PE錯誤,就是最后空格多出來一個,不過這題就邪門了uj5u.com熱心網友回復:
這道題可能是不是要處理一種特殊情況, 就是當最大值的idx是0的時候,那時交換要判斷一下 ,分別處理。。。。
temp = a[0];
a[0] = a[min];
a[min] = temp;
// ----------------------------------- 加上這段
if(max==0) {
max = min;
}
//----------------------------------
temp = a[n - 1];
a[n - 1] = a[max];
a[max] = temp;
。。。。。。。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250896.html
標籤:C語言
上一篇:vscode 變數變灰
