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
#include<iostream> using namespace std; int main() { int a, b, c, d; int A, B, C; while(cin>>a>>b>>c>>d) { A=(a+c)/2; B=(b+d)/2; C=(d-b)/2; if(A-B==a && B-C==b && A+B==c && B+C==d) { cout<<A<<" "<<B<<" "<<C<<endl; } else { cout<<"No"<<endl; } } return 0; } //由于涉及到除法,得到的结果A,B,C有可能是截断后的数据,不妨逆推回去验算一遍,假如与原方程相符合,则正确
// 要求ABC都为整数 // 注意多了一个约束条件,即四个式子解3个未知数,所以还要确定满不满足A+B==n3 import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n1 = in.nextInt(); int n2 = in.nextInt(); int n3 = in.nextInt(); int n4 = in.nextInt(); int A = (n1+n3)/2; int B = (n2+n4)/2; int C = B-n2; if((n1+n3)%2==0&&(n2+n4)%2==0&&(A+B==n3)){ System.out.println(A+" "+B+" "+C); }else{ System.out.println("No"); } } }
#include<iostream> using namespace std; int main() { int x1, x2, x3, x4; cin >> x1 >> x2 >> x3 >> x4; if(x1+x2+x4 != x3)//判断是否无解-自相矛盾 cout << "No" << endl; else { int B = (x2+x4) / 2; if(2*B != (x2+x4))//判断是否为非整数解 { cout << "No" << endl; } else { int A = B + x1; int C = B - x2; cout << A << " " << B << " "<< C << endl; } } }
增广矩阵要有唯一解,要求r(A|b) = r(A),可以得到一个式子,不等于0时则没有唯一解。满足则可以 根据阶梯矩阵求解A B C。 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int M = scanner.nextInt(); int N = scanner.nextInt(); int P = scanner.nextInt(); int Q = scanner.nextInt(); if (M - P + N + Q != 0) { System.out.println("No"); }else { int A = P - (N + Q) / 2; int B = (N + Q) / 2; int C = (Q - N) / 2; System.out.println(A + " " + B + " " + C); } } }
i1, i2, i3, i4 = map(int, input().split())
a = (i1 + i3) // 2 if (i1 + i3) % 2 == 0 and (i1 + i3) >= 0 else None
b = (i2 + i4) // 2 if (i2 + i4) % 2 == 0 and (i2 + i4) >= 0 else None
print(" ".join(map(str, [a, b, i4 - b])) if isinstance(a, int) and isinstance(b, int) else "No")
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec(4); int a, b, c; for(int i = 0; i < 4; ++i) { cin >> vec[i]; } a = (vec[0] + vec[2])/2; b = (vec[1] + vec[3])/2; c = (vec[3] - vec[1])/2; if( ((a-b) == vec[0]) && ((b-c) == vec[1]) && ((a+b) == vec[2]) && ((b+c) == vec[3]) ) cout << a << " " << b << " " << c << endl; else cout << "No" << endl; return 0; }
#include<stdio.h> int main() { int A,B,C,a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); A = (a+c)/2; B = (a-c)/(-2); C = (b-d)/(-2); if(B == (b+d)/2) printf("%d %d %d",A,B,C); else printf("No"); }
#include<stdio.h> int main() { int n1,n2,n3,n4,A1,B1,C1; float A,B,C; scanf("%d %d %d %d",&n1,&n2,&n3,&n4); A=(n1+n3)/2.0; B=(n2+n4)/2.0; C=n4-B; A1=(int)A; B1=(int)B; C1=(int)C; if((A1==A)&&(B1==B)&&(C1==C)&&(A1>=0)&&(B1>=0)&&(C1>=0)) printf("%d %d %d",A1,B1,C1); else printf("No"); return 0; }
<?php //输入1 -2 3 4 //输出 2 1 3 $arr = explode(' ',trim(fgets(STDIN))); $a = ($arr[0]+$arr[2])/2; //A-B+A+B = 2A =1+3=4 A=2 $b = ($arr[1]+$arr[3])/2; //B-C+B+C = 2B =-2+4=2 B=1 $c = $arr[3]-$b; // B+C=4 C=4-B //糖果数必须是大于等于0的整数 if(!is_int($a*$b*$c)||$a<0||$b<0||$c<0) echo 'No'; else echo "$a $b $c";
def isOK(x): if x < 0: return False if int(x) == x: #是否是小数 return True line = raw_input() data = line.strip().split(" ") data = map(int,data) d0 = data[0] d1 = data[1] d2 = data[2] d3 = data[3] #求解A、B、C A = 0.5 * (d2 + d0) B1 = 0.5 * (d2 - d0) C = 0.5 * (d3 - d1) B2 = 0.5 * (d3 + d1) #B1、B2为B的两种算法,必须一致才能满足要求 if isOK(A) and isOK(B1) and isOK(C) and (B1 == B2): A = int(A) B1= int(B1) C = int(C) print str(A) +" " + str(B1) + " " + str(C) else: print "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"); } } }
//根据代数式特点,直接用消元法可以得到结果 #include<iostream> using namespace std; int main(){ int a,b,c,d; cin>>a>>b>>c>>d; int A=(int)(a+c)/2; int B=(int)(b+d)/2; int C=(int)(d-b)/2; if(A-B==a&&B-C==b&&A+B==c&&B+C==d){ cout<<A<<" "<<B<<" "<<C<<endl; } else cout<<"No"<<endl; return 0; }
def Number(n1,n2,n3,n4): ls=[] A=(n1+n3)/2 B1=(n3-n1)/2 B2=(n2+n4)/2 C=(n4-n2)/2 if A>=0 and A==int(A): ls.append(int(A)) ls.append(" ") if B1>=0 and B1==int(B1) and B1==int(B2): ls.append(int(B1)) ls.append(" ") if C>=0 and C==int(C): ls.append(int(C)) if len(ls)==5: return ls else: return "No" a,b,c,d= map(int,input().split()) b=Number(a,b,c,d) if b!='No': for i in b: print(i,end="") else: print(b)
#include<stdio.h> int main(){ int i,j,k,flag=0,a,b,c,d,res1,res2,res3; scanf("%d%d%d%d",&a,&b,&c,&d); for(i=-30;i<=30;i++) for(j=-30;j<=30;j++) for(k=-30;k<=30;k++) if(i-j==a&&j-k==b&&i+j==c&&j+k==d){ flag=1; res1=i,res2=j,res3=k; } flag?printf("%d %d %d\n",res1,res2,res3):printf("No\n"); }
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"); } } } }