首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求高手点拨:“计算欧盟成员国权利系数”作业代码前后的输出异常原因

2013-01-06 
求高手点拨:“计算欧盟成员国权利系数”作业代码前后的输出错误原因。高手,您好:我是一名学习软件的学生,由于

求高手点拨:“计算欧盟成员国权利系数”作业代码前后的输出错误原因。
高手,您好:
    我是一名学习软件的学生,由于刚刚接触VS2010工作环境并且原来一直在学Java,最近刚刚开始学习C语言。
      近日老师布置了一个作业:要求我们计算欧盟成员国排出了英国后的权力系数计算问题,题目背景需求如下:
      欧盟投票,14个国家。(去掉英国)
      法,德,意各持10票;
      西班牙持8票;
      比,西,荷,葡各持5票;
      奥地利,瑞典各持4票;
      爱,丹,芬各持3票;
      卢森堡持2票;
      欧盟一个决议需要各成员国投票后,以总票数超过62票为“决议通过并执行”的,边界条件。
      当一个国家位于“其所持票能够影响到决议是否执行”时,其是为“关键角色”。
      一个国家i的权力系数index的计算方法为:该国家i为关键角色时各成员国进行排列的可能个数/个成员国排
  列的总可能个数;
      初始时,我采用第一个算法求得的正确的结果的输出是:
      France power coefficient is 12.858253%
     German power coefficient is 12.858253%
     Italy power coefficient is 12.858253%
     Spanish power coefficient is 11.110001%
     Belgium power coefficient is 6.063381%
     Greece power coefficient is 6.063381%
     Holland power coefficient is 6.063381%
     Portuguese power coefficient is 6.063381%
     Austria power coefficient is 5.278610%
     Sweden power coefficient is 5.278610%
     Ireland power coefficient is 4.307359%
     Denmark power coefficient is 4.307359%
     Finland power coefficient is 4.307359%
     Luxembourg power coefficient is 2.582418%
     The result is 1.000000%
     请按任意键继续...

     高手,请允许我将上面的代码的输出结果起名为:“【标准答案输出】”。

     经过反复的思考和研究,我写出了经过改良的代码如下:

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "timer.h"
using namespace std;

#define N 14

void perm(int);/*以下文中的perm()函数,来进行对诸成员国的“递归由数组a[N]中的下文的第
                   “arrayhere”个元素进行排列的开始点的选取*/
void print();
void swap(int, int);

int a[N]={10,10,10,8,5,5,5,5,4,4,3,3,3,2};/*以数组a[N]作为各成员国所持票数的承载容器*/
long long lcount[N] = {0};/*以数组lcount[i]来进行作为"计算各个成员国权力系数"的中转工具*/


int main(){
Timer mt;/*老师给出的“计算程序效率”的头文件"Timer.h"文件中的一个Timer类,创建一个用于
                       在我的代码中行使"记录并计算整个程序的执行时间"的Timer类的对象mt*/
mt.start();/*Timer类中有用于记录程序时间起点的start()函数,可以为mt对象直接调用*/
int i,n,k;/*我认为的错误就在这里,i一直负责了整个程序的所有数组的循环变量控制,而n与k没有
                        参与运算,这里,我认为是错误所在。求高手指导*/
int arrayhere;/*以上文中perm()函数的“传值参数”的身份,参与整个程序的计算*/
perm(0);
print();
mt.stop();/*老师给出的Timer类中含有计算程序的结束点的stop()函数*/
std::cout<<"duration in seconds:" << mt.seconds ()<<std::endl;


system("pause");
return 0;
}

void perm(int arrayhere){
int i;
if(arrayhere == N-1){
int sum = 0;
int i = 0;
for(i = 0;i < N; ++i)
{
sum += a[i];
if (sum >= 62)
break;
}
++lcount[i];
return;
}else{
for(i = arrayhere;i < N; i++){
swap(i, arrayhere);
perm(arrayhere + 1);
swap(i, arrayhere);
}
}
}
void print(){
double fact = 1;
for(int i = 2;i <= N; ++i)
{
fact *= i;
}
double index[N];
for(int i = 0; i < N; ++i)
index[i] = lcount[i]/fact;

printf("France     power coefficient is %f%%\n",100*index[ 0]);
printf("German     power coefficient is %f%%\n",100*index[ 1]);
printf("Italy      power coefficient is %f%%\n",100*index[ 2]);
printf("Spanish    power coefficient is %f%%\n",100*index[ 3]);
printf("Belgium    power coefficient is %f%%\n",100*index[ 4]);
printf("Greece     power coefficient is %f%%\n",100*index[ 5]);
printf("Holland    power coefficient is %f%%\n",100*index[ 6]);
printf("Portuguese power coefficient is %f%%\n",100*index[ 7]);
printf("Austria    power coefficient is %f%%\n",100*index[ 8]);
printf("Sweden     power coefficient is %f%%\n",100*index[ 9]);
printf("Ireland    power coefficient is %f%%\n",100*index[10]);
printf("Denmark    power coefficient is %f%%\n",100*index[11]);
printf("Finland    power coefficient is %f%%\n",100*index[12]);
printf("Luxembourg power coefficient is %f%%\n",100*index[13]);

double one = 0.0;
for(int i = 0; i < N; ++i)
one += index[i];
printf("The result is %f%%\n",one);/*以“one是否等于1”作为整个程序计算结果的“验证变量”*/

void swap(int i, int arrayhere){
int temp;
temp = a[arrayhere];
a[arrayhere] = a[i];
a[i] = temp;

     
     高手,上述是我的代码,但是,最终的输出结果是:
      France power coefficient is 0.000000%
     German power coefficient is 0.000000%
     Italy power coefficient is 0.000000%
     Spanish power coefficient is 0.000000%
     Belgium power coefficient is 0.000000%
     Greece power coefficient is 0.000000%
     Holland power coefficient is 0.000000%
     Portuguese power coefficient is 0.000000%
     Austria power coefficient is 0.099900%
     Sweden power coefficient is 9.990010%
     Ireland power coefficient is 30.294705%


     Denmark power coefficient is 53.021978%
     Finland power coefficient is 6.593407%
     Luxembourg power coefficient is 0.000000%
     The result is 1.000000%
     duration in seconds:1297.51
     请按任意键继续...
     
     高手,我的代码的输出结果,与上文的“【标准答案输出】”相差甚远,本人诚挚地期望高手能够为本人解惑:
      我的错误在哪?
      并且,如果高手能够为我将调出正确结果的代码给出的话,我,在这里起誓:
      我将把欠下的高手的“帮助我摆平一件事”的人情,认认真真地记忆在心。好好学习,壮大自己的能量。
      让我有一日,能够将帮助我的高手的人情,使用我的双手,为高手,还上一个“高质量”的“摆平一件事”的回报!!
      我的信箱账户是:CJCOMEON2013@163.com
     恳切地期待帮助我的高手,能够将您的联系方式,发送到我的信箱..
     我将同时在计算机面前,思考我的算法的纰漏,再次谢谢高手的帮助!!
      我将我全部的积分,35分,奉上,作为我的另外的一点点的谢意的表示。
[解决办法]
还有, lz的机器很强大 。。

这个程序我的机器保守估计没有一天跑不下来。。。
求高手点拨:“计算欧盟成员国权利系数”作业代码前后的输出异常原因

热点排行