跳到正文

个人记录

题解:U575670

yonex_pcy约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月11日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

题目传送门

本题要我们求 |x| 该题只需用c++自带函数abs()求解即可。

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	double x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		cout<<abs(x)<<'\n'
	}
	return 0;
}

或者对<0的x进行-x的操作,对>=0的x直接输出

C++
#include <bits/stdc++.h>
using namespace std;
signed main()
{;
	int n;
	double x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		if(x<0)  cout<<-x<<'\n';
		if(x>=0) cout<<x<<'\n';
	}
	return 0;
}
原文