day03

70 阅读2分钟

day02学习改进

可以使用string类型来表示

  1. 一个小球从10m处落下,每次的弹回是之前高度一半,请问小球在静止之前一共走过多少距离。

思路:

思路1:

  1. 小球的高度变为h/2;
  2. 求等比数列的极限。
  3. 将n取到max

思路2:

分开计算。

  1. 下落距离+反弹距离
  2. 判断条件,高度为0.

知识点:

  1. 引入limits.h其中包含INT_MAX,INT_MIN的宏

具体实现:

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

int main()
{
    int h=10;
    float q=0.5;

    int i;
    int result=1;

    float tmp=1;

    for(i=1;i<INT_MAX;i++)
    {
        tmp=tmp*q;
    }

    result=h*(1-tmp)/(1-q)*2-h;
    printf("%d",result);

    return 0;
}

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

int main()
{
    double intial_h=10;
    double bounce_h=intial_h/2;

    double h=0;

    while(bounce_h>0)
    {
        h+=bounce_h*2;
        bounce_h/=2;
    }
    h+=intial_h;

    printf("%f",h);

    return 0;
}

  1. 我要通过!

“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的答案正确大派送----只要输入的字符串满足下列条件,系统就会输出“答案正确”,否则就输出“答案错误”

得到“答案正确”的条件:

  1. 字符串中必须仅有P、A、T这三种字符,不可以包含其他字符。
  2. 任意形如xPATx的媳妇穿都可以获得“答案正确”,其中x或者是空字符串,或者仅有字母A组成的字符串。
  3. 如果aPbTc是正确的,那么aPbATca也是正确的,其中a,b,c均或者是空字符串,或者是仅有A组成的字符串。

思路:

  1. PAT 正确,则P T之间包含多个A都正确
  2. 如果a,b,c都是A,APATA正确,则APAATAA正确。
  3. 总之类比推理得到的结果为PT之间A的个数与P之前的A的个数相乘的乘积为T之后A的个数。
  4. 所以,统计PT之前之后中间A的个数即可。进行判断是否符合乘积关系。

具体实现:

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

void judgeYesOrNo()
{
    int num=0;
    scanf("%d",&num);
    char ch;

    while(getchar()!='\n');
    //吸收掉n之后的第一个回车

    for(int i=0;i<num;i++)
    {
        int pos=0,record_A[3]={0,0,0};
        while((ch=getchar())!='\n')
        {
            if(ch=='A')
            {
                record_A[pos]++;
            }
            else if(ch=='P'&&pos==0)
                pos=1;
            else if(ch=='T'&&pos==1)
                pos=2;
            else
                break;
        }

        if(ch=='\n'&&pos==2&&record_A[0]*record_A[1]==record_A[2]&&record_A[1])
            printf("yes\n");
        else
            printf("no\n");
        if(ch!='\n')
            while(getchar()!='\n');
    }

}

int main()
{
    //1. 统计P之前的数目
    //2. 统计PT之间的数目
    //3. 统计T之后的数目
    //那么是不是首先出现P,再出现T才可以
    judgeYesOrNo();

    return 0;
}