已知 XYZ+YZZ=532,其中,X、Y、Z 为数字,编程求出 X、Y 和 Z 的值。
C语言实现
#include<stdio.h>
int main()
{
int x, y, z;
for (x = 0; x < 10; x++){
for (y = 0; y < 10; y++){
for (z = 0; z <10 ; z++){
if (x * 100 + y * 10 + z + y * 100 + z * 10 + z == 532){
printf("x=%d\n", x);
printf("y=%d\n", y);
printf("z=%d\n", z);
break;
}
}
}
}
return 0;
}
C++实现(枚举)
#include<iostream>
using namespace std;
int main() {
int x, y, z;
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
for (z = 0; z < 10; z++) {
if (x * 100 + y * 10 + z + y * 100 + z * 10 + z == 532) {
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << "z=" << z << endl;
}
}
}
}
return 0;
}
Java实现
public class yzg {
public static void main(String[] args) {
int x,y,z;
for(x=0;x<10;x++){
for(y=0;y<10;y++){
for(z=0;z<10;z++){
if(x*100+y*10+z+y*100+z*10+z==532){
System.out.println("X="+x);
System.out.println("Y="+y);
System.out.println("Z="+z);
}
}
}
}
}
}