#include <stdio.h>
#include <stdlib.h>
int main()
{
static char char_value[3][6][4][5] = {
{
{
{0,0,0,0,0}, // 0,0,0,0-4
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0} // 0,0,3,0-4
},
{
{0,0,0,0,0}, // 0,1,0,0-4
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0} // 0,1,3,0-4
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}
},
{ // 1,0,0,0
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{ // 1,1,0,0
{0,0,0,0,0},
{0,' ',0,0,0}, // 1,1,1,1 ' '
{0,0,0,0,0},
{0,0,0,0,0}
},
{ // 1,2,0,0
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,'A',0}, // 1,2,2,3 'A'
{0,0,0,0,'x'} // 1,2,3,4 'x'
},
{ // 1,3,0,0
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0xf3,0,0}, //1,3,2,2 0xf3
{0,0,0,0,0}
},
{ // 1,4,0,0
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,'\n',0}, // 1,4,2,3 '\n'
{0,0,0,0,0}
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}
},
{ // 2,0,0,0
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{ // 2,1,0,0
{0,0,0,0,0},
{0,0,0320,0,0}, // 2,1,1,2 0320
{0,0,0,0,0},
{0,0,0,0,0}
},
{ // 2,2,0,0
{0,0,0,0,0},
{0,'0',0,0,0}, // 2,2,1,1 '0'
{0,0,'\'',0,0}, // 2,2,2,2 '\''
{0,'\121',0,0,0} //2,2,3,1 '\121'
},
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
},
{ // 2,4,0,0
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,'3',3,0} //2,4,3,2 '3' 2,4,3,3 3
},
{ // 2,5,0,0
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,125}
}
}
};
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 6; ++j)
{
for (int k = 0; k < 4; ++k)
{
for (int l = 0; l < 6; ++l)
{
printf("%x, ", char_value[i][j][k][l]); //打印16进制
}
printf("\n----------------------------------\n");
}
printf("\n+++++++++++++++++++++++++++++++\n");
}
printf("\n***************************\n");
}
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LEN 5
static float st_fTaxlevel[ARRAY_LEN] = {0,23350, 56500, 117950, 256500};
static float st_fTaxPercent[ARRAY_LEN] = {0.15, 0.28, 0.31, 0.36, 0.396};
float single_tax( float income )
{
float fTax = 0.0;
for (int i = ARRAY_LEN-1 ; i >= 0; --i)
{
if (income > st_fTaxlevel[i])
{
fTax = fTax + (income - st_fTaxlevel[i]) * st_fTaxPercent[i];
income = st_fTaxlevel[i];
}
}
return fTax;
}
int main()
{
printf("%f\n", single_tax( 256600));
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
bool identity_matrix(bool iArray[10][10])
{
bool bRet = true;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
if (i == j)
bRet = bRet && (iArray[i][j] == 1);
else
bRet = bRet && (iArray[i][j] == 0);
if (!bRet)
return bRet;
}
}
return bRet;
}
int main()
{
bool iArray[10][10];
// 单位矩阵
#if 1
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
if (i == j)
iArray[i][j] = 1;
else
iArray[i][j] = 0;
}
}
#endif
// 随机矩阵
#if 0
srand(time(NULL));
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
iArray[i][j] = rand() % 2;
}
}
#endif
printf("%d\n", identity_matrix(iArray));
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
// iArray指针表示二维数组
// iLen表示矩阵一边的长,如: 上一题的10 * 10的矩阵,iLen = 10
bool identity_matrix(bool **iArray, int iLen)
{
bool bRet = true;
for (int i = 0; i < iLen; ++i)
{
for (int j = 0; j < iLen; ++j)
{
if (i == j)
bRet = bRet && (iArray[i][j] == 1);
else
bRet = bRet && (iArray[i][j] == 0);
if (!bRet)
return bRet;
}
}
return bRet;
}
int main()
{
bool iArray[10][10];
// 单位矩阵
#if 1
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
if (i == j)
iArray[i][j] = 1;
else
iArray[i][j] = 0;
}
}
#endif
// 随机矩阵
#if 0
srand(time(NULL));
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
iArray[i][j] = rand() % 2;
}
}
#endif
printf("%d\n", identity_matrix(iArray, 10));
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
void matrix_multiply( int *m1, int *m2, int *r, int x, int y, int z )
{
// 不做入参检查,如果数组越界,那就段错误程序崩
// 方案1: 用一维数组的方式表示二维数组
// m1的长度x * y = 3 * 2
// m2的长度y * z = 2 * 4
// r 的长度x * z = 3 * 4
for (int i = 0; i < x; ++i) // 表示行
{
for (int j = 0; j < z; ++j) // 表示列
{
r[i * z + j] = 0; // 初始化
for (int k = 0; k < y; ++k) // 表示和的值
{
int iM1 = m1[i * y + k];
int iM2 = m2[k * z + j];
r[i * z + j] = r[i * z + j] + iM1 * iM2;
}
}
}
// 方案2: 先转成二维数据计算,在将二维数据转换成一维数组返回 (略)
}
int main()
{
int m1[6] = {
2, -6,
3, 5,
1, -1
};
int m2[8] = {
4, -2, -4, -5,
-7, -3, 6, 7
};
int r[12];
matrix_multiply( m1, m2, r, 3, 2, 4 );
for (int i = 0; i < 3; ++i) // 表示行
{
for (int j = 0; j < 4; ++j) // 表示列
{
printf("%d, ", r[i * 4 + j]);
}
printf("\b\b \n"); //退2格再换行 (\b表示退格但是不会删除,\b + 空格,用空格覆盖掉 表示删除)
}
system("pause");
return EXIT_SUCCESS;
}
// 第6、7题答案
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int array_offset(int array_info[], ...)
{
int dimension = array_info[0]; // arrayinfo[0]指定伪数组具有的维数,它的值必须在1和10之间。
//超出维度范围直接返回-1
if(1 > dimension || 10 < dimension)
{
return -1;
}
int now;//当前维度
int offset;//偏移量
int index;//当前下标
va_list arg;
va_start(arg, array_info);
//每次进两位,有上下限两个数
for(now = 1, offset = 0; now <= dimension * 2; now += 2){
//获取一个下标值
index = va_arg(arg, int);
//和对应的array_info中上下限对比,超过返回-1;
if(index < array_info[now] || index > array_info[now + 1]){
return -1;
}
//之前一维度的偏移量 乘以 维度 加上当前维度的偏移量
offset = offset * (array_info[now + 1] - array_info[now] + 1) + index - array_info[now];
}
va_end(arg);
return offset;
}
#if 0
int main()
{
int array_info[] = {3, 4, 6, 1, 5, -3, 3};
printf("%d\n", array_offset(array_info, 4, 1, -3));
printf("%d\n", array_offset(array_info, 4, 1, 3));
printf("%d\n", array_offset(array_info, 5, 1, -3));
printf("%d\n", array_offset(array_info, 4, 1, -2));
printf("%d\n", array_offset(array_info, 4, 2, -3));
printf("%d\n", array_offset(array_info, 6, 3, 1));
system("pause");
return 0;
}
#endif // 第六题答案
// 第七题答案
int array_offset2(register int *arrayinfo, ...) {
register int ndim;
register int offset;
register int hi, lo;
register int *sp;
int s[10];
va_list subscripts;
ndim = *arrayinfo++;//维数
if (ndim >= 1 && ndim <= 10) {
//取出坐标到s数组
va_start(subscripts, arrayinfo);
for(offset = 0; offset < ndim; offset+=1) {
s[offset] = va_arg(subscripts, int);
}
va_end(subscripts);
//从后往前计算每一维的偏移
offset = 0;
arrayinfo += (ndim * 2);//指向数组尾部
sp = s + ndim;//指向最后一个下标
while(ndim-- >=1 ) {
hi = *--arrayinfo;
lo = *--arrayinfo;
if(*--sp > hi || *sp < lo) { return -1;}
offset *= (hi-lo + 1);
offset += (*sp -lo);
}
return offset;
}
return -1;
}
int main()
{
int array_info[] = {3, 4, 6, 1, 5, -3, 3};
printf("%d\n", array_offset2(array_info, 4, 1, -3));
printf("%d\n", array_offset2(array_info, 4, 2, -3));
printf("%d\n", array_offset2(array_info, 4, 1, -1));
printf("%d\n", array_offset2(array_info, 5, 1, -3));
printf("%d\n", array_offset2(array_info, 4, 3, -3));
printf("%d\n", array_offset2(array_info, 5, 3, -1));
printf("%d\n", array_offset2(array_info, 6, 1, -3));
printf("%d\n", array_offset2(array_info, 4, 1, -2));
printf("%d\n", array_offset2(array_info, 6, 5, 3));
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define QUEEN_COUNT 8
static int st_iTotal = 0; // 用于记录符合8皇后的放置方法的个数
bool IsPlacedQueen(int *pQueens, int iQueenExistCnt, int iQueenCol) // 判断能否放置皇后
{
for (int i = 1; i < iQueenExistCnt; ++i) // 行列坐标从1开始比较好计数
{
if (pQueens[i] == iQueenCol ||
pQueens[i] - i == iQueenCol - iQueenExistCnt ||
pQueens[i] + i == iQueenCol + iQueenExistCnt )
{
return false;
}
}
return true;
}
void AddQueen(int *pQueens, int iQueenExistCnt)
{
if (iQueenExistCnt > QUEEN_COUNT)
{
++st_iTotal;
// 打印皇后的位置
for (int i = 1; i <= QUEEN_COUNT; ++i)
{
for (int j = 1; j <= QUEEN_COUNT; ++j)
printf("%d ", (pQueens[j] == j ? 1 : 0));
printf("\n");
}
printf("\n");
}
else
{
for (int i = 1; i <= QUEEN_COUNT; ++i)
{
if (IsPlacedQueen(pQueens, iQueenExistCnt, i))
{
pQueens[iQueenExistCnt] = i;
AddQueen(pQueens, iQueenExistCnt + 1);
}
}
}
}
int main()
{
int iQueens[10] = {0};
AddQueen(iQueens, 1);
printf("%d\n", st_iTotal);
system("pause");
return EXIT_SUCCESS;
}