1.杨辉三角问题
#include <stdio.h>
#include <stdlib.h>
const int length = 10; // 确定杨辉三角的高度
int main()
{
int nums[length][length];
int i, j;
//计算
for(i=0; i<length; i++)
{
nums[i][0] = 1; //每行首尾为零
nums[i][i] = 1;
for(j=1; j<i; j++) //每行需要计算的数只有N-2个
nums[i][j] = nums[i-1][j-1] + \
nums[i-1][j]; //每行中间的数等于上一行\
左右右两个数之和
}
//打印
for(i=0; i<length; i++)
{
for(j=0; j<length-i-1; j++)
printf(" ");
for(j=0; j<=i; j++)
printf("%-5d ", nums[i][j]);
putchar('\n');
}
return 0;
}
2.五名运动员跳水问题
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int n = 0;
int flag = 0;
for(a = 1; a<=5; a++)
{
for(b = 1; b<=5; b++)
{
for(c = 1; c<=5; c++)
{
for(d = 1; d<=5; d++)
{
for(e = 1; e<=5; e++)
{
if(((b==1)+(a==3)==1) //每个人的话只有半句是真的
&&((b==2)+(e==4)==1)
&&((c==1)+(d==2)==1)
&&((c==5)+(d==3)==1)
&&((e==4)+(a==1)==1))
{
//flag = 0;
n |= (1<<(a-1));
n |= (1<<(b-1)); // 左移一位与0按位或
n |= (1<<(c-1));
n |= (1<<(d-1));
n |= (1<<(e-1));
if(n==1||n==3||n==7||n==15||n==31)
printf("a = %d b = %d c = %d d = %d e = %d\n",a,b,c,d,e);
n=0;
}
}
}
}
}
}
return 0;
}
3.谁是嫌疑犯
#include<stdio.h>
int main()
{
int killer=0;
for (killer = 'A'; killer <= 'D'; killer++) //凶手在A,B,C,D中
{
if ((killer != 'A') + (killer == 'C') + (killer =='D') + (killer != 'D') == 3)
//满足三真一假的killer为真正凶手
{
printf("killer=%c\n", killer); //输出凶手的字母
return 0;
}
}
return 1;
}