首页 > 试题广场 >

翻转翻转

[编程题]翻转翻转
  • 热度指数:5424 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个N*M的矩阵,在矩阵中每一块有一张牌,我们假定刚开始的时候所有牌的牌面向上。
现在对于每个块进行如下操作:
> 翻转某个块中的牌,并且与之相邻的其余八张牌也会被翻转。
XXX
XXX
XXX
如上矩阵所示,翻转中间那块时,这九块中的牌都会被翻转一次。
请输出在对矩阵中每一块进行如上操作以后,牌面向下的块的个数。

数据范围:

输入描述:
输入的第一行为测试用例数t(1 <= t <= 100000),
接下来t行,每行包含两个整数N,M(0 <= N, M <= 1,000,000,000)


输出描述:
对于每个用例输出包含一行,输出牌面向下的块的个数
示例1

输入

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);
        }

    }
}
发表于 2019-09-19 23:39:56 回复(2)
#include <bits/stdc++.h>
using namespace std;

int main(){
    long long t,n,m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        if(n==1 && m==1)
            cout<<1<<endl;
        else if(n==1 && m>1)
            cout<<m-2<<endl;
        else if(m==1 && n>2)   
            cout<<n-2<<endl;
        else
            cout<<(n-2)*(m-2)<<endl;
    }
    return 0;
}

发表于 2019-08-25 01:16:57 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        n-=2;
        m-=2;
        if(n<m)
            swap(n,m);
        if(n<0)
        {
            cout<<1<<endl;
            continue;
        }
        if(m==-1)
            cout<<n<<endl;
        else 
            cout<<n*m<<endl;
    }
    return 0;
}

发表于 2019-07-11 20:39:36 回复(0)
(1) 对于四个角的牌,会经过自己和相邻的三张牌翻转而进行翻转,一共4次。
(2) 对于非四个角且在边缘的牌,会经过自己和相邻的五张牌而进行翻转,一共6次。
(3) 对于非以上两种情况的牌,会经过自己和相邻的八张牌而进行翻转,一共9次。
而翻转偶数次的牌仍然是向上的,只有翻转奇数次,才能保证牌面向下,所以只需要计算第三种情况有多少张牌,即(n-2)*(m-2)
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));
            }
        }
    }
}


发表于 2021-01-30 18:33:17 回复(0)
import sys
imp = sys.stdin.readlines()
n = int(imp[0])
for i in range(n):
    x,y = map(int, imp[i+1].split())
    ans = -(x-2)*(y-2) if (x==1) ^ (y==1) else (x-2)*(y-2)
    print(ans)
用异或替代abs函数一行搞定
发表于 2020-08-13 13:00:46 回复(0)

Java

分三种情况讨论

如果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));
            }
        }
    }
}
发表于 2019-07-06 11:39:30 回复(6)
分四种情况 
n==1,m==1 翻转1次 改变为1
n==1,m>1 角翻转2次 中间翻转3次 改变为 m-2
n>1,m==1 角翻转2次 中间翻转3次 改变为 m-2
n>1,m>1 角翻转4次 第1行和第n行出去两角后翻转6次,同理第1列和第m列翻转6次,在第2行到第n-1行之间 ,第2列到第m-1列之间,翻转9次
                改变为 (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;
}


发表于 2019-08-27 21:22:34 回复(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%的例子,醉了。
发表于 2020-08-08 14:01:43 回复(1)
t = int(input())
for _ in range(t):
    N, M = map(int, input().split())
    N, M = min(N, M), max(N, M)
    if N == 1 and M == 1:
        print(1)
    elif N == 1 and M > 1:
        print(M - 2)
    else:
        print((N - 2)*(M - 2))

发表于 2020-08-06 14:10:59 回复(0)
找规律的题目,应该不是编程题。
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));
        }
    }
}


发表于 2020-05-08 17:25:16 回复(0)
javascript涉及大数相乘
var lines = readline();
for(var i=0;i<lines;i++){
    var arr = readline().split(" ")
    var res = 0;
    var m = arr[0];
    var n = arr[1];
    if(m<n){
        var flag = n;
        n = m;
        m =flag;
    }
    if(m==1&&n==1){
        res = 1;
    }else if(n==1){
        res = m-2;
    }else{
        m-=2;
        n-=2;
        if((m+"").length+(n+"").length>=16){
            var arr1 = (m+"").split("");
            var arr2 = (n+"").split("")
            var arr3 = [];
            for(var j=arr1.length-1;j>=0;j--){
                for(var q =arr2.length-1;q>=0;q--){
                    var num = arr1[j]*arr2[q];
                    num = arr3[q+j+1]?arr3[q+j+1]+num:num;
                    arr3[q+j+1]=num%10;
                    arr3[q+j]=arr3[q+j]?Math.floor((arr3[q+j]*10+num)/10):Math.floor(num/10);
                    
                }
            }
            res = arr3[0]?arr3.join(""):arr3.slice(1).join("");
            
        }else{
            res = m*n
        }
    }
    console.log(res)
}
发表于 2020-04-07 10:26:15 回复(1)
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));
            }
        }
    }
}

编辑于 2020-04-04 18:49:06 回复(0)
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%?问题出在哪里?
发表于 2019-09-23 17:46:24 回复(1)
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)
报错:

如果使用c/c++要保证int main函数最终 return 0;如果使用其余语言请检查代码\&quot;是否有数组越界等异常\&quot;或者\&quot;是否有语法错误\&quot;
File "a.py3", line 7
t = sys.stdin.readline().strip()
^
SyntaxError: invalid syntax
你的输出为:  File "a.py3", line 7
t = sys.stdin.readline().strip()
^
SyntaxError: invalid syntax

求解答?
编辑于 2019-09-20 14:47:08 回复(0)
import java.util.Scanner;
  
public class Main {
      
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; ++i) {
            long m = scanner.nextInt();
            long n = scanner.nextInt();
            if (m >= 2) m -= 2;
            if (n >= 2) n -= 2;
            System.out.println(m * n);
        }
    }
}
//没通过,哪里错了😖
发表于 2019-09-17 21:53:47 回复(0)
#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;
}
求大佬指点,出现段错误的原因,测试通过10%,但自我感觉良好,是在找不出来毛病了。
发表于 2019-08-29 20:22:55 回复(4)
为什么现在提交只过90%?
发表于 2019-08-17 20:56:19 回复(2)
这是什么垃圾题,看了半天题意不知道想干啥,是我语文不好还是。。。。
发表于 2019-08-13 01:28:15 回复(4)
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long t,n,m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        if(n==1&&m==1)
            cout<<1<<endl;
        else if(n==1&&m>1)
            cout<<m-2<<endl;
        else if(n>1&&m==1)
            cout<<n-2<<endl;
        else
            cout<<(n-2)*(m-2)<<endl;
    }
    return 0;
}
发表于 2019-08-04 03:25:47 回复(0)
#include <iostream>
using namespace std;
int main(){  
    long long t,row,col;
    cin>>t;
    while (t--){
        cin>>row>>col;
        if (row == 1 && col == 1) cout<<1<<endl;
        else if (row == 1 && col > 1) cout<<(col-2)<<endl;
        else if (row>1 && col == 1) cout<<row-2<<endl;
        else if (row>1 &&col >1) cout<<(row-2)*(col-2)<<endl;
       //else cout<<(row-2)*(col-2)<<endl;
       
        
    }
    return 0;
     
}
这里吧所有的情况都讨论了,理论上只要输入合法最后一个else if和else是一个效果,结果用else if 是只通过80%,醉了
发表于 2019-07-31 17:35:35 回复(1)