c++ code:(2)function

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define EPS 1e-7
int main()
{
	double a,b,c; //不要用 float,精度不够
	scanf("%lf%lf%lf",&a,&b,&c);
	double tmp = b*b - 4*a*c;
	if( tmp < EPS && tmp > - EPS)
		printf("x1=x2=%.5f",(-b)/(2*a)+EPS);  // + EPS是为了防止出现 -0.00000 
	else if( tmp > EPS) {
		double x1 = (-b+sqrt(tmp))/(2*a);
		double x2 = (-b-sqrt(tmp))/(2*a)+EPS;
		if( x1 - x2 > EPS)
			printf("x1=%.5f;x2=%.5f",x1+EPS,x2+EPS);
		else
			printf("x1=%.5f;x2=%.5f",x2+EPS,x1+EPS);
	}
	else {
		
		printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",(-b)/(2*a)+EPS,sqrt(-tmp)/(2*a)+EPS,(-b)/(2*a)+EPS,sqrt(-tmp)/(2*a)+EPS);
	}
	return 0;
}

 

#include <iostream>
//#include <iomanip>
#include <cstdio>
using namespace std;
const int MX = 110;
short a[MX][MX];  //0-255 so 采用short
short b[MX][MX];
int main()
{
	freopen("f:\\MOOC c++\\week5\\8167.txt", "r", stdin);
	int n,m;
	cin >> n >> m;
	for(int i = 1;i <= n; ++i)
		for(int j = 1; j <= m; ++j )  {
			cin >> a[i][j];
			b[i][j] = a[i][j];
		}
	for(int i = 1;i <= n; ++i)
		for(int j = 1; j <= m; ++j ) {
			if( i > 1 && i < n && j > 1 && j < m) {
				int sum = a[i][j] + a[i-1][j] + a[i+1][j]+a[i][j-1]+a[i][j+1];
				int v = sum / 5;
				//rounding ~~~~
				double f = (double)sum / 5;
				if( f - v - 0.5 > 1e-6)
					++v;

				b[i][j] = v;
			}
		}
	for(int i = 1;i <= n; ++i) {
		for(int j = 1; j <= m; ++j ) 
			cout << b[i][j] << " ";
		cout << endl;
	}
	return 0;
}

#include<iostream>
using namespace std;

int gcd(int a, int b)
{
	if (a % b == 0)
		return b;
	return gcd(b,a%b);
}

int main()
{
	freopen("f:\\freopen.txt", "r", stdin);
	int x, y;
	cin >> x >> y;
	cout << gcd(y, x) << endl;
	return 0;
}

 

#include <iostream>
using namespace std;
int bitManipulation1(int n, int m, int i) {
	return ((n&~(1<<i))|((m>>i)&1)<<i);
}
int main() {
	freopen("f:\\freopen.txt", "r", stdin);
	int n, m, i, t;
	cin >> t;
	while (t--) {
		cin >> n >> m >> i;
		cout << bitManipulation1(n, m, i) << endl;
	}
	return 0;
}

 

 

#include <iostream>
using namespace std;

int bitManipulation2(int n, int i) {
	return (n&~(1 << i))&(~(((n >> i) & 1) << i));
	return n ^ (~0 << (32 - i));
}

int main() {
	freopen("f:\\freopen.txt", "r", stdin);
	int t, n, i;
	cin >> t;
	while (t--) {
		cin >> n >> i;
		cout << bitManipulation2(n, i) << endl;
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C++中的std::make_unique是一个函数模板,用于动态分配对象并返回一个std::unique_ptr智能指针。它接受构造函数的参数,并将它们完美转发给要创建对象的构造函数。\[1\] 使用std::make_unique可以避免手动调用new操作符来分配内存,并且在对象创建后,会自动管理内存的释放。这样可以避免内存泄漏和手动释放内存的麻烦。 下面是一个示例代码片段,演示了如何使用std::make_unique函数动态分配对象: ```cpp #include <memory> #include <iostream> class MyClass { public: MyClass(int a, float b) { std::cout << "Constructor called with parameters: " << a << ", " << b << std::endl; // Other initialization code... } }; int main() { // Create a unique_ptr type pointer managed by std::make_unique function auto ptr = std::make_unique<MyClass>(10, 20.5); // Use the created object // ... return 0; } ``` 在上面的示例中,我们使用std::make_unique来创建一个指向MyClass对象的std::unique_ptr指针。通过传递参数10和20.5,我们调用了MyClass的构造函数,并创建了一个MyClass对象。\[1\] 总结起来,std::make_unique是一个方便的函数模板,用于动态分配对象并返回一个std::unique_ptr智能指针,它可以简化内存管理的过程。\[1\] #### 引用[.reference_title] - *1* [std::make_unique 使用](https://blog.csdn.net/oHeHui1/article/details/130790011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C++11总结](https://blog.csdn.net/zhzhangnews/article/details/100565986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值