relay 2015-02-05 21:00 27人阅读 评论(0) 收藏

scanf函数是以在输入多个数值数据时,若格式控制串中没有非格式字符作输入数据之间的间隔,则可用空格,TAB或回车作间隔。

C编译在碰到空格,TAB,回车或非法数据(如对“%d”输入“12A”时,A即为非法数据)时即认为该数据结束。
getchar函数getchar 由实现:#define getchar() getc(stdin)。getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar才开始从stdio流中每次读入一个字符.

缓冲区不属于scanf printf函数范围中。是新的一片区域。

//这个程序虽然比较小,但是包含的各种技能还是很多的!

/*
#include<stdio.h>
#include<iostream>
using namespace std;

int main()
{
    int n,sumtime,num;
    double d;
    char h,m1,m2,s1,s2;
    scanf("%d",&n);
    scanf("%lf",&d);
    while(scanf("%d",&num)!=EOF)//题目意思是对于每个队伍便输出答案
    {
        printf("%3d ",num);
        bool flag=true;
        sumtime=0;
        for(int i=0;i<n;i++)
        {
            getchar();//将键盘输入的全读入缓冲区直到按下第二次回车才回到scanf printf中。
            scanf("%c:%c%c:%c%c",&h,&m1,&m2,&s1,&s2);
            if(h=='-') flag=false;
            if(flag==false) continue;//这里如果输入了"-:--:--",那么接下来就不用计算了!
            sumtime=sumtime+(h-'0')*3600+((m1-'0')*10+(m2-'0'))*60+(s1-'0')*10+s2-'0';
        }
        //cout<<sumtime<<endl;//这里计算总时间是正确的!
        
        if(flag)
        {
            int t2=sumtime/d+0.5;//这里算出来了平均每走一千米所需要的时间(s),这里由于数据的缘故需要四舍五入!
            //cout<<"t2="<<t2<<endl;
            if(t2-t2/60*60<10)
            printf("%d:0%d min/km ",t2/60,t2-t2/60*60);
            else
            printf("%d:%d min/km ",t2/60,t2-t2/60*60);
        }
        else
            printf("- ");

    }
    return 0;
}


原文地址:https://www.cnblogs.com/ZP-Better/p/4639621.html