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 -2 3 4
2 1 3
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"); } } }
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); } }
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"); } }
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"); } } }
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(); } }
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"); } } }
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"); } } } }这个题应该是小学数学题吧没就是需要吧结果不存在的情况考虑全面
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"); } } } }
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"); } } } }
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); } } }
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); } } }
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(); } }