数据分页,对于指定的页数和每页的元素个数,返回该页应该显示的数据。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int total = scanner.nextInt(); int[] irr = new int[total]; for (int i = 0; i < total; i++) { irr[i] = scanner.nextInt(); } int p = scanner.nextInt(); int size = scanner.nextInt(); int page = (total - 1) / size + 1; if (p > page -1) System.out.println("超过分页范围"); else { int start = p * size, end = start + size > total ? total : start + size; for (int i = start; i < end; i++) System.out.println(irr[i]); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n+5]; for (int i = 0; i!=n; i++) { nums[i] = sc.nextInt(); } int pageNumber = sc.nextInt(); int pageCapacity = sc.nextInt(); if ((n + pageCapacity - 1) / pageCapacity < pageNumber) { System.out.println("超过分页范围"); return; } StringBuilder sb = new StringBuilder(); for (int i=pageNumber*pageCapacity; i < Math.min(pageCapacity*(pageNumber+1), n); i++) { sb.append(nums[i]); sb.append("\n"); } System.out.print(sb.toString()); return; } }
// 数据分页说明.cpp
//-_-||题目半天没看懂
//后面发现就是类似于将数组存入内存中,内存分页大小固定,让输出给定页号的内容。
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin >> n) {
int *arr = new int[n];
for (int i = 0; i < n; ++i)
cin >> arr[i];
int page, num;
cin >> page >> num;
if (page > n / num - 1) {
cout << "超过分页范围" << endl;
continue;
}
for (int i = page * num; i < page * num + num; ++i)
cout << arr[i] << endl;
delete[]arr;
}
return 0;
}
while True: try: n=int(input().strip()) inp=list(map(int,input().strip().split(' '))) ye=int(input().strip()) num=int(input().strip()) num_ye=n//num if n%num!=0 and num_ye==ye: print('超过分页范围') else: for i in range(num_ye+1): if i>ye: #print(i) break else: re=inp[0:num] #print(re) inp=inp[num:] for i in re: print(i) except: break
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arrs=new int[n]; for(int i=0;i<n;i++){ arrs[i]=sc.nextInt(); } int target=sc.nextInt(); int count=sc.nextInt(); getResult(n,arrs,target,count); } private static void getResult(int n,int[] arrs,int target,int count){ if((target+1)*count>n){ System.out.println("超出分页范围"); return; } for(int i=0;i<count;i++){ if((target*count+i)<n){ System.out.println(arrs[target*count+i]); } } } }
注意页码从0,开始 #include <iostream> #include <vector> using namespace std; int main() { int sum,pn,num,mid; vector <int> da; cin>>sum; for(int i=0;i<sum;i++) { cin>>mid; da.push_back(mid); } cin>>pn>>num; if(pn*num+num>sum) { cout<<"超过分页范围"<<endl; } else { for(int i=pn*num;i<pn*num+num;i++) { cout<<da[i]<<endl; } } return 0; }
#include <iostream> #include <vector> using namespace std; int main(void) { int inum, pagenum, pagemax, itemp; cin >> inum; vector<int> data(inum); for (int i = 0; i < inum; i++) { cin >> itemp; data[i] = itemp; } cin >> pagenum >> pagemax; if ((pagenum+1)*pagemax > data.size()) cout << "超过分页范围" << endl; else for (int i = 0, j = pagenum*pagemax; i < pagemax; i++,j++) cout << data[j] << endl; return 0; }
#include<stdio.h> #include<stdlib.h> int main() { int i, n, page, page_num; int *data = NULL; scanf("%d",&n); data = (int *)malloc(n*sizeof(int)); for(i=0; i<n; i++) { scanf("%d", &data[i]); } scanf("%d%d",&page,&page_num); if((page+1) * page_num > n) { printf("超过分页范围\n"); } else { for(i=page*page_num; i<(page+1)*page_num&&i<n; i++) { printf("%d\n",data[i]); } } return 0; }
#include<iostream> #include<vector> using namespace std; int main(){ int n; while(cin>>n){ vector<int>data(n); for(int i=0;i<n;i++){ cin>>data[i]; } int page,num; cin>>page>>num; for(int i=page*num;i<(page+1)*num;i++){ cout<<data[i]<<endl; } } return 0; }//这题目表述***
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author wylu */ 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()); String[] datas = br.readLine().split(" "); int curPage = Integer.parseInt(br.readLine()); int pageSize = Integer.parseInt(br.readLine()); if (curPage > n / pageSize - 1) { System.out.println("超过分页范围"); } else { for (int i = curPage * pageSize; i < Math.min(n, (curPage + 1) * pageSize); i++) { System.out.println(datas[i]); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { if(sc.hasNext()) { list.add(sc.nextInt()); } } int pageNo = sc.nextInt(); int pageSize = sc.nextInt(); sc.close(); int pages = (n - 1) / pageSize + 1; if(pageNo < 0 || pageNo > pages) { System.out.println("超过分页范围"); } else { int s = pageNo * pageSize; for(int i = s; i < s + pageSize; i++) { System.out.println(list.get(i)); } } } }这题有点坑,页码竟然是从0开始算
import java.util.Scanner;/*** 没什么难点,有个细节就是在尾页的时候,输出不一定要等于每页的元素个数。* @author moci* @create 2018-05-22 16:30**/public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int num = sc.nextInt();int[] data = new int[num];for (int i = 0; i < num; i++) {data[i] = sc.nextInt();}int page = sc.nextInt();int pageNumber = sc.nextInt();sc.close();if(num<=0||num<page*pageNumber) System.out.println("超出范围");else {run(data,num,page,pageNumber);}}static void run (int[] data,int num,int page,int pageNumber){int root = page*pageNumber;for (int i = 0; i < pageNumber; i++) {System.out.println(data[root++]);if(root>=num) return;}}