日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

符号三角形

發(fā)布時(shí)間:2024/10/5 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 符号三角形 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

符號(hào)三角形的 第1行有n個(gè)由“+”和”-“組成的符號(hào) ,以后每行符號(hào)比上行少1個(gè),2個(gè)同號(hào)下面是”+“,2個(gè)異 號(hào)下面是”-“ 。計(jì)算有多少個(gè)不同的符號(hào)三角形,使其所含”+“ 和”-“ 的個(gè)數(shù)相同 。 n=7時(shí)的1個(gè)符號(hào)三角形如下:?
+ + - + - + +?
+ - - - - +?
- + + + -?
- + + -?
- + -?
- -?
+?

Input

每行1個(gè)正整數(shù)n <=24,n=0退出.?

Output

n和符號(hào)三角形的個(gè)數(shù).?

Sample Input

15 16 19 20 0

Sample Output

15 1896 16 5160 19 32757 20 59984

這道題應(yīng)該用深搜去構(gòu)造頂層,然后推算出其他的層,在推算的同時(shí)進(jìn)行給其中一種符號(hào)的計(jì)數(shù),最后判斷該符號(hào)的數(shù)是不是總符號(hào)數(shù)的一半,剩下的一些細(xì)節(jié)(諸如符號(hào)總數(shù)是否可以整除2、如何在輸入0時(shí)結(jié)束程序等)我就不解釋了,不過(guò)代碼里的注釋中會(huì)有。

C++版本一

DFS

#include<iostream> using namespace std; int n,total,sum; int word[30][30]; //存儲(chǔ)符號(hào)的數(shù)組 void wxy() //函數(shù)名沒(méi)有含義 {int x=n,y=0; //x用于枚舉層數(shù),y用于計(jì)算其它層負(fù)號(hào)個(gè)數(shù)while(x--) //枚舉層數(shù)for(int i=1;i<=x;i++){word[x][i]=(word[x+1][i]+word[x+1][i+1])%2; //定義第n-x+1層的第i個(gè)符號(hào)if(word[x][i]) y++; //若word[x][i]為負(fù)號(hào),其它層負(fù)號(hào)個(gè)數(shù)加1}if(sum+y==n*(n+1)/2/2) total++; //若負(fù)號(hào)的個(gè)數(shù)為符號(hào)總數(shù)的一半,情況數(shù)加1(運(yùn)用了等差數(shù)列) } void dfs(int x) {for(int i=0;i<2;i++) //0為正號(hào),1為負(fù)號(hào){if(i) sum++; //給題目中頂層的負(fù)號(hào)計(jì)數(shù)word[n][x]=i; //定義頂層的第x個(gè)符號(hào)是正還是負(fù)if(x==n) wxy(); //若頂層的所有符號(hào)定義完畢,計(jì)算其它層的負(fù)號(hào)個(gè)數(shù)else dfs(x+1); //定義頂層的第x+1個(gè)符號(hào)if(i) sum--; //回溯} } main() {while(cin>>n&&n) //輸入n,判斷n為不為0{cout<<n<<" ";if((n*(n+1)/2)%2) {cout<<"0"<<endl;continue;} //判斷符號(hào)總數(shù)是否可以整除2,(n*(n+1)/2是運(yùn)用等差數(shù)列來(lái)算總數(shù)的)dfs(1);cout<<total<<endl;total=sum=0;} }

C++版本二

水題

#include <iostream> #include <stdio.h> #include <queue> #include <string.h> #include <math.h> using namespace std; int n; int a[30][30];int main() {while(scanf("%d",&n)!=EOF){if(n==0) break;//int ans=0;cout <<n<<" ";switch (n){case 3:cout <<"4"<< endl;break;case 4:cout <<"6"<< endl;break;case 7:cout <<"12"<< endl;break;case 8:cout <<"40"<< endl;break;case 11:cout <<"171"<< endl;break;case 12:cout <<"410"<< endl;break;case 15:cout <<"1896"<< endl;break;case 16:cout <<"5160"<< endl;break;case 19:cout <<"32757"<< endl;break;case 20:cout <<"59984"<< endl;break;case 23:cout <<"431095"<< endl;break;case 24:cout <<"822229"<< endl;break;default :cout <<"0"<< endl;break;}}//cout << "Hello world!" << endl;return 0; }

C++版本三

這個(gè)應(yīng)該會(huì)OLE

#include <iostream> #include <stdio.h> #include <queue> #include <string.h> #include <math.h> using namespace std; int n; int a[30][30];int main() {while(scanf("%d",&n)!=EOF){if(n==0) break;int ans=0;for(int i=0;i<1<<n;i++){int cnt1=0,cnt0=0;for(int j=0;j<n;j++){a[0][j]=i>>j & 1;if( a[0][j]==1) cnt1++;if( a[0][j]==0) cnt0++;//cout << a[0][n-j-1];}for(int k=1;k<n;k++){for(int j=0;j<n-k;j++){if(a[k-1][j]==a[k-1][j+1]){a[k][j]=1;cnt1++;}else{a[k][j]=0;cnt0++;}//cout << a[0][n-j-1];}//cout<< endl;}// for(int k=0;k<n;k++){ // for(int j=0;j<n-k;j++){ // // if( a[k][j]==1) cnt1++; // if( a[k][j]==0) cnt0++; // } // //cout<< endl; // }if(cnt0==cnt1) ans++;//cout<< endl;}cout <<n<<" " <<ans<< endl;}cout << "Hello world!" << endl;return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的符号三角形的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。