首页 > 试题广场 >

访友

[编程题]访友
  • 热度指数:9242 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小易准备去拜访他的朋友,他的家在0点,但是他的朋友的家在x点(x > 0),均在一条坐标轴上。小易每一次可以向前走1,2,3,4或者5步。问小易最少走多少次可以到达他的朋友的家。

输入描述:
一行包含一个数字x(1 <= x <= 1000000),代表朋友家的位置。


输出描述:
一个整数,最少的步数。
示例1

输入

4

输出

1
示例2

输入

10

输出

2
Java解答:若x除5后有余数则+1.
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int m = x/5;
        if (x%5 != 0)
            m++;
        System.out.println(m);
    }
}

发表于 2019-08-02 20:11:25 回复(0)
1. 少于5,输出1
2. 等于5或5的倍数,输出n/5
3. 大于五,输出n/5的向上取整
综上,直接向上取整可满足全部情况:
import math
print(math.ceil(int(input())/5))
运行时间:25ms
占用内存:3556k
编辑于 2019-09-02 14:49:16 回复(0)
采用模拟的方式进行倒推
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String strX;
        while((strX = br.readLine()) != null) {
            int x = Integer.parseInt(strX);
            int step = 0;
            while(true){
                // 从最大的步长5开始倒推
                int start = 5;
                // 如果走出这一步没有到0的左边,则可以跨出这一步,否则减小步长
                while(x - start < 0 && start > 0) start --;
                // 跨出这一步,步数增加
                x -= start;
                step ++;
                // 到达原点,返回总步数
                if(x == 0) break;
            }
            System.out.println(step);
        }
    }
}

发表于 2020-11-25 12:10:38 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x;
    while(cin >> x)
    {
        int cnt = x/5 + (x%5 ? 1 : 0);//设需要走cnt次,若x除5后有余数则次数加1
        cout << cnt << endl;
    }

    return 0;
}

编辑于 2019-07-03 12:50:57 回复(1)
#include <stdio.h>
int main(){
    int x;
    scanf("%d",&x);
    printf("%d\n",(x-1)/5+1);
    return 0;
}
发表于 2019-10-02 15:08:49 回复(0)
"""
上取整
"""
import math

if __name__ == "__main__":
    n = int(input().strip())
    print(math.ceil(n / 5))

发表于 2019-07-18 19:12:27 回复(1)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x,i=0,res=0;
    cin>>x;
    while(i!=x)
    {
        if(x-i>=5)
            i+=5;
        else if((x-i<5)&&(x-i>=4))
            i+=4;
        else if((x-i<4)&&(x-i>=3))
            i+=3;
        else if((x-i<3)&&(x-i>=2))
            i+=2;
        else
            i+=1;
        res++;
    }
    cout<<res<<endl;
    return 0;
}

发表于 2019-07-11 20:20:31 回复(0)
# 贪心,尽量走大步,后续一步到家
# 注意只走大步就能到家的情况

x = int(input())
if x%5 == 0:
    print(x//5)
else:
    print(x//5+1)
发表于 2020-07-25 02:14:40 回复(0)
#include <iostream> 

int main(){
    int x;
    std::cin >> x;
    std::cout << x / 5 + ((x%5>0)?1:0) << std::endl;
}

发表于 2020-07-16 11:00:43 回复(0)
//可以理解为动态规划的题
import java.io.*;
public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader( new InputStreamReader( System.in ));
        int n = Integer.parseInt(br.readLine());
        if(n<=5){
            System.out.println(1);
            return;
        }
        int[] dp = new int[n+1];
        for(int i = 0;i<=5;i++)
            dp[i] =1;
        
        //
        for(int i = 6;i<=n;i++){
            int min = Integer.MAX_VALUE;
            for(int j = i-1;j>i-5-1;j--){
                min = Math.min(dp[j],min);
            }
            dp[i] = 1+min;
        }
        System.out.println(dp[n]);
    }
}
一看到跳台阶,就想用动态😂
发表于 2020-05-03 17:37:43 回复(0)
求一个一行的代码
x = int(input())
print(int(x / 5) + (0 if x % 5 == 0 else 1))



发表于 2019-12-03 10:29:38 回复(0)
#include <iostream>
using namespace std;
int main()
{
    int x;
    cin >> x;
    if(x%5)
        cout << x/5+1<< endl;
    else
        cout << x/5 << endl;
    return 0;
}

发表于 2019-11-01 15:55:37 回复(0)
// 第一反应,想到青蛙跳台阶的问题了, 一看见大家的答案,我发现又被自己绣了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int f[N];
int main() {
    int n = 0;
    while (cin >> n) {
        fill(f, f+n, INT_MAX);
        if (n <= 5) {
            cout << 1 << endl;
            continue;
        }
        for (int i=1; i<=5; ++i) {
            f[i] = 1;
        }
        for (int i=6; i<=n; ++i) {
            for (int j=1; j<=5; ++j) {
                f[i] = min(f[i], f[i-j]);
            }
        }
        cout << f[n] <<endl;
    }
    return 0;
}

发表于 2019-10-17 19:38:03 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x = 21,res=0;
    cin >> x;
    if( x <= 5)
    {
        res = 1;
    }else
    {
        res = (x/5)+1;
    }
    cout << res << endl;
    return 0;
}

发表于 2019-09-20 16:39:13 回复(0)

1.若小于5就为1
2.若大于5 看他有没有余数
有余数就x/5+1
没有余数就x/5

import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int A1=sc.nextInt();
        int s;
        if(A1<=5){
            s=1;
        }else{
            if(A1%5>0){
                s=(A1/5)+1;
            }else{
                s=(A1/5);
            }
        }
        System.out.println(s);
    }
}

别人更精简的答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int m = x/5;
        if (x%5 != 0)
            m++;
        System.out.println(m);
    }
}
编辑于 2019-09-19 21:23:56 回复(0)
import java.util.Scanner;
public class Main{
    private static final int MAX_VALUE_OF_X=1000000;
    /**
    parameter x 小易朋友的位置
    return 小易走的次数,-1表示x超出范围
    **/
    public int walk(int x){
        if(x<1 || x>MAX_VALUE_OF_X){
            System.out.println("请输入正确小易朋友家的位置");
            return -1;
        }
        int tmp_distance=x;
        int next_step=5;
        int step_count=0;
        while(tmp_distance!=0){
            step_count+=tmp_distance/next_step;
            tmp_distance=tmp_distance%next_step;
            next_step--;
        }
        return step_count;
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        Main m=new Main();
        int steps=m.walk(x);
        System.out.println(steps);
    }
}
发表于 2019-09-19 14:59:07 回复(0)
remaining_steps = int(input())
res = 0
for step_unit in reversed(range(1, 6)):  # 迭代对应 5 4 3 2 1
    res += remaining_steps // step_unit  # 累加由商代表的所走的步数
    remaining_steps %= step_unit  # 将剩余的步数更新为余
    if remaining_steps == 0:
        break
print(res)

发表于 2019-09-09 17:33:13 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    cout<<int(ceil(n/5.0))<<endl;

    return 0;
}

发表于 2019-08-25 00:57:18 回复(0)
package main

import (
    "fmt"
)

func main() {
    var x int
    fmt.Scan(&x)
    var ans int
    if x%5==0{
        ans=x/5
    }else{
        ans=x/5+1
    }
    fmt.Print(ans)
}

发表于 2023-03-12 17:24:59 回复(0)
def func(p):
    if p==0:
        return 0
    elif p<=5:
        return 1
    else:
        return p//5+func(p%5)
x=int(input())
print(func(x))

发表于 2022-02-18 15:30:55 回复(0)