HackerRank初级篇之Apple and Orange

本文介绍了一个简单的控制台应用示例——水果投掷游戏。该程序通过读取房子坐标范围及水果初始位置与投掷距离,计算并输出落在指定房屋范围内的水果数量。此示例适合初学者学习C++编程的基础知识。

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

题目说明;


题目解读:

(读懂题目很重要,题目懂了,算法逻辑都很简单了)

输入格式:

1、给出房子坐标(只有在这个范围内才算落在房子上,才能得分)

2、给出apple和orange的坐标(起始位置)

3、给出apple和orange数目

4、给出apple的每次投掷距离

5、给出orange的每次投掷距离

示例代码:

// apple_orange.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

//by zhaocl

int main()
{
    int score_larry = 0;
    int score_rob = 0;
    int house_a, house_b;
    cin >> house_a >> house_b;
    int apple_distance, orange_distance;
    cin >> apple_distance >> orange_distance;
    int apple_num, orange_num;
    cin >> apple_num >> orange_num;
    int apple_point, orange_point;

    while( apple_num-- )
    {
        cin >> apple_point;
        apple_point += apple_distance;

        if( apple_point >= house_a && apple_point <= house_b )
            score_larry++;
    }

    while( orange_num-- )
    {
        cin >> orange_point;
        orange_point += orange_distance;

        if( orange_point >= house_a && orange_point <= house_b )
            score_rob++;
    }

    cout << score_larry << endl << score_rob << endl;
    system( "pause" );
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值