| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1071 人关注过本帖
标题:作业里的问题,求指教,谢了!
只看楼主 加入收藏
shutyourhead
Rank: 1
等 级:新手上路
帖 子:52
专家分:2
注 册:2010-8-22
收藏
 问题点数:0 回复次数:10 
作业里的问题,求指教,谢了!
题目:函数fun的功能是:对a数组中n种商品价格进行分段统计,商品各段价格的数目存到b数组中; 其中:价格为100元以下的个数存到b[0]中,价格为100元到199元的个数存到b[1],价格为200元到299元的个数存到b[2],价格为300元到399元的个数存到b[3],价格为400元到499元的个数存到b[4],价格为500元以上的个数存到b[5]中。
int i;                              或者 int i;
for(i=0;i<6;i++) b[i]=0;                for(i=0;i<6;i++) b[i]=0;
for(i=0;i<strlen(a);i++)                 for(i=0;i<strlen(a);i++)
switch(a[i]/100)                              if(a[i]/100<5) b[a[i]/100]++;
{case 0: b[0]++;break;                   else b[5]++;
 case 1: b[1]++;break;
 case 2: b[2]++;break;红色部分代码不能改。

 case 3: b[3]++;break;
 case 4: b[4]++;break;
default: b[5]++;}
 我写的代码如下(错了很多):我写得很烂,希望各位大侠能帮我改改,或者给我个优化点的代码,谢le!
#include<stdio.h>
#include<string.h>
void fun(int a[])
{
int i,b[5];
for(i=0;i<6;i++)
b[i]=0;
for(i=0;i<strlen(a);i++)
switch(a[i]/100)
{
case 0:b[0]++;break;
case 1:b[1]++;break;
case 2:b[2]++;break;
case 3:b[3]++;break;
case 4:b[4]++;break;
default:b[5]++;
}
printf("the price under 100 yuan: %d\n",b[0]);
printf("the price for 100 to 200 yuan: %d\n",b[1]);
printf("the price for 200 to 300 yuan: %d\n",b[2]);
printf("the price for 300 to 400 yuan: %d\n",b[3]);
printf("the price for 400 to 500 yuan: %d\n",b[4]);
printf("the price more than 500 yuan: %d\n",b[5]);
}
void main()
{
int a[20],i,j,*k,n=20;
printf("the price of %d input\n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);
fun(a);

搜索更多相关主题的帖子: 作业 指教 
2010-08-23 17:22
shutyourhead
Rank: 1
等 级:新手上路
帖 子:52
专家分:2
注 册:2010-8-22
收藏
得分:0 
哎,还是没人回答
2010-08-23 18:58
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:0 
声明的是int类型的数组,用strlen求长度的?明显不对的吗
2010-08-23 19:04
S_12s
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:110
专家分:670
注 册:2010-7-21
收藏
得分:0 
我水平有限……把红色的代码也改了……你的fun()函数中的b[5]改成b[6];default后加个break;
#include<stdio.h>
#include<string.h>


void fun(int a[],int n)
{
    int i,b[6];
    for(i=0;i<6;i++)
        b[i]=0;
    for(i=0;i<n;i++)
    {
        switch(a[i]/100)
        {
            case 0:b[0]++;break;
            case 1:b[1]++;break;
            case 2:b[2]++;break;
            case 3:b[3]++;break;
            case 4:b[4]++;break;
            default:b[5]++;break;
        }
    }
    printf("the price under 100 yuan: %d\n",b[0]);
    printf("the price for 100 to 200 yuan: %d\n",b[1]);
    printf("the price for 200 to 300 yuan: %d\n",b[2]);
    printf("the price for 300 to 400 yuan: %d\n",b[3]);
    printf("the price for 400 to 500 yuan: %d\n",b[4]);
    printf("the price more than 500 yuan: %d\n",b[5]);
}


void main()
{
    int a[20],i,n=20;
    printf("the price of %d input\n",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    fun(a,n);
}
2010-08-23 19:12
shutyourhead
Rank: 1
等 级:新手上路
帖 子:52
专家分:2
注 册:2010-8-22
收藏
得分:0 
以下是引用erikyo在2010-8-23 19:04:06的发言:

声明的是int类型的数组,用strlen求长度的?明显不对的吗
老师给的题目就是这样,没办法。所以我不会写
2010-08-24 13:25
shutyourhead
Rank: 1
等 级:新手上路
帖 子:52
专家分:2
注 册:2010-8-22
收藏
得分:0 
以下是引用S_12s在2010-8-23 19:12:04的发言:

我水平有限……把红色的代码也改了……你的fun()函数中的b[5]改成b[6];default后加个break;
#include
#include


void fun(int a[],int n)
{
    int i,b[6];
    for(i=0;i<6;i++)
        b=0;
    for(i=0;i
这样改我知道可以,不过老师要求红色部分不能变
2010-08-24 13:26
唐小虎
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:46
专家分:119
注 册:2010-6-25
收藏
得分:0 
#include<stdio.h>
#include<string.h>
void fun(int a[],int b[],int n)
{

int i;
for(i=0;i<n;i++)
switch(a[i]/100)
{
case 0:b[0]++;break;
case 1:b[1]++;break;
case 2:b[2]++;break;
case 3:b[3]++;break;
case 4:b[4]++;break;
default:b[5]++;
}

}
void main()
{
int a[20],b[5],i,n=20;
for(i=0;i<6;i++)
b[i]=0;
printf("the price of %d input\n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);
fun(a,b,n);
printf("the price under 100 yuan: %d\n",b[0]);
printf("the price for 100 to 200 yuan: %d\n",b[1]);
printf("the price for 200 to 300 yuan: %d\n",b[2]);
printf("the price for 300 to 400 yuan: %d\n",b[3]);
printf("the price for 400 to 500 yuan: %d\n",b[4]);
printf("the price more than 500 yuan: %d\n",b[5]);
}
感觉该的多了!!看看吧!
2010-08-24 14:35
shutyourhead
Rank: 1
等 级:新手上路
帖 子:52
专家分:2
注 册:2010-8-22
收藏
得分:0 
回复 7楼 唐小虎
大哥,写出程序好写,不过要求是不能改红色部分,这就有点难办。特别是strlen(a),如果改成n,题目就简单了。
所以这题不好写就是这样。我也想想,大家都多想想
2010-08-24 14:47
baboon0001
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2010-8-24
收藏
得分:0 
#include <stdio.h>
main()
{int a[100]={0},b[10]={0},c,d,e,f,n=20;
 clrscr();
 printf("the price of %d input\n",n);
 for(c=1;c<=n;c++)
 {scanf("%d",&a[c]);
 d=(int)a[c]/100;
 if(d>=6){b[10]++;}
 else{b[d]=b[d]+1;}}
    printf("the price under 100 yuan: %d\n",b[0]);
    printf("the price for 100 to 200 yuan: %d\n",b[1]);
    printf("the price for 200 to 300 yuan: %d\n",b[2]);
    printf("the price for 300 to 400 yuan: %d\n",b[3]);
    printf("the price for 400 to 500 yuan: %d\n",b[4]);
    printf("the price more than 500 yuan: %d\n",b[5]);

 }
2010-08-24 21:08
carmeloyin
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
帖 子:161
专家分:157
注 册:2008-12-2
收藏
得分:0 
strlen(a)
光这1句就不对
怎么写?
2010-08-25 12:09
快速回复:作业里的问题,求指教,谢了!
数据加载中...
 
   



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

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