在幼儿园的时候,我们就学习过把一个数分成\(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】的更多相关文章

  1. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】Sum of Two Integers

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  7. 每天一道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 ...

  8. 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 - ...

  9. 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 -. ...

  10. 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 ...

随机推荐

  1. javascript原生js轮播图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. saltstack集合

    saltstack集合 saltstack(一):   saltstack简介 saltstack(二):   saltstack安装及配置 saltstack(三):   saltstack远程执行 ...

  3. 1163 - Bank Robbery

    1163 - Bank Robbery   In one very cold morning, Mark decides to rob a bank. But while trying hacking ...

  4. leetcode-简单-栈-逆波兰表达式

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明:  整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话 ...

  5. ABP框架迁移到Mysql

    ABP框架 .NetCore3.x版本 1.首先找到xxx.Core 项目,添加引用Microsoft.EntityFrameworkCore.Tools 2.找到xxx.EntityFramewor ...

  6. 多线程共享变量和 AsyncLocal

    >>返回<C# 并发编程> 1. 简介 2. 异步下的共享变量 3. 解析 AsyncLocal 3.1. IAsyncLocalValueMap 的实现 3.2. 结论 1. ...

  7. vue目录结构熟悉

    给项目的入口文件做点小改变: [补充:编辑器建议使用vscode,我还没装,暂时先用phpstorm] 打开 APP.vue 文件,代码如下(解释在注释中) <!-- 展示模板 --> & ...

  8. mysql必知必会--创建计算字段

    计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式.下面举 几个例子. * 如果想在一个字段中既显示公司名,又显示公司的地址,但这两 个信息一般包含在不同的表列中. * 城市.州和邮政编码存 ...

  9. CentOS、Ubuntu的安装

    Linux使用最广泛的2个发行版:CentOS.Ubuntu. CentOS安全性高,常用作企业的服务器,Ubuntu常用作个人桌面. 常见的虚拟机有2个: VM VirtualBox,这个是Orac ...

  10. JS事件绑定的三种方式比较

    js事件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...