int multiply(int x, int y);
int main() {
int a = 4;
int b = 5;
int result = multiply(a, b);
std::cout << "The result is: " << result << std::endl;
return 0;
}
int multiply(int x, int y) {
return x * y;
}
int x = 10;
void func() { int x = 20; std::cout << x; }
int main() {
func();
std::cout << x;
return 0;
}
void swap(int a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 1, y = 2;
swap(x, y);
std::cout << x << y;
return 0;
}
struct Person {
std::string name;
int age;
struct Address {
std::string street;
std::string city;
};
Address address;
};
下面描述错误的是( )。
int f(int n) {
if (n == 1 || n == 2)
return n;
int f1 = 1;
int f2 = 2;
int res = 0;
for (int i = 3; i <= n; i++) {
________________________________ // 在此处填入代码
}
return res;
}
bool f(int arr[], int n, int target) {
for (int i = 0; i < (1 << n); i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
sum += arr[j];
}
}
if (sum == target) return true;
}
return false;
}
double hmean(double a, double b) {
if (a == -b )
throw runtime_error("Runtime error occurred.");
return 2.0*a*b/(a + b);
}
int main() {
double x = 10;
double y = -10;
try {
int result = hmean(x, y);
cout << "hmean: " << result << endl;
} catch (const runtime_error& e) {
cout << "Caught: " << e.what() << endl;
} catch (...) {
cout << "Caught an unknown exception." << endl;
}
return 0;
}