题解 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 ...
随机推荐
- Android更改popupmenu背景并显示图标
似乎popupmenu是无法单独设置style的,好像是由context决定的,前几天需要设置style,找了很久才找一一个办法,似乎是通过 ContextThemeWrapper 包装一个 Cont ...
- Centos 7 使用(Service iptables stop/start)关闭/打开防火墙 Failed to stop iptables.service: Unit iptables.service not loaded.
背景: 测试部署NetCore 项目到linux 系统时,窗口显示项目部署成功:但是本机无法访问(linux 在虚拟机上[ centos 7.6] ); 如下图↓ 能够相互ping 通,(Xshe ...
- python3-cookbook笔记:第九章 元编程
python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...
- idea创建基于maven的web项目
1.点击create new project,选择maven,点击next 2.输入项目信息,点击finish 3.进入项目后,点击菜单File->Project Structure开始配置项目 ...
- WebSocket以及socketIO的使用
简介 WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 WebSocket API 中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久 ...
- ES6 - 基础学习(1): 开发环境搭建
现在Chrome浏览器已经很好的支持ES6了,但有些低版本的浏览器或其他浏览器还是不支持ES6的语法,因此实际项目开发或上线过程中就需要把ES6的语法转变成ES5的语法.项目开发过程中 Webpack ...
- [VB.NET Tips]创建匿名类型列表
在调用一些Web API时经常要发送或接收一些数据,在构造Json时可能要创建一些类. 很多都是在调用相关方法才使用到这些类,那使用匿名类型是个不错的选择.如果要传些表结构数据时,就要创建List. ...
- C#上位机之—WinForm实现Socket异步通讯示例
工作中常用到的一些知识点,总是用完就忘,第一次尝试用博客记录下来,以备后用: Socket通讯,Socket(套接字)是基于TCP/IP通讯方式的封装好的类,调用时需要添加下面的服务引用: using ...
- C#实现的Table的Merge,以及实现Table的Copy和Clone
C#实现的对两个Table进行Merge,两表必须存在至少一个公共栏位作为连接项,否则连接就失去了意义.如下是对两个table进行Merge的详细代码: private void button1_Cl ...
- 关于HashMap中的扰动函数的疑问
最近再看jdk8的hashmap源码,当看到这一步的时候有点疑问,去网上搜了一下,看到的所有文章基本上都是一篇抄一篇的(反正目前各大社区就是这么个状况),那个意思就是让高16位也参与运算,增加结果的随 ...