假设南京鼓楼区的移动电话基站为如下操作:把该区域分成一个正方形,形成一个 S * S 的 矩阵,行和列的编号为从 0 到 S-1。正方形的每个块为一个基站。 基站内的活跃手机数量 可能会发生变化,因为手机会从一个块移动到另一个块,或者开机和关机。 在这时,每个 基站会向主基站报告活跃手机数量的变化以及矩阵变化位置的行和列。 编写一个程序,接收这些报告,并回答有关任何矩阵中当前活跃手机总数的查询。
输入: 每一次输入会在单独的一行上,包含一个整数指令和多个整数参数。
输入值可以考虑为始终在范围内,对于 A 是负数而言,可以保证它不会把矩阵值减小到负 数。索引查询从 0 开始,例如,对于大小为 4*4 的表,我们有 0<=X<=3 且 0<=Y<=3。
表的大小:1 * 1 <= S * S <= 1024 * 1024
每个基站中的值 V(任何时间):0 <= V <= 32767
更新的活跃手机数量:-32768 <= A <= 32767
输入的指令数:3 <= U <= 60002
整个表中的最大电话数:M = 2 ^ 30
输出:
您的程序不应该对除 2 之外的指令的行返回任何内容。如果指令是 2,那么您的程序应该通 过将答案写为包含单个整数的标准输出。
样例输入:
0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3
样例输出: 3 4
分析:主要仔细看输入输出的描述。其中根据输入,知道入参,包含四中指令,0 开始创建矩阵,二维数组来实现,记录个位置手机数量。3指令,表示结束。1指令对指定位置上的手机数量进行改变,整数为增加,负数为减少,第一次为该位置上的手机数量。2指令输出部分区域的手机数量。
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MobilePhoneTest1 {
String sep;
MobilePhone mp;
PrintStream console = null;
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
InputStream input = null;
@Before
public void setUp() throws Exception {
mp = new MobilePhone();
out = new ByteArrayOutputStream();
input = System.in;
console = System.out;
System.setOut(new PrintStream(out));
sep = System.getProperty("line.separator");
}
@After
public void tearDown() throws Exception {
System.setIn(input);
out.close();
System.setOut(console);
}
@Test(timeout=4000)
public void test1() {
in = new ByteArrayInputStream(("0 4" + sep + "1 1 2 3" + sep +"2 0 0 2 2" + sep + "1 1 1 2" + sep + "1 1 2 -1" + sep + "2 1 1 2 3" + sep +"3").getBytes());
System.setIn(in);
mp.entrance();
String ans = out.toString();
assertEquals("3" + sep + "4"+sep, ans);
}
@Test(timeout=4000)
public void test2() {
in = new ByteArrayInputStream(("0 9" + sep + "1 2 5 55" + sep +"2 0 3 5 7" + sep + "1 3 3 77" + sep + "2 0 0 6 5" + sep +"3").getBytes());
System.setIn(in);
mp.entrance();
String ans = out.toString();
assertEquals("55" + sep + "132"+sep, ans);
}
@Test(timeout=4000)
public void test3() {
in = new ByteArrayInputStream(("0 110" + sep + "1 77 66 233" + sep +"2 0 0 0 0" + sep + "1 11 22 112" + sep + "1 11 22 112" + sep + "1 3 5 -17" + sep + "2 21 34 5 9" + sep +"3").getBytes());
System.setIn(in);
mp.entrance();
String ans = out.toString();
assertEquals("0" + sep + "224"+sep, ans);
}
@Test(timeout=4000)
public void test4() {
in = new ByteArrayInputStream(("0 255" + sep + "1 128 64 196" + sep +"2 167 173 47 53" + sep + "2 133 247 150 232" + sep + "2 152 167 201 37" + sep +"3").getBytes());
System.setIn(in);
mp.entrance();
String ans = out.toString();
assertEquals("196" + sep + "0" + sep+ "0" + sep, ans);
}
@Test(timeout=4000)
public void test5() {
in = new ByteArrayInputStream(("0 777" + sep + "1 7 7 77" + sep +"2 7 7 7 7" + sep + "1 6 6 -66" + sep + "1 111 111 111" + sep + "2 17 27 37 47" + sep + "2 57 67 77 87" + sep + "2 97 107 127 137" + sep +"3").getBytes());
System.setIn(in);
mp.entrance();
String ans = out.toString();
assertEquals("77" + sep + "0" + sep + "0" + sep + "111" + sep, ans);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MobilePhone {
static int [][] juzheng =null;
public static void entrance() {
String input = getSystemIn();
String [] instructions = input.split("/");
for(int i=0;i<instructions.length;i++){
String instruction = instructions[i];
String [] content = instruction.split(" ");
String zhiling = "";//0,1,2,3
if(content!=null){
zhiling = content[0];
if("0".equals(zhiling) && i==0){
int s = Integer.parseInt(content[1]);//矩阵大小
juzheng=new int[s][s];
}else if("1".equals(zhiling)){
int x = Integer.parseInt(content[1]);
int y = Integer.parseInt(content[2]);
int num = Integer.parseInt(content[3]);
if(juzheng[x][y]==0){
juzheng[x][y]=num;
}else{
juzheng[x][y]=juzheng[x][y]+num;
}
}else if("2".equals(zhiling)){
int l= Integer.parseInt(content[1]);
int b= Integer.parseInt(content[2]);
int r= Integer.parseInt(content[3]);
int t= Integer.parseInt(content[4]);
int sum=0;
int L,B,R,T;
if(l<r){
L =l;
R =r;
}else {
L =r;
R =l;
}
if(b<t){
B =b;
T =t;
}else {
B =t;
T =b;
}
for (int m=L;m<R+1;m++){
for (int n=B;n<T+1;n++){
sum=sum+juzheng[m][n];
}
}
System.out.println(sum);
}else if("3".equals(zhiling)){
return;
}
}
}
}
/**
* 将输入流转换为字符串
* @return
*/
private static String getSystemIn(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
String res = "" ;
try {
int b;
while(( str=br.readLine()) != null){
res=res+"/"+str;
}
} catch (IOException e) {
e.printStackTrace();
}
return res.substring(1,res.length());
}
}