题解 AT5632 【Sum of Two Integers】
在幼儿园的时候,我们就学习过把一个数分成\(a\)与\(b\),我们只需要用计算机来模拟这个过程就可以了。
我们先从奇数开始看起,以\(5\)为例:

我们可以发现,\(5\)可以分成\(1\)和\(4\),\(2\)和\(3\),\(3\)和\(2\),以及\(4\)和\(1\),也就是说,一个奇数可以有\(n-1\)种方法进行分解,去重后也就是\((n-1)\div2\)种方法。
可以在举一个例子,如\(7\),它可以分成\(1\)和\(6\),\(2\)和\(5\),\(3\)和\(4\),\(4\)和\(3\),\(5\)和\(2\),\(6\)和\(1\),去重后就是\(1\)和\(6\),\(2\)和\(5\),\(3\)和\(4\),共\(3\)种,再用刚刚推出的公式,\((7-1) \div 2 = 3\),发现公式是正确的。
再看偶数,在这里以\(4\)为例。

我们可以发现,一共有\(3\)种方法可以将\(4\)分解,和奇数一样,也有\(n-1\)种方案,其中,\(2\)与\(2\)是重复的,去掉,\(3\)和\(1\)也是重复的,去掉。因此我们发现,\(4\)只有\(1\)和\(3\)一种分解方法,可以用\((n-1)\div2\)的方法判断。
最后我们发现,他们的公式都一样,所以可以直接套公式。
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	cout<<(n-1)/2;
	return 0;
}
												
											题解 AT5632 【Sum of Two Integers】的更多相关文章
- LeetCode Sum of Two Integers
		
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
 - [LeetCode] Sum of Two Integers 两数之和
		
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
 - Nim Game,Reverse String,Sum of Two Integers
		
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
 - LeetCode 371. Sum of Two Integers
		
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
 - leetcode371. Sum of Two Integers
		
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
 - 【LeetCode】Sum of Two Integers
		
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
 - 每天一道LeetCode--371. Sum of Two Integers
		
alculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Examp ...
 - 371. Sum of Two Integers -- Avota
		
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
 - Leetcode 371: Sum of Two Integers(使用位运算实现)
		
题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
 - LeetCode 371. Sum of Two Integers (两数之和)
		
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
 
随机推荐
- Kafka中数据的流向
			
1: 多个消费者消费同一个Topic数据相同的数据 2: 多个消费者消费同一个Topic数据不同数据 3: 各个消费者按组协调消费 1: 多个消费者消费同一个Topic数据相同的数据 (1)使用一个全 ...
 - bat常用符合和for语句等
			
一.开头 @echo off(默认是echo on)@echo off执行以后,后面所有的命令均不显示,包括本条命令 二.特殊符号 1. | 命令管道符,echo Y|rd /s c:\abc,通过管 ...
 - NAT 地址转换
			
NAT功能 NAT不仅能解决了lP地址不足的问题,而且还能够有效地避免来自网络外部的攻击,隐藏并保护网络内部的计算机.1.宽带分享:这是 NAT 主机的最大功能.解决IP4地址短缺的问题 ...
 - Linux 配置 DNS
			
这里不讨论如何在linux上搭建一台DNS服务器: 这里讨论的是 配置 linux系统,让其能够解析域名,使用户可以流畅使用Internet 先了解几个文件,位于/etc目录下的有:hosts,hos ...
 - Mac启动MongoDB报错:exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
			
这是主要错误: initAndListen中的异常:NonExistentPath:找不到数据目录/ data / db. 最新版的Mac系统Catalina发生了令人惊讶的更改:它不允许更改根目录( ...
 - 2020-2-27今日总结——滚动监听&导航
			
利用Bootstrap 开发工具实现滚动监听 (此文只做学习路上的归纳分享总结用,如有侵权,请联系我删除) 使用滚动监听,比较特殊,要在body中设置scroll,以及触点. 很好理解,因为滚动是多对 ...
 - StarUML之四、StarUML的Diagrams(图)与Elements(元素)及相关属性
			
Diagrams(图)可以理解为画布 1:创建图 在右侧的Model Explorer管理界面的第一个节点右键,或者选择菜单中Model | Add Diagram | [DiagramType]都 ...
 - 「Flink」Flink中的时间类型
			
Flink中的时间类型和窗口是非常重要概念,是学习Flink必须要掌握的两个知识点. Flink中的时间类型 时间类型介绍 Flink流式处理中支持不同类型的时间.分为以下几种: 处理时间 Flink ...
 - 「Flink」理解流式处理重要概念
			
什么是流式处理呢? 这个问题其实我们大部分时候是没有考虑过的,大多数,我们是把流式处理和实时计算放在一起来说的.我们先来了解下,什么是数据流. 数据流(事件流) 数据流是无边界数据集的抽象 我们之前接 ...
 - ubuntu18.04管理redis
			
ubuntu下 redis的安装使用 安装 1. 进入redis安装路径 cd ~/installed/redis-5.0.7 2. 启动redis 启动服务端redis-server 启动客户端(必 ...