题意:给出数n,找出所有满足下列条件的三角形的个数:
三条边a,b,c,满足1 ≤ a ≤ b ≤ c ≤ n且
解题思路:
异或:
异或,英文为exclusive OR,缩写成xor
异或(
xor)是一个数学运算符。它应用于逻辑运算。异或的数学符号为“⊕”,计算机符号为“xor”。其运算法则为:
a⊕b = (¬a ∧ b) ∨ (a ∧¬b)
如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。
枚举b,c两条边,要想符合条件那么a==b⊕c,此时判断a是否小于b和c并且这三边能否构成三角形。
AC代码:
#include<stdio.h>
int main()
{
int n,a;
while(~scanf("%d",&n))
{
int sum=0;
for(int c=1;c<=n;c++)
{
for(int b=1;b<=c;b++)
{
int a=b^c;
if(a>b||a>c) continue;
if(a+b>c) sum++;
}
}
printf("%d\n",sum);
}
return 0;
}