1 条题解

  • 0
    @ 2021-6-14 23:25:22

    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;
    } 
    

    Pascal :

    var  p,a,b,c,d,e,f,g,h,i,s:longint;
    begin
      readln(p);
      for a:=1 to 9 do
       for b:=1 to 9 do
        for c:=1 to 9 do
         for d:=b+1 to 9 do
          for e:=c+1 to 9 do
           for f:=a+1 to 9 do
            for g:=1 to 9 do
             for h:=g+1 to 9 do
              for i:=f+1 to 9 do
               if (a+b+c+d+e+f+g+h+i=45)and(a*b*c*d*e*f*g*h*i=362880)and(a<f)and
                (f<i)and(b<d)and(g<h)and(c<e)and(a+b+d+f=p)and(f+g+h+i=p)and(i+e+c+a=p)
                 then s:=s+1;
      if s>0 then writeln(s) else writeln('Not exist');
    end.
    

    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);
    		}
     
    	}
     
    }
    
    
    • 1

    信息

    ID
    186
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者