当前位置: 代码迷 >> C语言 >> 柱状图~
  详细解决方案

柱状图~

热度:580   发布时间:2006-10-17 10:46:17.0
柱状图~

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
int *p,n=0,i,j;
char t;
char q[11][20]={"0:","10:","20:","30:","40:","50:","60:","70:","80:","90:","100:"};
p=(int*)malloc(100*sizeof(int));
while(*(p-1)!=-1)
{
scanf("%ld",p);
p++;
n++;
}
p=p-n;
for(i=0;i<n-1;i++)
{
switch (*(p++)/10)
{
case 0:
strcat(q[0],"*");
break;

case 1:
strcat(q[1],"*");
break;
case 2:
strcat(q[2],"*");
break;
case 3:
strcat(q[3],"*");
break;
case 4:
strcat(q[4],"*");
break;
case 5:
strcat(q[5],"*");
break;
case 6:
strcat(q[6],"*");
break;
case 7:
strcat(q[7],"*");
break;
case 8:
strcat(q[8],"*");
break;
case 9:
strcat(q[9],"*");
break;
case 10:
strcat(q[10],"*");
break;
}
}
for(i=0;i<=10;i++)
{
printf("%s\n",q[i]);
}
free(p);
}

输入一串整数(0-100),假设输入10,20,20,30然后输出格式如下:
0;
10;*
20;**
30;*
40;
50;
60;
70;
80;
90;
100;

上面那个程序是对的!现在我要把程序的输入格式变成垂直的:
*
* * *
0 10 20 30 40 50 60 70 80 90 100

程序要怎么改?

搜索更多相关的解决方案: 柱状  include  char  int  

----------------解决方案--------------------------------------------------------

改用数组存放*号的个数.同时保存输入数据个数的最大值(max).再输出时,比较每个元素是否等于max则输出*,同时将a[i]--.否则输出空格.循环一次max--,后面要换行.待*号全部输出.再输出
0 10 20 30 40 50 60 70 80 90 100

/*看这个可以不?*/
#include<stdio.h>

int main()
{
int i,max=0,num,a[11];
for(i=0;i<11;i++)
{
a[i]=0;

}
while(scanf("%d",&num)&&num!=-1)
{
a[num/10]++;
}
for(i=0;i<11;i++)
{
if(a[i]>max)
{
max=a[i];
}
}

while(max)
{
for(i=0;i<11;i++)
{

if(a[i]==max)
{
printf("*");
a[i]--;
}
else
{
printf("");
}
}
printf("\n");
max--;
}
getch();
return(0);
}


----------------解决方案--------------------------------------------------------
  相关解决方案