这道题目涉及到了C语言结构体,函数指针,排序的相关内容
函数
小王是公司的仓库管理员,一天,他接到了这样一个任务:从仓库中找出一根钢管(仓库中待测的钢管数不超过1000根)。这听起来不算什么,可是这根钢管的要求可真是让他犯难了,要求以下:
一、 这根钢管必定要是仓库中最短的;
二、 这根钢管必定要是最短的钢管中最粗的;
三、 这根钢管必定要是符合前两条的钢管中编码最大的(每根钢管都有一个互不相同的编码,越大表示生产日期越近)。
相关的资料到是有,但是,手工从几百份钢管材料中选出符合要求的那根……
要不,仍是请你编写个程序来帮他解决这个问题吧。
输入测试的钢管数n,整数n(n<=1000)表示测试数据的组数)
每根钢管有这样三个信息,分别表示一根钢管的长度(以毫米为单位)、直径(以毫米为单位)和编码(一个含有9位数字的字符串)。
输出符合条件的那根钢管
测试
钢管结构体定义以下:编码
typedef struct m { int a;//长度 int b;//直径 char c[10];//编号 }Infor;
输入提示信息:"请输入钢管数量:\n"
输入数据格式:"%d"
输入提示信息:"请输入钢管信息(长度、直径和编码:\n"
输入数据格式:"%d%d%s"
输出提示信息:printf("仓库中符合条件的那根钢管的信息是:\n");
输出数据格式:"%d\t%d\t%s\n"
指针
#include <stdio.h> #include <stdlib.h> #include <string.h> const int maxn = 1010; typedef struct m info; struct m { int a; int b; char c[10]; }; void sort(int n, info a[],int (*cmp)(info, info)); int SortBya(info, info); int SortByb(info, info); int SortByc(info, info); int main(void) { int n; info a[maxn]; printf("请输入钢管数量:\n"); scanf("%d",&n); printf("请输入钢管信息(长度、直径和编码:\n"); int i; for(i=0;i<n;i++) scanf("%d %d %s",&a[i].a, &a[i].b, a[i].c); sort(n, a, SortBya); int find = 0, mark=0; int t = a[0].a; while(!find) { mark ++; if(t != a[mark].a) find = 1; } sort(mark, a, SortByb); find = 0; mark = 0; t = a[0].b; while(!find) { mark ++ ; if(t != a[mark].b) find = 1; } sort(mark, a, SortByc); printf("仓库中符合条件的那根钢管的信息是:\n"); printf("%d\t%d\t%s\n", a[0].a, a[0].b, a[0].c); return 0; } void sort(int n, info a[],int (*cmp)(info, info)) { int i,j; for(i=1;i<n;i++) for(j=0;j<n-i;j++) if((*cmp)(a[j], a[j+1])) { info temp; temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } int SortBya(info a, info b) { if(a.a > b.a) return 1; else return 0; } int SortByb(info a, info b) { if(a.b < b.b) return 1; else return 0; } int SortByc(info a, info b) { long int t1,t2; t1 = atol(a.c); t2 = atol(b.c); if(t1 < t2) return 1; else return 0; }