刷题记录之盖印章

104 阅读1分钟

声明 记录学习过程

题目链接

盖印章

思路

题目文字描述很多,题意如下:我给你一个盖完印章后的图,以及完成这个图所使用印章A和B的总次数,求印章A和印章B的各自使用次数

设印章A的使用次数是x,印章B的使用次数是y,盖完印章后的图中1的个数是cnt,印章A和印章B一共使用了k次,则建立如下方程

{x+y=k3x+2y=cnt\left\{ \begin{array}{ll} x + y = k \\ 3x + 2y = cnt \\ \end{array} \right.

x=k-y带入3x+2y=cnt可得如下y=3k-cnt,然后代码就很简单了

代码

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int k = s.nextInt();
        int one_cnt = 0;
        char[][] map = new char[n][m];
        for(int i=0;i<n;i++){
                map[i] = s.next().toCharArray();
            for(int j=0;j<m;j++){
                if(map[i][j]=='1')
                    one_cnt++;
            }
        }
        int y = 3*k-one_cnt;
        int x = k-y;
        System.out.printf("%d %d",x,y);
        // x + y = k
        // 3x + 2y = one_cnt
        // x = k-y
        // 3(k-y)+2y=one_cnt
        // 3k-3y + 2y = one_cnt
        // 3k-y = one_cnt
        // 3k+one_cnt = y
        s.close();
    }
}

总结

我看到这个题的时候很蒙,还旋转,没有一点想法,自己写的时候想到了上面方程中的第二个方程,但是没有想到第一个,慢慢来把,加油

最后

由于正在学习的过程中,会有错误哈,也请各位指出,共同进步