不足:while循环应全部换成for循环
#include<iostream>
#include<malloc.h>
using namespace std;
int main() {
int a = 0; //记录小猪个数
cout << "请输入小猪的个数 :";
cin >> a;
int temp = 0; //存储体重最大小猪的体重
int y = 0; //依次输出最重小猪的小标
int k = 0; //扫描每只小猪的体重
int r = 0; //记录体重最大小猪的个数的小标
int j = 0; //依次输出第几只小猪的下标
int* arr = (int*)malloc((sizeof(int)) * a);
int* arr2 = (int*)malloc(sizeof(int) * a);
cout << "现在有" << a << "只小猪" << endl;
cout << "请依次输入小猪的体重:" << endl;
int i = 0;
while (i<a) {
cout << "请输入第" << i+1 << "只小猪的体重:";
cin >> arr[i];
i++;
}
cout << endl;
while (j<a) {
cout << "第" << j+1 << "只小猪的体重为:"<<arr[j]<< endl;
j++;
}
while (k<a) {
if (temp < arr[k]) {
temp = arr[k];
r = 0;
arr2[r] = k+1;
}
else if (temp == arr[k]) {
r++;
arr2[r] = k+1;
}
k++;
}
cout << "最重的小猪是:" << endl;
while (y <= r) {
if (y == r) {
cout << "第" << arr2[y];
}
else {
cout << "第" << arr2[y] << "、";
}
y++;
}
cout << "只小猪" << endl;
free(arr);
free(arr2);
system("pause");
return 0;
}