杭电ACM step(1.2.1-1.2.8)

这篇博客介绍了杭电ACM训练的第一章第二部分,内容包括Elevator、Biker's Trip Odometer等题目,重点考察了gets()和getchar()的使用,以及基础的文本处理和数字操作。尽管难度较第一章略有增加,但依然属于入门级别,主要侧重于基础编程技巧和函数应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一章的第二部分开始开始涉及一些计算


Elevator

#include <stdio.h>

int main()
{
   int N,requestfloor,currentfloor=0,time=0;
   while(scanf("%d",&N) != EOF){
        if(N==0){
            break;
        }else{
       for(int i=0;i<N;i++){
        scanf("%d",&requestfloor);
        if(requestfloor > currentfloor){
            time += (requestfloor-currentfloor) * 6;
        }else{
            time += (currentfloor-requestfloor) * 4;
        }
        currentfloor = requestfloor;
       }
       time += 5*N;
     printf("%d\n",time);
     time = 0;
     currentfloor = 0;
   }
   }
return 0;
}

Biker’s Trip Odometer

#include <stdio.h>
#define PI 3.1415927
int main()
{
   double diameter,time;
   int revolutions,flag=1;
   double res1,res2;
   while(scanf("%lf%d%lf",&diameter,&revolutions,&time) != EOF){
        if(revolutions==0){
            break;
        }else{
            res1 = diameter*PI*revolutions/(12*5280);
            res2 = res1*3600/time;
     printf("Trip #%d: %.2lf %.2lf\n",flag++,res1,res2);
   }
   }
return 0;
}

Text Reverse
( 考察gets()和getchar() )

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int main(void)
{
    int n, k, l, i, j;
    char a[1000+10];
    while (scanf("%d", &n)!=EOF)
    {
        getchar();
        while (n--)
        {
            gets(a);
            k = l = 0;
            for (i = 0; a[i] != '\0'; ++i)
            {
                if (a[i] == ' ')
                {
                    k = i;
                    for (j = k - 1; j >= l; --j)
                        printf("%c", a[j]);
                    l = k + 1;
                    printf("%c", a[i]);
                }
            }
            for (i = strlen(a) - 1; i >= l; --i)
                printf("%c", a[i]);
            printf("\n");
        }
    }

    return 0;
}

Buildings

#include <stdio.h>

int main()
{
    int T,n,m,num=0,flag;
    while(scanf("%d",&T)!=EOF){
        while(T--){
            scanf("%d%d",&n,&m);
            for(int i=0;i<n*m;i++){
                    scanf("%d",&flag);
                    if(flag ==1){
                        num++;
                    }
            }
            printf("%d\n",num);
            num=0;
        }
    }
return 0;
}

Balloon Comes!

#include <stdio.h>

int main()
{
    int T,n,m;
    double b;
    char c;
    scanf("%d",&T);
        while(T--){
            c=getchar();
            scanf("%c%d%d",&c,&n,&m);
            switch(c){
            case '+': printf("%d\n",n+m);break;
            case '-': printf("%d\n",n-m);break;
            case '*': printf("%d\n",n*m);break;
            case '/': if(n%m==0){printf("%d\n",n/m);break;}b=(double)n/m;printf("%.2f\n",b);break;
            default : break;
            }
        }
return 0;
}

Lowest Bit

#include <stdio.h>

int main()
{
    int n,i,m;
    int a[7];
    while(scanf("%d",&n)!=EOF,n){
        m=1;
        i=0;
        while(n){
            a[i]=n%2;
            n/=2;
            i++;
        }
        for(int j=0;j<7;j++){
           if(a[j]!=1){
            m=m*2;
           }else{
               printf("%d\n",m);
               break;
           }
        }
    }
return 0;
}

Specialized Four-Digit Numbers
(注意一定要写成函数,函数用重写的形式,最后要换行,不然不能AC)

#include <stdio.h>
int ten(int i){
    int a=i%10;
    i/=10;
    a+=i%10;
    i/=10;
    a+=i%10;
    i/=10;
    a+=i%10;
    return a;

}
int twil(int i){
    int b=i%12;
    i/=12;
    b+=i%12;
    i/=12;
    b+=i%12;
    i/=12;
    b+=i%12;
    return b;
}
int six(int i){
    int c=i%16;
    i/=16;
    c+=i%16;
    i/=16;
    c+=i%16;
    i/=16;
    c+=i%16;
    return c;
}
int main()
{
    int i,a=0,b=0,c=0;
for(i=2992;i<10000;i++){
    a=ten(i);
    b=twil(i);
    c=six(i);
    if(a==b && b==c){
            printf("%d\n",i);
    }

}
return 0;
}

Vowel Counting

#include <stdio.h>

int main()
{
int T,i;
scanf("%d",&T);
getchar();
while(T--){
    char word[50] = "\0";
    gets(word);
    for(i=0;i<50;i++){
        if(word[i]=='a' || word[i]=='i' || word[i]=='e' || word[i]=='o' || word[i]=='u'){
            word[i] -= 32;
        }
        if(word[i] >='A' && word[i] <='Z' && word[i]!='A' && word[i]!='I' && word[i]!='E' && word[i]!='O' && word[i]!='U'){
            word[i] += 32;
        }
    }
    printf("%s\n",word);
}
return 0;
}

第二部分的内容相较于第一部分只有简单的A+B来说,难度稍有提升,但是还是非常基础的内容,基本不涉及算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值