要点:
1.比较简单的字符串问题,不解释了
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *a, const void *b)
{
return(*(int*)b - *(int*)a);//快排从大到小输出
}
int main()
{
int m, n,t;
char s[55][1005];
char b[1005];
scanf("%d", &t);
while (t--)
{
int num = 0;
memset(b, 0, sizeof(b));
memset(s, 0, sizeof(s));
scanf("%d%d", &m, &n);
for (int i = 0; i < m; i++)
scanf("%s", s[i]);
for (int j = 0; j < n; j++)
{
int a[4] = {0};
for (int i = 0; i < m; i++)
{
if (s[i][j] == 'A') a[3]++;
if (s[i][j] == 'C') a[2]++;
if (s[i][j] == 'G') a[1]++;
if (s[i][j] == 'T') a[0]++;
}
for (int k = 0; k <4; k++)
a[k] = 1000 * a[k] + k;//使值大小排序原本的数组顺序也可保留
qsort(a, 4, sizeof(a[0]), cmp);
int k = a[0] % 1000;
if (k == 3) b[j] = 'A';
if (k == 2) b[j] = 'C';
if (k == 1) b[j] = 'G';
if (k == 0) b[j] = 'T';
for (int i = 0; i < m; i++)
{
if (s[i][j] != b[j])
num++;
}
}
for (int j = 0; j < n; j++)
{
printf("%c", b[j]);
}
printf("\n");
printf("%d\n", num);
}
return 0;
}
学习心得:
1.a[4]={0}说明都赋值为0,但a[4]={1}不可行,必须a[4]={1,1,1,1}