100岁以上的判断出错,原因在于,你把100岁以上的人,仍然按10岁一级统计了(分别统计了110、120...以下),你的fun函数修改后(如果其他的地方没有错的话):
创新互联专注于济宁企业网站建设,自适应网站建设,成都做商城网站。济宁网站建设公司,为济宁等地区提供建站服务。全流程定制设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
void fun(int *a, int *b)
{
int i,j,n;
for(j=0;jM;j++)
b[j]=0; //计数器初始化
for(i=0;iN;i++) //循环判断所有的年龄
{
n=a[i]/10;
if(n10)
b[a[i]/10]++;
else b[10]++;
}
#include stdio.h
int main(void)
{
int num,A = 0,B = 0,C = 0,D = 0,E = 0,F = 0;
int i,score;
scanf("%d",num);
for(i = 0; i num; i++)
{
scanf("%d",score);
score /= 10;
switch(score)
{
case 9: A++;break;
case 8: B++;break;
case 7: C++;break;
case 6: D++;break;
default: E++;
}
}
printf("%d %d %d %d %d",A,B,C,D,E);
return 0;
}
凑个热闹
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define N 7
typedef struct node
{
char num[3]; /*教室的编号*/
char name[9]; /*教师的姓名*/
char sex[5]; /*教师的性别*/
char prof[6]; /*教师的职称*/
char dept[10]; /*教师所在院系*/
struct node * link;
}teacher;
void fun_count_prof( teacher *head )
{
teacher *p=head-link ;
char prof[6]="";
int cnt=0;
while (p)
{
if ( strcmp(prof,p-prof) )
{
if ( prof[0] )
printf("prof:%s cnt=%d\n" , prof , cnt );
//统计下一职称
cnt=0;
strcpy(prof,p-prof) ;
}
cnt++;
p=p-link ;
}
if ( prof[0] )
printf("prof:%s cnt=%d\n" , prof , cnt ) ;
}
void fun_count_dept_prof( teacher *head )
{
teacher *p=head-link ;
char prof[6]="";
char dept[10]="";
int cnt=0;
while (p)
{
//检查职称变化
if ( strcmp(prof,p-prof) )
{
if ( prof[0] )
printf("dept:%s prof:%s cnt=%d\n" , dept , prof , cnt ) ;
//统计下一职称
cnt=0;
strcpy(dept,p-dept) ;
strcpy(prof,p-prof) ;
}
//检查院系变化
if ( strcmp(dept,p-dept) )
{
//输出上一院系的最后一个统计
printf("dept:%s prof:%s cnt=%d\n" , dept , prof , cnt ) ;
//统计下一院系
cnt=0;
strcpy(dept,p-dept) ;
}
cnt++;
p=p-link ;
}
if ( prof[0] )
printf("dept:%s prof:%s cnt=%d\n" , dept , prof , cnt ) ;
}
void crt_link( teacher *head )
{
//模拟数据:要求数据按职称由高到低,职称下按部门排序,方便统计
struct teach_er
{
char num[3]; /*教室的编号*/
char name[9]; /*教师的姓名*/
char sex[5]; /*教师的性别*/
char prof[6]; /*教师的职称*/
char dept[10]; /*教师所在院系*/
} stTeacher[N]={
{"01","name0","male","prof0" , "mac"},
{"02","name1","male","prof0" , "com"},
{"03","name2","male","prof0" , "com"},
{"04","name3","male","prof1" , "mac"},
{"05","name4","male","prof1" , "mac"},
{"06","name5","male","prof1" , "com"},
{"07","name6","male","prof2" , "com"},
};
teacher *tail=head , *p ;
int i=0;
for ( i=0;iN;i++ )
{
p=malloc(sizeof(teacher) );
strcpy(p-num,stTeacher[i].num );
strcpy(p-name,stTeacher[i].name );
strcpy(p-sex,stTeacher[i].sex );
strcpy(p-prof,stTeacher[i].prof );
strcpy(p-dept,stTeacher[i].dept );
p-link=NULL ;
tail-link=p;
tail=p;
}
}
int main()
{
teacher *head;
head=malloc( sizeof(teacher) );
head-link=0;
crt_link( head );
fun_count_prof( head );
fun_count_dept_prof( head );
return 0;
}
#includestdio.hint count[5];const char *str[5] = {"1-59: ","60-69: ","70-79: ","80-89: ","90-100: "};int main(void){ int score,i; while(1) { scanf("%d",score); if(score==-1) break; if(score=1 score =59) count[0]++; else if(score = 60 score=69) count[1]++; else if(score=70 score=79) count[2]++; else if(score=80 score=89) count[3]++; else if(score=90 score=100) count[4]++; else printf("输入错误"); } for(i = 0; i 5; i++) { printf("%s%d\n",str[i],count[i]); } return 0;}
#include stdio.h //使用printf函数要包含的头文件void main(void) //主函数
{
int n;
int x1=0,x2=0,x3=0,x4=0,x5=0;
printf("请输入学生的成绩1~100:\n");
scanf("%d",n);
while(n!=-1)
{
if(1=nn=59)
{
x1+=1;
}
else if(60=nn=69)
{
x2+=1;
}
else if(70=nn=79)
{
x3+=1;
}
else if(80=nn=89)
{
x4+=1;
}
else
{
x5=+1;
}
scanf("%d",n);
}
printf("1~59分数段的人数为:%d\n",x1);
printf("60~69分数段的人数为:%d\n",x2);
printf("70~79分数段的人数为:%d\n",x3);
printf("80~89分数段的人数为:%d\n",x4);
printf("90~100分数段的人数为:%d\n",x5);
}
我说说思路
定义一个枚举类型保存职称
enum
{
Teaching_Assistant, //助教
Lecturer, //讲师
Associate_Professor,//副教授
Professor //教授
};
定义一个结构体保存教师信息
typedef struct TeacherInfo
{
char name[20];//保存教师姓名
int TITLE;//保存职称,在上面定义的enum中选取
}TI;
主函数中
#define NUM 20 //教师人数
int main(void)
{
TI Teacher[NUM];//声明20位老师信息
//输入20位老师信息
//判断教师职称,统计信息
//输出
return 0;
}
有了上面的思路,实现起来就很容易了