8個整數形成奇特的立方體

來源:互聯網
上載者:User

由8個整數形成奇特的立方體任意給出8個整數,將這8個整數分別放在一個立方體的八個頂點上,要求每個面上的四個數之和相等。*問題分析與演算法設計簡化問題:將8個頂點對應數組中的8個元素,將“每個面上的四個數之和皆相等”轉換為數組無素之間和的相等關係。這裡的關鍵在於正確地將立方體的8個頂點與數組的8個元素對應。可以利用簡單的窮舉方法建立8個數的全部排列。

#include <stdio.h>#include <stdlib.h>int main(){int a[9],ii=0,i,a1,a2,a3,a4,b1,b2,b3,b4,flag;for(i=1;i<=8;i++)//輸入8個整數{printf("Please enter number [%d]:",i);scanf("%d",&a[i]);ii+=a[i];}printf("************************************************\n");if(ii%2)//和為奇數則輸入的8個數不可用{printf("Sorry they can't be constructed required cube!\n");exit(0);}for(flag=0,a1=1;a1<=8;a1++)//flag:完成標記,flag=1:表示完成for(a2=1;a2<=8;a2++)//採用八重迴圈建立八個整數的全排列for(a3=1;a3<=8;a3++)for(a4=1;a4<=8;a4++)for(b1=1;b1<=8;b1++)for(b2=1;b2<=8;b2++)for(b3=1;b3<=8;b3++)for(b4=1;b4<=8;b4++)if((a2!=a1)&&//前兩個數不能相同(a3!=a2&&a3!=a1)&&//前三個數不能相同(a4!=a3&&a4!=a2&&a4!=a1)&&//前四個數不能相同(b1!=a4&&b1!=a3&&b1!=a2&&b1!=a1)&&//前五個數不能相同(b3!=b2&&b3!=b1&&b3!=a4&&b3!=a3&&b3!=a2&&b3!=a1)&&//前六個數不能相同(b2!=b1&&b2!=a4&&b2!=a3&&b2!=a2&&b2!=a1)&&//前七個數不能相同(b4!=b2&&b4!=b1&&b4!=b3&&b4!=a4&&b4!=a3&&b4!=a2&&b4!=a1)&&//前八個數不能相同(a[b1]+a[b2]+a[b3]+a[b4]==ii/2&&a[a1]+a[a2]+a[b1]+a[b2]==ii/2&&a[a1]+a[a4]+a[b1]+a[b4]==ii/2)){flag=1;goto out;//滿足條件則將flag置1後退出}out:if(flag){printf("They can be constructed required cube as follow:\n");printf("    %2d ───────%2d\n",a[a4],a[a3]);printf("   /│            /│\n");printf(" %2d─┼─────%2d  │\n",a[a1],a[a2]);printf("  │ │          │  │\n");printf("  │ │          │  │\n");printf("  │ │          │  │\n");printf("  │ │          │  │  \n");printf("  │%2d ─────┼─%2d\n",a[b4],a[b3]);printf("  │/           │/\n");printf(" %2d───────%2d\n",a[b1],a[b2]);}else printf("Sorry, they can't be constructed required cube!\n");return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.