public class Triangle { public static void main(String[] args) { // TODO Auto-generated method stub int a,b,c,n; int time = 0; for(n = 100; n <= 999; n++){ a = n / 100; b = (n - a*100) / 10; c = (n - a*100 - b*10); // System.out.println("a = " + a); // System.out.println("b = " + b); // System.out.println("c = " + c); if(a == 0 || b==0 || c ==0){ continue; } if(a == b && a + b > c || a==c && a+c > b || b ==c && b+c > a){ time++; } } System.out.println(time); } }
//在100-999这900个自然数中,若将组成这个数的三个数字认为是三条线段的长度,那么是三条线段组成一个等腰三角形(包括等边)的共有()个. #include <iostream> using namespace std; int main() { int a = 0; int b = 0; int c = 0; int time = 0; for (int i = 100; i <= 999;i++) { c = i % 10; b = (i - c) / 10 % 10; a = i / 100; if (a==0||b==0||c==0) { continue; } if ((a == b && (a + b>c)) || (b == c && (c + b>a)) || (c == a && (a + c>b))) { time++; } } cout << time << endl; return 0; }
function tri(){ var arr1 = []; for(var i = 100; i<=999; i++){ arr1.push(i) }; return arr1.filter((item)=>{ var aa = String(item).split(''); aa.sort((a,b)=>{return b-a}); return (String(item).indexOf('0')==-1&&parseInt(aa[0])<(parseInt(aa[1])+parseInt(aa[2]))&&((aa[0]==aa[1])||(aa[2]==aa[1]))) }).length; }
#include <stdio.h> int main() { int i=100; int one=0,ten=0,hand=1; int ang=0,bottom=0; int num=0; for(i=100;i<1000;i++){ hand = i / 100; ten = (i % 100) / 10; one = i % 10; if(hand == ten) { ang = hand; bottom = one; } else if(hand == one) { ang = hand; bottom = ten; } else if(ten == one) { ang = ten; bottom = hand; } else continue; if(ang * 2 > bottom && bottom > 0) num ++; } printf("%d",num); return 0; }
则a=b=c=1,2,3,4,5,6,7,8,9,此时n有9个
再考虑等腰三角形情况,若a,b是腰,则a=b
当a=b=1时,c<a+b=2,则c=1,与等边三角形情况重复;
当a=b=2时,c<4,则c=1,3(c=2的情况等边三角形已经讨论了),此时n有2个;
当a=b=3时,c<6,则c=1,2,4,5,此时n有4个;
当a=b=4时,c<8,则c=1,2,3,5,6,7,有6个;
当a=b=5时,c<10,有c=1,2,3,4,6,7,8,9,有8个;
由加法原理知n有2+4+6+8+8+8+8+8=52个
同理,若a,c是腰时,c也有52个,b,c是腰时也有52个
所以n共有9+3×52=165个