1、杨辉三角
# include<iostream>
using namespace std;
int main()
{
int n;
cout<<"请输入杨辉三角的行数n: ";
cin>>n;
int a[n][n];
int i, j;
//先输入最右列和对角线元素1
for (i = 1; i <= n; i++) {//i从1开始
a[i][i] = 1;
a[i][1] = 1;
}
for (i = 3; i <= n; i++) {
for (j = 2; j <= i - 1; j++)
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
2、
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
for(int x = 1; x <=4; x++)//x:凶手
{
if((x!=1)+(x==3)+(x==4)+(x!=4)==3)
cout<<x<<endl;
}
return 0;
}
3、
//问题规模
#include<iostream>
using namespace std;
int main(){
int a, c;
for(a = 0; a <= 20; a++){
for(c = 0; 3*c <= 100; 3*c++){
if(4*c - a == 100&&a+3*c <= 100){
cout<<"公鸡数量:"<<a<<endl<<"母鸡数量:"<<100-a-3*c<<endl<<"小鸡仔数量:"<<3*c<<endl;
cout<<"**************"<<endl;
}
}
}
return 0;
}
4、求X*X为九位数,且各位数都不相同
#include<iostream>
int main( )
{
int i,t,k = 0,num=0;
long x,y1,y2;
int p[10];
for(x = 10000; x <= 31622; x++){
for(i = 0; i <= 9; i++)
p[i] = 1;
y1 = x*x;
y2 = y1;
for(int i = 1; i <= 9; i++){
t = y2%10;
y2 /= 10;
if(p[t] == 1){//y2中各位上是否有与数字t相同的
k++;
p[t] = 0;//下次再遇到相同数字时就 p[t] ≠ 1了
}
else
break;
}
if(k==9){
num++;
cout<<x;
}
}
return 0;
}