#include <stdio.h>
#include <stdlib.h>
struct book{
char name[31];
double charge;
}B[13];
int main(){
int n,i,max=0,min=0;;
char c;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%c",&c);//读入缓冲区的"\n
gets(B[i].name);
scanf("%lf",&B[i].charge);
if(B[i].charge>B[max].charge){
max = i;
}
if(B[i].charge<B[min].charge){
min = i;
}
}
printf("%.2f, %s\n",B[max].charge,B[max].name);
printf("%.2f, %s",B[min].charge,B[min].name);
}
#include <stdio.h>
#include <stdlib.h>
struct pp{
char name[10];
int brithday;
char tNum[20];
}P[12];
int main(){
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s%d%s",P[i].name,&P[i].brithday,P[i].tNum); //这道题就和上一道不一样因为字符串中不会有空格,且下一个数据不需要换行所以只能用scanf
}
//按照年龄从大到小即是 出生年月日从小到大排序 选择排序
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(P[i].brithday>P[j].brithday){
P[12] = P[i];
P[i] = P[j];
P[j] = P[12];
}
}
}
for(i=0;i<n;i++){
printf("%s %d %s\n",P[i].name,P[i].brithday,P[i].tNum);
}
}
首先线区别gets 和scanf的区别:
二者都是从终端读入字符串。功能为
1、 gets功能为读入一行,并将换行符转换为字符串结束符。
2、 scanf("%s",s);读入时,遇到空白字符,包括空格,制表符,换行符时均会停止输入。
这里来讲讲为什么9-4要使用gets来接收字符串,而9-5要使用scanf("%s",s)呢
9-4: 输入格式为字符串 回车 数字,且字符串中有空格。
若scanf("%d",&n); 后面马上要gets(s); 中间需要加一个 scanf("%c",&c);来读入缓冲区的"\n ,不然gets(s)会直接读入\n并结束。
即输入数字后要输入字符串使用gets();需要在输入数字之后加入scanf("%c",&c);来读入缓冲区的"\n。
但是反过来观察9-5的输入格式,字符串和数字是只隔了一个空格且,字符串中间没有空格,所以只能用scanf