| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 762 人关注过本帖
标题:求教:为什么这个简单程序计算结果第二项(-10 -44 -10.000000)是错误的 ...
只看楼主 加入收藏
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
结帖率:97.06%
收藏
已结贴  问题点数:30 回复次数:5 
求教:为什么这个简单程序计算结果第二项(-10 -44 -10.000000)是错误的?
#include<stdio.h>                                                
void main()                                                
{                                                
    int c[41],i[13],j,k=41,g=13;                                            
float m,n,a=2.1,b=1.2;                                                
                                                
                                                
printf("请输入(1)个整数: \n");                                                
for(j=0;j<k;j++)                                                
    scanf("%d",&c[j]);                                            
printf("\n");                                                
                                                
for(j=0;j<g;j++)                                                
    scanf("%d",&i[j]);                                            
printf("\n");                                                
                                                
                                                
                                                
    m=c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]-c[i[2]]+c[i[1]]-c[i[0]],n=i[6]-i[5]+i[4]-i[3]-i[2]+i[1]-i[0];                                            
    printf("%d  ",c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]-c[i[2]]+c[i[1]]-c[i[0]]);                                            
    printf("%d  ",i[6]-i[5]+i[4]-i[3]-i[2]+i[1]-i[0]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]],n=i[7]-i[6]+i[5]-i[4]-i[3]+i[2]-i[1];                                            
    printf("%d  ",c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]]);                                            
    printf("%d  ",i[7]-c[i[6]]+i[5]-i[4]-i[3]+i[2]-i[1]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]-c[i[4]]+c[i[3]]-c[i[2]],n=i[8]-i[7]+i[6]-i[5]-i[4]+i[3]-i[2];                                            
    printf("%d  ",c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]-c[i[4]]+c[i[3]]-c[i[2]]);                                            
    printf("%d  ",i[8]-i[7]+i[6]-i[5]-i[4]+i[3]-i[2]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]],n=i[9]-i[8]+i[7]-i[6]-i[5]+i[4]-i[3];                                            
    printf("%d  ",c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]);                                            
    printf("%d  ",i[9]-i[8]+i[7]-i[6]-i[5]+i[4]-i[3]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[10]]-c[i[9]]+c[i[8]]-c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]],n=i[10]-i[9]+i[8]-i[7]-i[6]+i[5]-i[4];                                            
    printf("%d  ",c[i[10]]-c[i[9]]+c[i[8]]-c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]);                                            
    printf("%d  ",i[10]-i[9]+i[8]-i[7]-i[6]+i[5]-i[4]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[11]]-c[i[10]]+c[i[9]]-c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]],n=i[11]-i[10]+i[9]-i[8]-i[7]+i[6]-i[5];                                            
    printf("%d  ",c[i[11]]-c[i[10]]+c[i[9]]-c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]);                                            
    printf("%d  ",i[11]-i[10]+i[9]-i[8]-i[7]+i[6]-i[5]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
    m=c[i[12]]-c[i[11]]+c[i[10]]-c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]],n=i[12]-i[11]+i[10]-i[9]-i[8]+i[7]-i[6];                                            
    printf("%d  ",c[i[12]]-c[i[11]]+c[i[10]]-c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]);                                            
    printf("%d  ",i[12]-i[11]+i[10]-i[9]-i[8]+i[7]-i[6]);                                            
    if(n!=0)                                            
    printf("%f \n",m/n);                                            
    else                                            
        printf("\n");                                       
                                                
            
                                                
}



[此贴子已经被作者于2016-12-26 11:33编辑过]

2016-12-26 11:22
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
请输入(1)个整数:
5       12      19      21      27      30      35      44      45      48

 51      52      56      58      59      60      62      64      68      71

 72      79      84      90      93      94      96      100     101     106

 111     112     116     119     123     127     131     136     141     143

 152


 0   4   8   10   11   12   15   21   22   24   31   32   35


 -18  0
-10 -44 -10.000000
 -37  -5  7.400000
 -27  -3  9.000000
 -32  -6  5.333333
 -62  -15  4.133333
 -35  -6  5.833333
 Press any key to continue
2016-12-26 11:27
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
-10除以-44不等于-10.00000
2016-12-26 11:29
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:30 
用循环结构执行运算不行么~把加号和减号分别处理就行了~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2016-12-26 11:35
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
我找到问题了!m=c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]],n=i[7]-i[6]+i[5]-i[4]-i[3]+i[2]-i[1];                                            
     printf("%d  ",c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]]);                                            
     printf("%d  ",i[7]-i[6]+i[5]-i[4]-i[3]+i[2]-i[1]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");               

[此贴子已经被作者于2016-12-26 11:56编辑过]

2016-12-26 11:41
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
问题解决了!虽然不知道程序错在何处?但是删除程序,重新编写程序后,计算正确了!

#include<stdio.h>                                                
 void main()                                                
 {                                                
     int c[41],i[13],j,k=41,g=13;                                            
 float m,n,a=2.1,b=1.2;                                                
                                                
                                                
 printf("请输入(1)个整数: \n");                                                
 for(j=0;j<k;j++)                                                
     scanf("%d",&c[j]);                                            
 printf("\n");                                                
                                                
 for(j=0;j<g;j++)                                                
     scanf("%d",&i[j]);                                            
 printf("\n");                                                
                                                
                                                
                                                
     m=c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]-c[i[2]]+c[i[1]]-c[i[0]],n=i[6]-i[5]+i[4]-i[3]-i[2]+i[1]-i[0];                                            
     printf("%d  ",c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]-c[i[2]]+c[i[1]]-c[i[0]]);                                            
     printf("%d  ",i[6]-i[5]+i[4]-i[3]-i[2]+i[1]-i[0]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                             
   
     m=c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]],n=i[7]-i[6]+i[5]-i[4]-i[3]+i[2]-i[1];                                            
     printf("%d  ",c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]-c[i[3]]+c[i[2]]-c[i[1]]);                                            
     printf("%d  ",i[7]-i[6]+i[5]-i[4]-i[3]+i[2]-i[1]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                          
     
     
                                                
     m=c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]-c[i[4]]+c[i[3]]-c[i[2]],n=i[8]-i[7]+i[6]-i[5]-i[4]+i[3]-i[2];                                            
     printf("%d  ",c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]-c[i[4]]+c[i[3]]-c[i[2]]);                                            
     printf("%d  ",i[8]-i[7]+i[6]-i[5]-i[4]+i[3]-i[2]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                                
     m=c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]],n=i[9]-i[8]+i[7]-i[6]-i[5]+i[4]-i[3];                                            
     printf("%d  ",c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]-c[i[5]]+c[i[4]]-c[i[3]]);                                            
     printf("%d  ",i[9]-i[8]+i[7]-i[6]-i[5]+i[4]-i[3]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                                
     m=c[i[10]]-c[i[9]]+c[i[8]]-c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]],n=i[10]-i[9]+i[8]-i[7]-i[6]+i[5]-i[4];                                            
     printf("%d  ",c[i[10]]-c[i[9]]+c[i[8]]-c[i[7]]-c[i[6]]+c[i[5]]-c[i[4]]);                                            
     printf("%d  ",i[10]-i[9]+i[8]-i[7]-i[6]+i[5]-i[4]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                                
     m=c[i[11]]-c[i[10]]+c[i[9]]-c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]],n=i[11]-i[10]+i[9]-i[8]-i[7]+i[6]-i[5];                                            
     printf("%d  ",c[i[11]]-c[i[10]]+c[i[9]]-c[i[8]]-c[i[7]]+c[i[6]]-c[i[5]]);                                            
     printf("%d  ",i[11]-i[10]+i[9]-i[8]-i[7]+i[6]-i[5]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                                
     m=c[i[12]]-c[i[11]]+c[i[10]]-c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]],n=i[12]-i[11]+i[10]-i[9]-i[8]+i[7]-i[6];                                            
     printf("%d  ",c[i[12]]-c[i[11]]+c[i[10]]-c[i[9]]-c[i[8]]+c[i[7]]-c[i[6]]);                                            
     printf("%d  ",i[12]-i[11]+i[10]-i[9]-i[8]+i[7]-i[6]);                                            
     if(n!=0)                                            
     printf("%f \n",m/n);                                            
     else                                            
         printf("\n");                                       
                                                
            
                                                
 }

请输入(1)个整数:
5       12      19      21      27      30      35      44      45      48

  51      52      56      58      59      60      62      64      68      71

  72      79      84      90      93      94      96      100     101     106

  111     112     116     119     123     127     131     136     141     143

  152

0   4   8   10   11   12   15   21   22   24   31   32   35

-18  0
-10  1  -10.000000
-37  -5  7.400000
-27  -3  9.000000
-32  -6  5.333333
-62  -15  4.133333
-35  -6  5.833333
Press any key to continue




2016-12-26 12:04
快速回复:求教:为什么这个简单程序计算结果第二项(-10 -44 -10.000000)是错 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved