【牛客网】oj输入输出练习
a+b,多组
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> arrays =new ArrayList<Integer>();
while(scanner.hasNextLine()){
String[] lines = scanner.nextLine().split(" ");
int a =Integer.parseInt(lines[0]);
int b = Integer.parseInt(lines[1]);
System.out.println(a+b);
arrays.add(a+b);
}
for( Integer i:arrays){
System.out.println(i);
}
}
}
字符串多行排序输出
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String[] lines = scanner.nextLine().split(" ");
Arrays.sort(lines);
for (int i = 0; i < lines.length-1; i++){
String res = lines[i] + " ";
System.out.print(res);
}
System.out.println(lines[lines.length-1]);
}
}
}
字符串排序
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int count = Integer.parseInt(scanner.nextLine());
String[] lines = scanner.nextLine().split(" ");
Arrays.sort(lines);
for (int i = 0; i < count-1; i++){
String res = lines[i] + " ";
System.out.print(res);
}
System.out.print(lines[count-1]);
}
}
a+b,增加换行
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> arrays =new ArrayList<Integer>();
while(scanner.hasNextLine()){
String[] lines = scanner.nextLine().split(" ");
if(lines.length == 2){
int a =Integer.parseInt(lines[0]);
int b = Integer.parseInt(lines[1]);
arrays.add(a+b);
}
}
for( Integer i:arrays){
System.out.println(i);
}
}
}
a+b,输入0结束
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> arrays =new ArrayList<Integer>();
while(scanner.hasNextLine()){
String[] lines = scanner.nextLine().split(" ");
int a =Integer.parseInt(lines[0]);
int b = Integer.parseInt(lines[1]);
if((a == 0)|| (b == 0)){
break;
}
arrays.add(a+b);
}
for( Integer i:arrays){
System.out.println(i);
}
}
}
a+b,每组数字输出求和
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
ArrayList<Integer> list =new ArrayList();
while(sca.hasNextLine()){
String[] temp = sca.nextLine().split(" ");
if (temp[0].equals("0"))
break;
int a =0;
for(int i =1;i<temp.length;i++){
a += Integer.parseInt(temp[i]);
}
list.add(a);
}
for(Integer i:list){
System.out.println(i);
}
}
}
a+b,每组数字输出求和,且取值范围很大,会数组越界
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String[] lines = scanner.nextLine().split(" ");
Long a =Long.parseLong(lines[0]);
Long b = Long.parseLong(lines[1]);
System.out.println(a+b);
}
}
}
oj输入输出练习部分的记录就先到这!接下来,是一些整理的c++要点概要!
额外的补充
fork()系统调用的特性
fork()系统调用是 Unix 下以自身进程创建子进程的系统调用, 一次调用, 两次返回, 如果返
回是 0, 则是子进程, 如果返回值>0, 则是父进程(返回值是子进程的 pid) 。 还有一个很
重要的东西是, 在 fork()的调用处, 整个父进程空间会原模原样地复制到子进程中, 包括指
令, 变量值, 程序调用栈, 环境变量, 缓冲区, 等等。
指针与const
- const A * 等价 A const * 。
- 允许用 A * 赋值 A const * 。
- 允许用 A* * 赋值 A const* const * 。
- 不允许用 A* * 赋值 A const* * 。
友元关系
友元关系是单向的, 不是对称, 不能传递。
- 关于传递性, 有人比喻: 父亲的朋友不一定是儿子的朋友。
- 那关于对称性, 是不是: 他把她当朋友, 她却不把他当朋友? 如, 类 A 是类B 的友元, 类 C 是类 A 的公有派生类。 则类 C 不是类 B 的友元, 类 B 不是类 A 的友元
复杂度
- vector 插入 ,该位置插入后后面的都要改变 O(n)
- Set 底层红黑树 O(logn)
- Hash_map 底层哈希表 O(1)
- Deque 尾部可以直接修改 O(1)
抽象类的规定
- 抽象类只能用作其他类的基类, 不能建立抽象类对象。
- 抽象类不能用作参数类型、 函数返回类型或显式转换的类型。
- 可以定义指向抽象类的指针和引用, 此指针可以指向它的派生类, 进而实现多态性。
常成员函数
const void print(const int num)const
- 第一个 const 修饰返回类型
- 第二个 const 修饰参数
- 第三个 const 修饰类的成员函数
常成员函数声明: 形如 void funcName(参数表) const;
对于常成员函数需要注意:
- 其实现部分也要带 const 关键字;
- 不能修改类的成员变量, 不能调用类中没有被 const 修饰的成员函数(即只能调用常成员函数) 。