小易准备去拜访他的朋友,他的家在0点,但是他的朋友的家在x点(x > 0),均在一条坐标轴上。小易每一次可以向前走1,2,3,4或者5步。问小易最少走多少次可以到达他的朋友的家。
import math print(math.ceil(int(input())/5))
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);
}
}
} #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;
}
//可以理解为动态规划的题
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]);
}
} 一看到跳台阶,就想用动态😂// 第一反应,想到青蛙跳台阶的问题了, 一看见大家的答案,我发现又被自己绣了
#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;
}
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);
}
}
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
let x = parseInt(line.trim());
// console.log(x);
let count = 0;
while (x > 0) {
if (x >= 5) {
x -= 5;
count++;
} else if (x >= 4 && x < 5) {
x -= 4;
count++;
} else if (x >= 3 && x < 4) {
x -= 3;
count++;
} else if (x >= 2 && x < 3) {
x -= 2;
count++;
} else {
x -= 1;
count++;
}
}
console.log(count)
});