Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

C language programming learning 8

1.编程统计候选人的得票数。设有3个候选人zhang、li、wang(候选人姓名不区分大小写),10个选民,选民每次输入一个得票的候选人的名字,若选民输错候选人姓名,则按废票处理。选民投票结束后程序自动显示各候选人的得票结果和废票信息。要求用结构体数组candidate表示3个候选人的姓名和得票结果。
例如:
Input vote 1:li
Input vote 2:li
Input vote 3:Zhang
Input vote 4:wang
Input vote 5:zhang
Input vote 6:Wang
Input vote 7:Zhang
Input vote 8:wan
Input vote 9:li
Input vote 10:lii
Election results:
      li:3
   zhang:3
    wang:2
Wrong election:2

#include<stdio.h>
 
#include<string.h>
 
#define TheNumberOfTheELECTORATE 10
 
#define TheNumberOfTheCANDIDATE 3
 
struct candidate
 
{
 
char name[20];
 
int count;
 
}CANDIDATE[3]={"li",0,"zhang",0,"wang",0};
 
int main()
 
{
 
int i,j,flag=1,wrong=0;
 
char name[20];
 
for(j=1;j<=TheNumberOfTheELECTORATE;j++)
 
{
 
printf("Input vote %d:",j);
 
scanf("%s",name);
 
strlwr(name);
 
flag=1;
 
for(i=0;i<TheNumberOfTheCANDIDATE;i++)
 
{
 
if(strcmp(name,CANDIDATE[i].name)==0)
 
{
 
CANDIDATE[i].count++;
 
flag=0;
 
}
 
}
 
if(flag)
 
{
 
wrong++;
 
flag=0;
 
}
}
printf("Election results:\n");
for(i=0;i<TheNumberOfTheCANDIDATE;i++)
 
{
 
printf("%8s:%d\n",CANDIDATE[i].name,CANDIDATE[i].count); }
printf("Wrong election:%d\n",wrong);
 
return 0;
 
}

 

C language programming learning 7

1.

//编程实现找出字符串中最大字符元素并输出该元素及其对应的ASCII值.
//****要求输入提示信息为:
//"Input a string:\n"
//****输出格式要求为:
//"The largest character of \"%s\" is \'%c\' ,The ASCII is %d."
#define N 100
#include<stdio.h>
#include<string.h>
main()
{

    printf("Input a string:\n");
    char c[N];
    gets(c);
    char d=c[0];
    int i;
    for(i=1;i<=strlen(c);i++)
    {
        if(d<c[i])
            d=c[i];
    }
    printf("The largest character of \"%s\" is \'%c\' ,The ASCII is %d.",c,d,d);
}

2.

按如下函数原型编程计算并输出n×n阶矩阵的转置矩阵。其中,n由用户从键盘输入。已知n值不超过10。
void Transpose(int (*a)[N], int n);
void  Swap(int *x, int *y);
void InputMatrix(int (*a)[N], int n);
void PrintMatrix(int (*a)[N], int n);
输入提示信息:"Input n:"
输入格式:"%d"
输入提示信息:"Input %d*%d matrix:\n"
输出提示信息:"The transposed matrix is:\n"
输出格式:"%d\t"

#include <stdio.h>
#define N 10
void  Swap(int *x, int *y);
void Transpose(int (*a)[N], int n);
void InputMatrix(int (*a)[N], int n);
void PrintMatrix(int (*a)[N], int n);
int main()
{                
    int s[N][N], n;
    printf("Input n:");
    scanf("%d", &n);
    InputMatrix(s, n);
    Transpose(s, n);
    printf("The transposed matrix is:\n");
    PrintMatrix(s, n);
    return 0;
}                
/* 函数功能:交换两个整型数的值 */
void  Swap(int *x, int *y)
{                
    int  temp;
    temp = *x;
    *x = *y;
    *y = temp;
}                
/* 函数功能:计算n*n矩阵的转置矩阵 */
void Transpose(int (*a)[N], int n)
{                
    int i, j;
    for (i = 0; i < n; i++)
    {                
        for (j = i; j < n; j++)
        {                
            Swap(*(a + i) + j, *(a + j) + i);
        }
    }
}                
/* 函数功能:输入n*n矩阵的值 */
void InputMatrix(int (*a)[N], int n)
{                
    int i, j;
    printf("Input %d*%d matrix:\n", n, n);
    for (i = 0; i < n; i++)
    {                
        for (j = 0; j < n; j++)
        {                
            scanf("%d", *(a + i) + j);
        }
    }
}                
/* 函数功能:输出n*n矩阵的值 */
void PrintMatrix(int (*a)[N], int n)
{                
    int i, j;
    for (i = 0; i < n; i++)
    {                
        for (j = 0; j < n; j++)
        {                
            printf("%d\t", *(*(a + i) + j));
        }
        printf("\n");
    }
}           

 

我的代码

#include<stdio.h>
#define N 10
void Transpose(int (*a)[N], int n)
{

    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            Swap(*(a+i)+j,*(a+j)+i);
        }
    }

}

void  Swap(int *x, int *y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;

}
void InputMatrix(int (*a)[N], int n)
{
    printf("Input %d*%d matrix:\n",n,n);
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {

            scanf("%d",*(a+i)+j);
        }
    }

}
void PrintMatrix(int (*a)[N], int n)
{
    int i,j;

    for(i=0; i<n; i++)
    {

        for(j=0; j<n; j++)
        {
            printf("%d\t",*(*(a+i)+j));
        }
        printf("\n");
    }
}
main()
{
    int n=100,s[N][N]= {0};

    printf("Input n:");
    do
    {

        scanf("%d",&n);

    }
    while(n>10);

    InputMatrix(s,n);
    Transpose(s,n);

    printf("The transposed matrix is:\n");

    PrintMatrix(s,n);
    return 0;
}

我个人认为,我的代码比答案还要简单,因为减少了一步不必要的计算,因此,运算时间会短一些。效率会高些!

注意:在Transpose的过程中,一定要注意一个关键性的问题

for(j=i; j<n; j++)

或者从i+1开始(i+1)效率更高,减少不必要的运算

mmp,我就是让j从0开始结果找这个错误找了一个小时,好奇啊。如果j从0开始的话,那么就相当于先把i,j互换,然后再换回来,等于说换了两次,相当于没换。

其实如果要直接输出转置矩阵,还有更简便的做法:

即:输出先j后i!无论是代码量还是计算量,都少了一大截!!!

 

#include<stdio.h>
#define N 10

void InputMatrix(int (*a)[N], int n)
{
    printf("Input %d*%d matrix:\n",n,n);
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {

            scanf("%d",*(a+i)+j);
        }
    }

}
void PrintMatrix(int (*a)[N], int n)
{
    int i,j;

    for(i=0; i<n; i++)
    {

        for(j=0; j<n; j++)
        {
            printf("%d\t",*(*(a+j)+i));
        }
        printf("\n");
    }
}
main()
{
    int n=100,s[N][N]= {0};

    printf("Input n:");
    do
    {

        scanf("%d",&n);

    }
    while(n>10);
    InputMatrix(s,n);
    printf("The transposed matrix is:\n");
    PrintMatrix(s,n);
    return 0;
}

 


题外话:

       有句mmp今天一定要讲。这种题出的简直就是多此一举

因为

我们可以认为:
int (*a)[]=int a [][]
他直接说定义一个二维数组多好

非得写这么恶心的东西的干嘛

可能他有点蛋疼
.....
因为数组作为参数类型退化成指针
int a[]写成int *a是一个意思
这跟C语言对数组的实现有关
二维数组要求第二个维度给出长度,这是因为编译器算偏移要用这个
因为你取数组里面一个元素实际上是解引用一个指针
这个指针是base+offset算出来的
 
int a[100]
a[4]等价于 *(a+4)
由加法交换性,4[a]等价于a[4],就是你可以把a[4]写成4[a]
------------------------------------------------------------------
大佬告诉我
毕竟C是一种比较低级的语言
像函数式语言是没指针的
甚至没有变量
感兴趣的话可以去玩玩haskell,这个比较典型
------------------------------------------------------------------
[]优先级比*高
int b[4][4]={0};
int (*a)[4]=b;

int b[4][4]={0};
int *a[4];
a[0]=b[0];
就是说
int *a[n]是指针数组(也就是说其本质上是数组,但里面存放的都是指针)
int (*a)[n]是数组指针(本质上是一个指针,而且是一个指向数组的指针)

第一部分:

int a[4]

a的类型是int[4],a[0]的类型是int;a是指向int[4]的指针

int (*a)[4]=b;指的是a指向的是int [4],然后把b的第一行地址赋给他

就是直接拷贝了首元素地址

第二部分很好理解

就是把b[0]的首行地址给a[4];

其实这种题完全可以用数组的方法做,看起来更加简洁,效率也几乎一样

#include<stdio.h>
#include<string.h>
#define N 4
int convertmatrix(int m[N][N])
{
    int i,j,temp;
    for(i=0;i<N;i++)
    {
        for(j=i+1;j<N;j++)
        {
            temp=m[i][j];
            m[i][j]=m[j][i];
            m[j][i]=temp;
        }
    }
    return 0;
}
int main()
{
    int matrix[N][N];
    int i,j;
    printf("请输入一个%d*%d的矩阵:\n",N,N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }
    convertmatrix(matrix);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%-3d",matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}

用指针编简直就是在浪费时间

所以。。。。

还有一种做法:

#include<stdio.h>
#include<string.h>
#define N 4
int convertmatrix(int m[N][N])
{
    int i,j,temp;
    for(i=0;i<N;i++)
    {
        for(j=i+1;j<N;j++)
        {
            temp=m[i][j];
            m[i][j]=m[j][i];
            m[j][i]=temp;
        }
    }
    return 0;
}
int main()
{
    int matrix[N][N];
    int i,j;
    printf("请输入一个%d*%d的矩阵:\n",N,N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }
    convertmatrix(matrix);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%-3d",matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}

另外一个比较类似的问题:

用指向一维数组的指针变量即二维数组的行指针作为函数参数,实现矩阵转置。按如下函数原型编程计算并输出m×n阶矩阵的转置矩阵。其中,m和n的值由用户从键盘输入。已知m和n的值都不超过10。

void Transpose(int (*a)[N], int (*at)[M], int m, int n);

void InputMatrix(int (*a)[N], int m, int n);

void PrintMatrix(int (*at)[M], int n, int m);

输入提示信息:"Input m, n:"

输入格式:"%d,%d"

输入提示信息:"Input %d*%d matrix:\n"

输出提示信息和格式:"The transposed matrix is:\n"

输出格式:"%d\t"

#include<stdio.h>

#define M 10

#define N 10

void Transpose(int (*a)[N], int (*at)[M], int m, int n);

void InputMatrix(int (*a)[N], int m, int n);

void PrintMatrix(int (*at)[M], int n, int m);

int main()

{

    int s[M][N],st[N][M],m,n;

    printf("Input m, n:");

    scanf("%d,%d",&m,&n);

    InputMatrix(s,m,n);

    Transpose(s,st,m,n);

    printf("The transposed matrix is:\n");

    PrintMatrix(st,n,m);

    return 0;

}

void Transpose(int (*a)[N], int (*at)[M], int m, int n)

{

    int i,j;

    for(i=0;i<m;i++)

    {

        for(j=0;j<n;j++)

        {

            *(*(at+j)+i)=*(*(a+i)+j);

        }

    }

}

void InputMatrix(int (*a)[N], int m, int n)

{

    int i,j;

    printf("Input %d*%d matrix:\n",m,n);

    for(i=0;i<m;i++)

    {

        for(j=0;j<n;j++)

        {

            scanf("%d",*(a+i)+j);

        }

    }

}

void PrintMatrix(int (*at)[M], int n, int m)

{

    int i,j;

    for(i=0;i<n;i++)

    {

        for(j=0;j<m;j++)

        {

            printf("%d\t",*(*(at+i)+j));

        }

        printf("\n");

    }

}

3.

从键盘任意输入一个整型表示的月份值,用指针数组编程输出该月份的英文表示,若输入的月份值不在1~12之间,则输出“Illegal month”。

**输入格式要求:"%d"  提示信息:"Input month number:"

**输出格式要求:"month %d is %s\n"

"Illegal month", "January", "February", "March", "April", "May", "June", "July", August", "September", "October", "November", "December"

程序运行示例1如下:

Input month number:5

month 5 is May

程序运行示例2如下:

Input month number:13

Illegal month

#include<stdio.h>

int main()

{

    int n;

    static char*monthName[]={"Illegal month", "January", "February", "March", "April", "May", "June", "July", “August", "September", "October", "November", "December"};

    printf("Input month number:");

    scanf("%d",&n);

    if((n<=12)&&(n>=1))

    printf("month %d is %s\n",n,monthName[n]);

    else

    printf("%s\n",monthName[0]);

    return 0;

}

4.

•编程计算2×3阶矩阵A和3×2阶矩阵B之积C。
•矩阵相乘的基本方法是:
•矩阵A的第i行的所有元素同矩阵B第j列的元素对应相乘,
•并把相乘的结果相加,最终得到的值就是矩阵C的第i行第j列的值。
•要求:
•(1)从键盘分别输入矩阵A和B,
•  输出乘积矩阵C
•(2)
•**输入提示信息为:
•输入矩阵A之前提示:"Input 2*3 matrix a:\n"
•输入矩阵B之前提示:"Input 3*2 matrix b:\n"
•**输入矩阵中每个值的格式为:"%d"
•**输出格式为:
•输出矩阵C之前提示:"Results:\n"
•输出矩阵C中每个值的格式:"%6d"
•输出矩阵C各行结束时换行
#include <stdio.h>
#define  ROW 2
#define  COL 3
void main()
{
	int a[ROW][COL], b[COL][ROW], c[ROW][ROW], i, j,k;

	printf("Input 2*3 matrix a:\n");
	for (i=0; i<ROW ;i++)
	{
		for (j=0; j<COL; j++)
		{
			scanf("%d", &a[i][j]);
		}
	}
	printf("Input 3*2 matrix b:\n");
	for (i=0; i<COL; i++)
	{
		for (j=0; j<ROW; j++) 
		{
			scanf("%d",  &b[i][j]);
		}
	}
	for (i=0; i<ROW; i++)
	{
		for (j=0; j<ROW; j++)
		{ 
			c[i][j] =   0 ;
			for (k=0; k<COL; k++)
			{
				c[i][j] = c[i][j]+a[i][k]*b[k][j] ;
			}
		}
	}
	printf("Results:\n");
	for (i=0; i<ROW; i++)
	{
		for (j=0; j<ROW; j++) 
		{
			printf("%6d", c[i][j]);
		}
		printf("\n") ;
	}
}

本次内容到此全部结束

ACM practice 1

1.

The Sarcophagus itself is locked by a secret numerical code. When somebody wants to open it, he must know the code and set it exactly on the top of the Sarcophagus. A very intricate mechanism then opens the cover. If an incorrect code is entered, the tickets inside would catch fire immediately and they would have been lost forever. The code (consisting of up to 100 integers) was hidden in the Alexandrian Library but unfortunately, as you probably know, the library burned down completely.

But an almost unknown archaeologist has obtained a copy of the code something during the 18th century. He was afraid that the code could get to the ``wrong people'' so he has encoded the numbers in a very special way. He took a random complex number B that was greater (in absolute value) than any of the encoded numbers. Then he counted the numbers as the digits of the system with basis B. That means the sequence of numbers an, an-1, ..., a1, a0 was encoded as the number X = a0 + a1B + a2B2 + ...+ anBn.

Your goal is to decrypt the secret code, i.e. to express a given number X in the number system to the base B. In other words, given the numbers X and Byou are to determine the ``digit'' a0 through an.

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case consists of one single line containing four integer numbers Xr, Xi, Br, Bi (|Xr|,|Xi| <= 1000000, |Br|,|Bi| <= 16). These numbers indicate the real and complex components of numbers X and B, i.e. X = Xr + i.Xi, B = Br + i.Bi. B is the basis of the system (|B| > 1), X is the number you have to express.

Output

Your program must output a single line for each test case. The line should contain the ``digits'' an, an-1, ..., a1, a0, separated by commas. The following conditions must be satisfied:

  • for all i in {0, 1, 2, ...n}: 0 <= ai < |B|
  • X = a0 + a1B + a2B2 + ...+ anBn
  • if n > 0 then an <> 0
  • n <= 100

If there are no numbers meeting these criteria, output the sentence "The code cannot be decrypted.. If there are more possibilities, print any of them.

Sample Input

4

-935 2475 -11 -15

1 0 -3 -2

93 16 3 2

191 -192 11 -12

Sample Output

8,11,18

1

The code cannot be decrypted.

16,15