2330 - 排三角形
时间限制 : 1 秒
内存限制 : 125 MB
将1,2,······,9共9个数排成下列形态的三角形。
a
b c
d e
f g h i
其中:a~i分别表示1,2,······,9中的一个数字,并要求同时满足下列条件:
(1)a<f<i;
(2)b<d, g<h, c<e
(3)a+b+d+f=f+g+h+i=i+e+c+a=P
程序要求:<span></span>
<span> </span>根据输入的边长之和<span>P,</span><span style="line-height:1.5;">输出所有满足上述条件的三角形的个数。</span>
<span style="line-height:1.5;"><br />
<br />
题目输入
每个测试文件只包含一组测试数据,每组输入一个整数P,表示边长之和。
题目输出
对于每组输入数据,输出所有满足上述条件的三角形的个数。
如果无解,则输出"Not exist"(引号不输出)。
输入/输出样例
输入格式
23
输出格式
2
C语言解答
#include<stdio.h> int main() { int k[9]={1,2,3,4,5,6,7,8,9}; int a,b,c,d,e,f,g,h,i,j,l=0,m,n,o,p,q,r,s,t,u,P; scanf("%d",&P); for(j=0;j<9;j++) { a=k[j]; for(m=0;m<9;m++) if(a!=k[m]) { b=k[m]; for(n=0;n<9;n++) if(k[n]!=a&&k[n]!=b) { c=k[n]; for(o=0;o<9;o++) if(k[o]!=a&&k[o]!=b&&k[o]!=c) { d=k[o]; for(p=0;p<9;p++) if(k[p]!=a&&k[p]!=b&&k[p]!=c&&k[p]!=d) { e=k[p]; for(q=0;q<9;q++) if(k[q]!=a&&k[q]!=b&&k[q]!=c&&k[q]!=d&&k[q]!=e) { f=k[q]; for(r=0;r<9;r++) if(k[r]!=a&&k[r]!=b&&k[r]!=c&&k[r]!=d&&k[r]!=e&&k[r]!=f) { g=k[r]; for(s=0;s<9;s++) if(k[s]!=a&&k[s]!=b&&k[s]!=c&&k[s]!=d&&k[s]!=e&&k[s]!=f&&k[s]!=g) { h=k[s]; for(t=0;t<9;t++) if(k[t]!=a&&k[t]!=b&&k[t]!=c&&k[t]!=d&&k[t]!=e&&k[t]!=f&&k[t]!=g&&k[t]!=h) { i=k[t]; if(a<f&&f<i&&b<d&&g<h&&c<e&&a+b+d+f==f+g+h+i&&f+g+h+i==i+e+c+a&&i+e+c+a==P) l=l+1; } } } } } } } } } if(l==0) printf("Not exist"); else printf("%d",l); return 0; }
C++解答
#include<iostream> #include<algorithm> using namespace std; int main() { int p;int count=0; while (cin>>p) { count=0; int arr[9]; int i; for (i=0;i<9;i++) arr[i]=i+1; while (next_permutation(arr,arr+9)) { if (arr[0]<arr[5] && arr[5]<arr[8]) if (arr[1]<arr[3] && arr[6]<arr[7] && arr[2]<arr[4]) if (arr[0]+arr[1]+arr[3]+arr[5]==p && arr[8]+arr[6]+arr[7]+arr[5]==p && arr[0]+arr[2]+arr[4]+arr[8]==p ) count++; } if (count) cout<<count<<endl; else cout<<"Not exist"<<endl; } return 0; }
Java解答
import java.util.Scanner; import java.util.Vector; public class Main { public static int count = 0; private void dosome(int n, Vector<Integer> re, Vector<Integer> su, int p[]) { // TODO Auto-generated method stub if(re.size() == 0) { if(su.elementAt(0) < su.elementAt(5) && su.elementAt(5) < su.elementAt(8)) if(su.elementAt(3)>su.elementAt(1) && su.elementAt(7)>su.elementAt(6) && su.elementAt(4) > su.elementAt(2)) if(su.elementAt(0)+su.elementAt(1)+su.elementAt(3)+su.elementAt(5) == n && su.elementAt(0)+su.elementAt(2) + su.elementAt(4)+su.elementAt(8) == n && su.elementAt(5)+su.elementAt(6) + su.elementAt(7)+su.elementAt(8) == n) { count++; if(count == 1) for (int i = 0; i < p.length; i++) { p[i] = su.elementAt(i); } } } for (int i = 0; i < re.size(); i++) { Vector<Integer> tre = new Vector<Integer>(re); Vector<Integer> tsu = new Vector<Integer>(su); tsu.add(re.elementAt(i)); tre.remove(i); new Main().dosome(n,tre, tsu, p); } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p[] = new int [9]; Vector<Integer> su = new Vector<Integer>(); Vector<Integer> re = new Vector<Integer>(); for (int i = 1; i < 10; i++) { re.add(i); } new Main().dosome(n, re, su, p); if(count == 0) { System.out.println("Not exist"); } else { System.out.println(Main.count); } } }