C++中想输入两个数据,第二个始终参与无法运算
程序代码:
#include <iostream> #include <vector> #include <algorithm> using namespace std; using std::cin; using std::endl; using std::cout; using std::vector; using std::sort; using std::iterator; int main(void) { cout << "请输入一个数集:" << endl; float x; vector<float> intsets; while (cin>> x) { intsets.push_back(x); } // 获取整数集中整数的个数 typedef vector<float>::size_type vec_sz; vec_sz size = intsets.size(); // 判断整数集是否为空 if (size <= 3) { cout << "你输入的数集小于3,数集需" "大于3,请重新输入!!" << endl; return 1; } // 整数集从小到大排序 sort(intsets.begin(), intsets.end()); // 输出排序后的结果 cout << "排序后的数集是:" << endl; vector<float>::iterator iter; for (iter = intsets.begin(); iter != intsets.end(); iter++) { cout << *iter << ' '; } cout << endl; // 中位数和相应的索引 vec_sz mid, mid1, mid3; double median, median1, median3; mid = size / 2; // 求得中值 median = size % 2 == 0 ? (intsets[mid] + intsets[mid-1]) / 2.0: intsets[mid]; //mid1 = size % 2 == 0 ? (mid-1)/2 : mid/2; mid1 = mid / 2; mid3 = size%2 == 0 ? (mid + mid1) : (mid+mid1+1); median1 = mid % 2 == 0 ? (intsets[mid1] + intsets[mid1-1]) / 2.0: intsets[mid1]; median3 = mid % 2 == 0 ? (intsets[mid3] + intsets[mid3-1]) / 2.0: intsets[mid3]; cout << "中分位数为:" << endl; cout << "median1 = " << median1 << endl; cout << "median2 = " << median << endl; cout << "median3 = " << median3 << endl; float a=median3+1.5*(median3-median1); cout << "a = " << a << endl; float b=median3-1.5*(median3-median1); cout << "b = " << b << endl; cout << "请输入一个数n:" << endl; int n; int s=0; for(int i=0;i<n;i++) cout<<"s= "<<intsets[i]; {if(intsets[n-1]>a) s++;} cout<<endl<<"这个数组中大于指定值的元素的个数是"<<s<<endl; int z=0; for(int i=0;i<n;i++) cout<<"z= "<<intsets[i]; {if(intsets[n-1]<b) z++;} cout<<endl<<"这个数组中小于指定值的元素的个数是"<<z<<endl; return 0; }
运行后结果如图:
。我想要先把数组和数组长度n输入,然后再运行计算。现在只是输入数组运算,n完全没有输入。请问怎么改。