ACM模式学习与题目

ACM模式

什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。

注意类名必须为Main, 不要有任何package xxx信息

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in); 
        while (in.hasNextInt()) { //注意while处理多个case  
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
} 

注意hasNext和hasNextLine的区别:
采用java语言的话,有的同学这样处理输入输出 :

Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
    int n = in.nextInt();
    //处理代码略
}

牛客网的系统会提示错误,但是如果把hasNextLine改成hasNext或者hasNextInt的话,就会提示正确 :

while (in.hasNextInt()) {
    int n = in.nextInt();
    //处理代码略
}

hasNextLine和hasNext的区别。

  • 采用has xxxx的话,后面也要用next xxxx。比如前面用hasNextLine,那么后面要用 nextLine 来处理输入。

  • hasNext是检查是否有非空字符。判断接下来是否有非空字符.如果有,则返回true,否则返回false。

  • hasNextLine是检查输入中是否还有linePattern。其中LinePattern其实是匹配一个正则表达式。 根据行匹配模式去判断接下来是否有一行(包括空行),如果有,则返回true,否则返回false。

题目

A. 多组空格分隔的两个正整数

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。

输出a+b的结果

1 5
10 20
6
30
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}
B.第一行组数接空格分隔的两个正整数

输入第一行包括一个数据组数t(1 <= t <= 100)

接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)

输出a+b的结果

2
1 5
10 20
6
30
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}
C.空格分隔的两个正整数为0 0 结束

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

输出a+b的结果

1 5
10 20
0 0
6
30
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            if(a ==0 && b == 0) break;
            System.out.println(a + b);
        }
    }
}
D.每行第一个为个数后带空格分割整数为0结束

输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

每组数据输出求和的结果

4 1 2 3 4
5 1 2 3 4 5
0
10
15
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}
E.第一行组数接第一个个数接空格分开的整数

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

每组数据输出求和的结果

2
4 1 2 3 4
5 1 2 3 4 5
10
15
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            while (n > 0) {
                sum += in.nextInt();
                n--;
            }
            System.out.println(sum);
        }
    }
}
F.每行第一个为个数后带空格分割整数

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

每组数据输出求和的结果

4 1 2 3 4
5 1 2 3 4 5
10
15
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int sum = 0;
            while (n > 0) {
                sum += in.nextInt();
                n--;
            }
            System.out.println(sum);
        }
    }
}
G.多组空格分隔的正整数

输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。

每组数据输出求和的结果

1 2 3
4 5
0 0 0 0 0
6
9
0
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            int sum = 0;
            for (int i = 0; i < s.length; i++) {
                sum += Integer.parseInt(s[i]);
            }
            System.out.println(sum);
        }
    }
}
H.第一行个数第二行字符串

输入有两行,第一行n

第二行是n个空格隔开的字符串

输出一行排序后的字符串,空格隔开,无结尾空格

5
c d a bb e
a bb c d e
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            Arrays.sort(s);
            for (int i = 0; i < s.length; i++) {
                System.out.print(s[i] + " ");
            }
            
        }
    }
}
I.多行空格分开的字符串

多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

a c bb
f dddd
nowcoder
a bb c
dddd f
nowcoder
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            Arrays.sort(s);
            for (String c:s) {
                System.out.print(c + " ");
            }
            System.out.println();
        }
    }
}
J.多行逗号分开的字符串

多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100

对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格

a,c,bb
f,dddd
nowcoder
a,bb,c
dddd,f
nowcoder
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(",");
            Arrays.sort(s);
            int l = s.length;
            for (int i = 0; i < l - 1; i++) {
                System.out.print(s[i] + ",");
            }
            System.out.println(s[l-1]);
        }
    }
}
K.多组空格分隔的两个正整数

输入有多组测试用例,每组空格隔开两个整数

对于每组数据输出一行两个整数的和

1 1
2
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLong()) { // 注意 while 处理多个 case
            Long a = in.nextLong();
            Long b = in.nextLong();
            System.out.println(a + b);
        }
    }
}

Scanner 类是获取键盘输入的一个类,首先先创建 Scanner 对象

Scanner sc = new Scanner(System.in);

接下来通过Scanner 类的方法来获取输入,在调用方法之前一般可以采取has…方法判断是否有输入。

next 和 nextLine 都是获取输入字符串的方法

next( )方法nextLine( )方法
只能读取到空格之前的字符串可以读取空格的字符串
比如“你好 java”,只能读取“你好”比如“你好 java”,可以读取“你好 java”

在读取前可以使用 hasNext 与 hasNextLine 判断是否有输入的数据

if ( sc.hasNext()) { String str1=sc.next(); }
if ( sc.hasNextLine()) { String str2=sc.nextLine(); }

还可以接受整数和小数

i = scan.nextInt(); // 接收整数
f = scan.nextFloat(); // 接收小数

Integer.parseInt和Integer.valueOf的区别

parseInt( )//返回的是基本类型int
valueOf( )//返回的是包装类Integer
  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1 图论 3 1.1 术语 3 1.2 独立集、覆盖集、支配集之间关系 3 1.3 DFS 4 1.3.1 割顶 6 1.3.2 桥 7 1.3.3 强连通分量 7 1.4 最小点基 7 1.5 拓扑排序 7 1.6 欧拉路 8 1.7 哈密顿路(正确?) 9 1.8 Bellman-ford 9 1.9 差分约束系统(用bellman-ford解) 10 1.10 dag最短路径 10 1.11 二分图匹配 11 1.11.1 匈牙利算法 11 1.11.2 KM算法 12 1.12 网络流 15 1.12.1 最大流 15 1.12.2 上下界的网络的最大流 17 1.12.3 上下界的网络的最小流 17 1.12.4 最小费用最大流 18 1.12.5 上下界的网络的最小费用最小流 21 2 数论 21 2.1 最大公约数gcd 21 2.2 最小公倍数lcm 22 2.3 快速幂取模B^LmodP(O(logb)) 22 2.4 Fermat小定理 22 2.5 Rabin-Miller伪素数测试 22 2.6 Pollard-rho 22 2.7 扩展欧几里德算法extended-gcd 24 2.8 欧拉定理 24 2.9 线性同余方程ax≡b(mod n) 24 2.10 中国剩余定理 25 2.11 Discrete Logging(BL == N (mod P)) 26 2.12 N!最后一个不为0的数字 27 2.13 2^14以内的素数 27 3 数据结构 31 3.1 堆(最小堆) 31 3.1.1 删除最小值元素: 31 3.1.2 插入元素和向上调整: 32 3.1.3 堆的建立 32 3.2 并查集 32 3.3 树状数组 33 3.3.1 LOWBIT 33 3.3.2 修改a[p] 33 3.3.3 前缀和A[1]+…+A[p] 34 3.3.4 一个二维树状数组的程序 34 3.4 线段树 35 3.5 字符串 38 3.5.1 字符串哈希 38 3.5.2 KMP算法 40 4 计算几何 41 4.1 直线交点 41 4.2 判断线段相交 41 4.3 三点外接圆圆心 42 4.4 判断点在多边形内 43 4.5 两圆交面积 43 4.6 最小包围圆 44 4.7 经纬度坐标 46 4.8 凸包 46 5 Problem 48 5.1 RMQ-LCA 48 5.1.1 Range Minimum Query(RMQ) 49 5.1.2 Lowest Common Ancestor (LCA) 53 5.1.3 Reduction from LCA to RMQ 56 5.1.4 From RMQ to LCA 57 5.1.5 An algorithm for the restricted RMQ 60 5.1.6 An AC programme 61 5.2 最长公共子序列LCS 64 5.3 最长上升子序列/最长不下降子序列(LIS) 65 5.3.1 O(n^2) 65 5.3.2 O(nlogn) 66 5.4 Joseph问题 67 5.5 0/1背包问题 68 6 组合数学相关 69 6.1 The Number of the Same BST 69 6.2 排列生成 71 6.3 逆序 72 6.3.1 归并排序求逆序 72 7 数值分析 72 7.1 二分法 72 7.2 迭代法(x=f(x)) 73 7.3 牛顿迭代 74 7.4 数值积分 74 7.5 高斯消元 75 8 其它 77

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值