赞
踩
- #include<iostream>
- #include<cmath>
- #define N 50
- using namespace std;
- int main() //比例+惯例法
- {
- int n[N], p[N], nsum, sum1, sum, m, i, j, k, r;
- float c[N];
- sum = 0;sum1 = 0;j = 0;
- cout << "please enter m n" << endl;
- cin >> m >> nsum;
- cout << "please enter pi " << endl;
- for (i = 0;i < m;i++)cin >> p[i];
- for (i = 0;i < m;i++)sum = sum + p[i];
- for (i = 0;i < m;i++) n[i] = floor(((float)p[i] / sum)*nsum);
- for (i = 0;i < m;i++)sum1 = sum1 + n[i];
- k = nsum - sum1;
- for (i = 0;i < m;i++)c[i] = ((float)p[i] / sum)*nsum - n[i];
- for (r = 0;r < k;r++)
- for (i = 0;i < m;i++)
- if (c[i] > c[j])j = i;
- n[j]++;
- c[j] = 0;
- for (i = 0;i < m;i++)cout << n[i]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。