首页 > 试题广场 >

计算糖果

[编程题]计算糖果
  • 热度指数:26294 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
A,B,C三个人是好朋友,每个人手里都有一些糖果,我们不知道他们每个人手上具体有多少个糖果,但是我们知道以下的信息:
A - B, B - C, A + B, B + C. 这四个数值.每个字母代表每个人所拥有的糖果数.
现在需要通过这四个数值计算出每个人手里有多少个糖果,即A,B,C。这里保证最多只有一组整数A,B,C满足所有题设条件。

输入描述:
输入为一行,一共4个整数,分别为A - B,B - C,A + B,B + C,用空格隔开。 范围均在-30到30之间(闭区间)。


输出描述:
输出为一行,如果存在满足的整数A,B,C则按顺序输出A,B,C,用空格隔开,行末无空格。 如果不存在这样的整数A,B,C,则输出No
示例1

输入

1 -2 3 4

输出

2 1 3
设1、A-B=a 
    2、B-C=b 
    3、A+B=c 
    4、B+C=d     根据1和3可以求出A、B的值;2和4可以求出B、C的值
A-B+A+B=a+c --->A=(a+c)/2                   A+B-A+B=c-a  --->B=(c-a)/2
B-C+B+C=b+c --->B=(b+d)/2                  B+C-B+C=d-b --->C=(d-b)/2
当两个B相等的时候满足上述条件,代码如下;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int d = scan.nextInt();

        int A = (a + c) / 2;
        int B1 = (c - a) / 2;
        int B2 = (b + d) / 2;
        int C = (d - b) / 2;

        if (B1 != B2) {
            System.out.println("No");
        } else {
            System.out.println(A + " " + B1 + " " + C);
        }
    }
}

发表于 2023-08-04 02:29:39 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[4];
        for(int i = 0;i < arr.length;i++){
            arr[i] = sc.nextInt();
        }
        int a,b1,b2,c = 0;
        a = (arr[2] + arr[0])/2;
        b1 = (arr[2] - arr[0])/2;
        b2 = (arr[3] + arr[1]) / 2;
        c = (arr[3] - arr[1]) / 2;
        
        if(b1 == b2){
            System.out.println(a+" " + b1 +" " + c);
        }else{
            System.out.println("No");            
        }
    }
}

发表于 2022-03-25 14:42:31 回复(0)
Jsva实现,已通过。题目中说"这里保证最多只有一组整数A.B.C满足所有题设条件。"
通过A-B,A+B解得A,通过A-(A-B)=B, B-(B-C)=C,求出B和C ,注意!!!这是假设有解的情况下解得,需要验证该方程有无解,将求得的B、C带入B+C中验证即可,如果相等,即可输出A,B,C,遇到A,B,C解得为负时说明不符合题意,还有第一步通过(A-B)+(A+B)/2=A时,如果(A-B)+(A+B)不是2的倍数,也会进行取整,导致计算错误。  这道题主要考思考的全面性。
import java.util.Scanner;

public class Main{
       public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String[]infor=in.nextLine().split(" ");
        int i0=Integer.valueOf(infor[0]);
        int i2=Integer.valueOf(infor[2]);
        int i3=Integer.valueOf(infor[3]);
        int A=(i0+i2)/2;
        int B=A-Integer.valueOf(infor[0]);
        int C=B-Integer.valueOf(infor[1]);
        if((i0+i2)%2!=0||A*B*C<0||B+C!=i3)
        {System.out.println("No");return;}
       else System.out.print(A+" "+B+" "+C);

    }
}

发表于 2020-01-27 12:08:00 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int a,b,c;
        int x1,x2,x3,x4;
        x1 = s.nextInt();
        x2 = s.nextInt();
        x3 = s.nextInt();
        x4 = s.nextInt();
        if((x1+x3)%2==0&&(x2+x4)%2==0){
            a = (x1+x3)/2;
            b = (x2+x4)/2;
            c = x4-b;
            if(a>=0&&b>=0&&c>=0)
                System.out.println(a+" "+b+" "+c);
            else
                System.out.println("No");
        }
        else
            System.out.println("No");
    }
    
}

发表于 2018-07-30 14:17:55 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        int result1,result2,result3,result4;
        Scanner sc = new Scanner(System.in);
        result1 = sc.nextInt();
        result2 = sc.nextInt();
        result3 = sc.nextInt();
        result4 = sc.nextInt();
        int A,B,C;
        if((result1+result3)%2==0){
            A=(result1+result3)/2;
            B=result3-A;
            C=result4-B;
            if(result1==A-B && result2==B-C && result3==A+B && result4==B+C){
                System.out.print(A+" "+B+" "+C);
            }else{
                System.out.print("No");
            }
        }else{
            System.out.print("No");
        }
    } 
}

编程经验不够,所以用的是最简单的方法去实现。希望帮助跟我一样基础不太好的人。
1、最开始想的是直接把键盘获得的值赋值给A+B,A-B,B-C,B+C,但是不行,所以通过result1,result2,result3,result4四个中间值进行赋值。所以解出A、B、C的值的过程就类似于解方程式, 故A=(result1+result3)/2, B=result3-AC=result4-B。
2、第二个人难点怎么判断解出的值是整型,因为按1中方程式的解法,B的值与A有关,C的值与B有关,输入的值都是整型,那么如果得出的A的值为整型,那么B与C的值也为整型。A的值为(result1+result3)/2,判断A是不是为整型,则只需要判断(result1+result3)%2是否为0即可。如果是整型,那么依次求出ABC的值,如果不是则输出 No。
3、判断语句if中的嵌套if是因为,保存测试程序时,测试系统给了四个值,按1中方法可以解出ABC的值,但是把解出的ABC的值带入A+B,B-C,B+C时,值可以对应上result1、result3、result4,但是A-B对应不上result2的值。所以解出的ABC值进行A+B,A-B,B-C,B+C运算时,值应当全部需要与result1、result2、result3、result4对应上才可以。
编辑于 2018-05-07 10:29:32 回复(1)
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
    //fixed code
    static Scanner in;
    static PrintWriter out;
    static Long startTime = null;
    static final int mod = (int) 1e9 + 7;
    static final int inf = 0x3f3f3f3f;
    static final long inff = 0x3f3f3f3f3f3f3f3fL;
    static final int maxn = (int) 1e3 + 5;
    //fixed code
    public static void main(String[] args) throws Exception {
        //Test.main();
        try {
            System.setIn(new FileInputStream("night_input"));
            startTime = System.currentTimeMillis();
        } catch (FileNotFoundException e) {
        }
        in = new Scanner(new BufferedInputStream(System.in));
        out = new PrintWriter(new BufferedOutputStream(System.out));
        /////////////////////////////////////////////
        int a_b = in.nextInt();
        int b_c = in.nextInt();
        int ab = in.nextInt();
        int bc = in.nextInt();
        int a = (a_b + ab) / 2;
        int b = (b_c + bc) / 2;
        int c = (bc - b_c) / 2;
        if (a_b == a - b && b_c == b - c && ab == a + b && bc == b + c) {
            out.println(a + " " + b + " " + c);
        } else {
            out.println("No");
        }
        ////////////////////////////////////////////
        out.flush();
        if (startTime != null) {
            out.println("\ntime: " + (System.currentTimeMillis() - startTime) + " (ms)");
        }
        in.close();
        out.close();
    }
}

发表于 2018-03-06 23:21:41 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x1 = sc.nextInt();
        int x2 = sc.nextInt();
        int x3 = sc.nextInt();
        int x4 = sc.nextInt();
        int x2_x4 = x2 + x4;
        double BB = x2_x4 / 2.0;
        int B = (int)BB;
        if (B == BB){
            int A1 = x1 + B;
            int A3 = x3 - B;
            int C2 = B - x2;
            int C4 = x4 - B;
            if (A1 == A3 && C2 == C4){
                System.out.print(String.valueOf(A1) + " ");
                System.out.print(String.valueOf(B) + " ");
                System.out.print(C2);
                return;
            }
            
        }
        System.out.println("No");

    }
}

发表于 2018-03-03 12:53:09 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int[] res = new int[4];
            for (int i = 0; i < res.length; i++)
                res[i] = in.nextInt();
            int A = (res[0] + res[2]) / 2;
            int B = (res[1] + res[3]) / 2;
            int C = B - res[1];
            if (A - B == res[0] && B - C == res[1]
                    && A + B == res[2] && B + C == res[3])
                System.out.println(A + " " + B + " " + C);
            else
                System.out.println("No");
        }
    }
}

发表于 2017-12-11 14:07:17 回复(0)
import java.util.Scanner;  public class Main
{  public static void main(String args[]) 
    {
        Scanner scanner = new Scanner(System.in);  while (scanner.hasNext()) 
        {
            String[] strings = scanner.nextLine().split(" ");  int AMinusB = Integer.parseInt(strings[0]);  int BMinusC = Integer.parseInt(strings[1]);  int APlusB = Integer.parseInt(strings[2]);  int BPlusC = Integer.parseInt(strings[3]);  if ((AMinusB + APlusB) % 2 == 0 && (BMinusC + BPlusC) % 2 == 0) 
            {  int a = (AMinusB + APlusB) / 2;  int b = (BMinusC + BPlusC) / 2;  int c = BPlusC - b;  if (a >= 0 && b >= 0 && c >= 0) 
                    {
                        System.out.println(a + " " + b + " " + c);   }  else {
                        System.out.println("No");   }
            }  else  {
                System.out.println("No");   }
        }
    }
}
这个题应该是小学数学题吧没就是需要吧结果不存在的情况考虑全面
发表于 2017-11-22 21:02:54 回复(0)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A_B = sc.nextInt();
        int B_C = sc.nextInt();
        int APB = sc.nextInt();
        int BPC = sc.nextInt();
        int sum = A_B+B_C+APB+BPC;
        sc.close();
        //System.out.println("666");
        if(sum != 2*APB) {
            System.out.println("No");
            return;
        }
        int A = (A_B+APB)/2;
        int B = (APB-A_B)/2;
        int C = B-B_C;
        System.out.println(A+" "+B+" "+C);
    }
}    
发表于 2017-11-14 18:50:36 回复(0)
计算糖果:
 这道题最重要的是要理清A, B, C的求解方法。其次,要确定A,B,C是否为整数。最后,要确定A,B,C必须为大于等于零的数。

import java.util.*;
public class Main{
    public static void main(String[] args){
     
    Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int s = scan.nextInt();
            int t = scan.nextInt();
            int j = scan.nextInt();
            int q = scan.nextInt();
            int sum =0;
            int A = 0;
            int B = 0;
            int C = 0;
            sum = s +j ;
            if(sum %2 !=0){
                System.out.println("No");
        }else{
        A = sum/2;
        B = j-A;
        C =q -B;
        if(A>=0&B>=0&C>=0){
        System.out.println(A+" "+B+" "+C);
        }else{
            System.out.println("No");
        }
        }
    }
 }
}
发表于 2017-10-27 21:03:42 回复(1)
这里需要判断有解和无解两种情况,假设有解,则A=(c+a)/2,B=(c-a)/2,C=(c-a-2b)/2;而此时就需要验证四个表达式是否都满足条件同时还要判断2A,2B,2C是否分别等于c+a,c-a,c-a-2b,要排除/运算自动取整的情况
import java.util.Scanner;

public class StringUtil {

	//计算糖果
	public static void main(String[ ] args){
		
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int a = in.nextInt();
			int b = in.nextInt();
			int c = in.nextInt();
			int d = in.nextInt();
			int x = (c+a)/2;
			int y = (c-a)/2;
			int z = (c-a-2*b)/2;
			if(x-y == a && x+y == c && y-z == b && y+z == d && x*2 == a+c && y*2 == c-a && z*2 == c-a-2*b){
				System.out.println(x + " " + y + " " + z);
			}
			else{
				System.out.println("No");
			}
		}
	}
}

发表于 2017-09-04 16:55:56 回复(0)
public class Main{
           
    public static void main(String[] args){
                
        Scanner in = new Scanner(System.in);
        
        while(in.hasNext()){
        //A-B
        int x = in.nextInt();
        //B-C
        int y = in.nextInt();
        //A+B
        int z = in.nextInt();
        //B+C
        int w = in.nextInt();
        
        if((x+z)%2 == 0){
            int A = (x+z)/2;
            int B = A - x;
            int C = B - y;
            if(B+C == w){
               System.out.print(A + " " + B +" "+ C + "\n");
              
            }else{
               System.out.println("No");
            }

        }else{
            System.out.println("No");
        }   
           
        }       
    }
}

发表于 2017-09-03 09:02:29 回复(0)
import java.util.Scanner;

public class Main{
	public static void fun(int a,int b,int c,int d){
		int s[]=new int[3];
		double  A=(double)(a+c)/2;
		double B=(double)(b+d)/2;
		double C=d-B;
		if (A>=0&&B>=0&&C>=0&&A%1==0&&B%1==0) {
			s[0]=(int) A;
			s[1]=(int) B;
			s[2]=(int) C;
			System.out.println(s[0]+" "+s[1]+" "+s[2]);
			
		}else {
			System.out.println("No");
			
		}
		
	

	}
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext()){
			int a=scanner.nextInt();
			int b=scanner.nextInt();
			int c=scanner.nextInt();
			int d=scanner.nextInt();
			
			fun(a, b, c, d);
			
		
		}
		
	}

}


发表于 2017-03-30 14:04:10 回复(0)
import java.util.Scanner;
public class Main {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int aDiffB = scanner.nextInt();
            int bDiffC = scanner.nextInt();
            int aAddB = scanner.nextInt();
            int bAddC = scanner.nextInt();
            float a = (float)(aDiffB+aAddB)/2;
            if (a!=(int)a||a<0) {System.out.println("No");return;}
            float b = (float)(bDiffC+bAddC)/2;
            if (b!=(int)b||b<0) {System.out.println("No");return;}
            int c = (int)(bAddC-b);
            System.out.println((int)a+" "+(int)b+" "+c);
        }
    }
}
隐藏条件:糖果数为整数且为正数
发表于 2016-09-22 19:27:09 回复(0)
这道题目的实质是:判断三元一次方程组是否有解及求解。
把题目条件用方程式表示:
A-B=Y1;
B-C=Y2;
A+B=Y3;
B+C=Y4;

用消元法求解:
A=(Y1+Y3)/2;
B=(Y3-Y1)/2=(Y2+Y4)/2;
C=(Y4-Y2)/2;

由于题目给出的是整数,要求解也是整数,这个约束条件也需要注意下。
不满足约束条件就是没解,就可以输出NO了,满足所有的约束条件那就是有解。

代码如下:
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int y1,y2,y3,y4;
        float a,b,c;
        while (in.hasNextInt()) {
            y1 = in.nextInt();
            y2 = in.nextInt();
            y3 = in.nextInt();
            y4 = in.nextInt();
            a=(y1+y3)/2f;
            b=(y3-y1)/2f;
            c=(y4-y2)/2f;
            if((a-((y1+y3)/2))!=0){
                System.out.print("No");
                return ;
            }
            if((b-((y3-y1)/2)!=0)||(b!=((y2+y4)/2))){
                System.out.print("No");
                return ;
            }
            if((c-((y4-y2)/2))!=0){
                System.out.print("No");
            return ;
            }
            //满足所有的约束条件,输出解。
            System.out.print((int)a+" "+(int)b+" "+(int)c);
            }
    }
}

编辑于 2016-12-05 12:33:22 回复(6)
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int line[] = new int[4];
        int a, b, c;
        
        while(sc.hasNextInt()) {
			line[0] = sc.nextInt();
			line[1] = sc.nextInt();
			line[2] = sc.nextInt();
			line[3] = sc.nextInt();
	
			Double doubleA = (line[0] + line[2]) / 2.0;
            Double doubleB = (line[1] + line[3]) / 2.0;

			a = (line[0] + line[2]) / 2;
			b = (line[1] + line[3]) / 2;
			c = b - line[1];
            
            //判断((A - B) + (A + B)) / 2和((B - C) + (B + C)) / 2是否为整数
			//以及a、b、c是否满足题目的四个条件
            if (Math.ceil(doubleA) == a && Math.ceil(doubleB) == b 
           	    	&& a - b == line[0] && b - c == line[1]
					&& a + b == line[2] && b + c == line[3]) {
				System.out.print(a + " " + b + " " + c);
			} else {
				System.out.print("No");
			}
        }
        
        sc.close();
	}

}

发表于 2016-09-14 17:47:58 回复(0)