题解 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 ...
随机推荐
- javascirpt获取随机数
/* getran(min, max, n): 获取min与max之间的随机数 n: n保留浮点数数量 */ function getran(min, max, n){ return Number(( ...
- pt-query-digest 慢日志监控
一.安装percona-toolkit,以centos为例 yum -y install https://repo.percona.com/yum/percona-release-latest.noa ...
- [Linux]curl 获取本服务器公网IP
curl ifconfig.me curl icanhazip.com curl curlmyip.com curl ip.appspot.com curl ipinfo.io/ip curl ipe ...
- light oj 1035 - Intelligent Factorial Factorization 素因子分解
1035 - Intelligent Factorial Factorization Given an integer N, you have to prime factorize N! (facto ...
- python随用随学20200118-函数的高级特性
高阶函数 话说当年C语言和Java里好像都有这么个东西...忘了 一句话说就是函数名本身就是一个引用. 可以作为变量传递. 一个简单的例子: def power_demo(x): return x* ...
- 如何用Java8 Stream API找到心仪的女朋友
传统的的Java 集合操作是有些啰嗦的,当我们需要对结合元素进行过滤,排序等操作的时候,通常需要写好几行代码以及定义临时变量. 而Java8 Stream API 可以极大简化这一操作,代码行数少,且 ...
- pikachu-跨站脚本漏洞(XSS)
一.跨站脚本漏洞概述 1.1 什么是XSS漏洞? XSS是一种发生在Web前端的漏洞,其危害的对象也主要是前端用户. 1.2 XSS如何攻击? 二.跨站脚本漏洞类型及测试流程 2.1 跨站脚本 ...
- java 测试 (junit+ junit 断言 + postman)
实际开发中,除了开发,我想测试也是必不可少的一环吧.从简单的@Test .main 方法测试 到 页面测试 ,断言,postman. bug是无处不在,随时发生的事,高效率的调试.检测可以节省大量的开 ...
- 金融计算的开源库——QuantLib 学习入门
本文在Creative Commons协议下发布. 简介 瞬息万变的金融市场开发出了太多的金融产品,产生了太多的计算问题,这对于 Fintech 来讲:无论是计算能力上的,还是软件设计上的是一个巨大的 ...
- MySQL 8 InnoDB 集群管理
使用 dba.checkInstanceConfiguration() 在添加实例到集群中前,使用该方法检查实例配置是否满足InnoDB 集群要求. 使用 dba.configureLocalInst ...