现在对于每个块进行如下操作:
> 翻转某个块中的牌,并且与之相邻的其余八张牌也会被翻转。
XXX
XXX
XXX
如上矩阵所示,翻转中间那块时,这九块中的牌都会被翻转一次。
请输出在对矩阵中每一块进行如上操作以后,牌面向下的块的个数。
请输出在对矩阵中每一块进行如上操作以后,牌面向下的块的个数。
数据范围: ![](https://www.nowcoder.com/equation?tex=1%20%5Cle%20t%20%5Cle%2010%5E5%2C%200%20%5Cle%20n%2Cm%20%5Cle%2010%5E9%5C)
输入的第一行为测试用例数t(1 <= t <= 100000),
接下来t行,每行包含两个整数N,M(0 <= N, M <= 1,000,000,000)
对于每个用例输出包含一行,输出牌面向下的块的个数
5 1 1 1 2 3 1 4 1 2 2
1 0 1 2 0
又因为他说N,M(1 <= N, M <= 1,000,000,000)
所有要用long类型
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int a=sc.nextInt(); for(int i=1;i<=a;i++){ long A1=sc.nextLong(); long A2=sc.nextLong(); long s; if(A1==1&&A2==1){ s=1; }else if(A1==1){ s=A2-2; }else if(A2==1){ s=A1-2; }else{ s=(A1-2)*(A2-2); } System.out.println(s); } } }
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)); int t = Integer.parseInt(br.readLine().trim()); while(t-- > 0){ String[] temp = br.readLine().trim().split(" "); long n = Long.parseLong(temp[0]), m = Long.parseLong(temp[1]); /* 注意:翻转偶数次就等于没翻转 中间的牌(非边缘和角落)在自己为中心或周围8张牌为中心时会发生翻转,一共9次,翻转后是向下的。 而边缘和角落的牌分别会翻转6次和4次,因此翻转后还是向上的。 */ if(n == 1 && m == 1){ System.out.println(1); }else if(n == 1){ System.out.println(m - 2); }else if(m == 1){ System.out.println(n - 2); }else{ System.out.println((n - 2)*(m - 2)); } } } }
如果N == 1, M == 1,则只翻转一次,输出0;
如果N == 1, M > 1, 则首尾被翻转2次,其它 M - 2 个被翻转三次,输出 M - 2;
如果N > 1, M > 1, 则矩阵的四个角被翻转4次,四条边上除角以外的部分被翻转6次,不在边上的部分会被翻转9次,所以输出 (N - 2) * (M - 2)。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { long N = sc.nextLong(); long M = sc.nextLong(); if (N > M) { long temp = N; N = M; M = temp; } if (N == 1 && M == 1) { System.out.println(1); } else if (N == 1 && M > 1) { System.out.println(M - 2); } else { System.out.println((N - 2) * (M - 2)); } } } }
#include<iostream> using namespace std; int main(){ int t,n,m; cin>>t; while(t--){ cin>>n>>m; if(n==1 and m==1){cout<<1<<endl;} if(n==1 and m>1){cout<<m-2<<endl;} if(n>1 and m==1){cout<<n-2<<endl;} if(n>1 and m>1){cout<<(n-2)*(m-2)<<endl;} } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t>0){ long n = in.nextLong(); long m = in.nextLong(); long ans = Math.abs((n-2)*(m-2)); System.out.println(ans); t--; } } }代码哪里错了,都改成long了,后面用JAVA的BigInteger做照样只有90%的例子,醉了。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @Author: coderjjp * @Date: 2020-05-08 16:39 * @Description: * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.valueOf(br.readLine()); while (t-- > 0){ String[] s = br.readLine().split(" "); long N = Integer.valueOf(s[0]); long M = Integer.valueOf(s[1]); if (N == 1 && M == 1) System.out.println(1); else if (N == 1) System.out.println(M - 2); else if (M == 1) System.out.println(N - 2); else System.out.println((M - 2) * (N - 2)); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); long[][] matrix = new long[t][2]; for (int i = 0; i < t; i++) { matrix[i][0] = sc.nextLong(); matrix[i][1] = sc.nextLong(); } for (int i = 0; i < t; i++) { long N = matrix[i][0]; long M = matrix[i][1]; if (N == 1 && M == 1) { System.out.println(1); } else if (N == 1) { System.out.println(M - 2); } else if (M == 1) { System.out.println(N - 2); } else if (N == 2 || M == 2) { System.out.println(0); } else { System.out.println((M - 2) * (N - 2)); } } } }
def cross(N,M): if N == 1 and M == 1: return 1 elif N == 1 and M != 1: return M - 2 elif N != 1 and M == 1: return N - 2 else: return (N - 2)*(M - 2) t = int(input()) for i in range(0,t): N,M = map(int,input().split()) print(cross(N,M))python代码,我看算法和各位的是一样的,但为什么通过率只有90%?问题出在哪里?
import sys if __name__ == "__main__": def solution(m, n): print(abs((int(m)-2)*(int(n)-2)) t = sys.stdin.readline().strip() ans=[] for i in range(int(t)): matrix = list(map(str, sys.stdin.readline().strip().split())) ans.append(solution(matrix[0], matrix[1])) for a in ans: print int(a)
#include<iostream> usingnamespacestd; intmain() { intnum; cin>>num; while(num--) { intn, m, array[1000][1000] = {0}, count = 0; cin>>n>>m; for(inti = 1; i < n+1; ++i) //外边加一圈 { for(intj = 1; j < m+1; ++j) { array[i-1][j-1] += 1; array[i-1][j] += 1; array[i-1][j+1] += 1; array[i][j-1] += 1; array[i][j] += 1; array[i][j+1] += 1; array[i+1][j-1] += 1; array[i+1][j] += 1; array[i+1][j+1] += 1; } } for(inti = 1; i < n+1; ++i) { for(intj = 1; j < m+1; ++j) { if(array[i][j] % 2) count++; } } cout<<count<<endl; } return0; } |