6-1 谁编程最厉害?
int Max2(int d[],int n){
int a[n];
for(int k=0;k<n;k++){
a[k]=-1;
}
for(int i=0;i<n;i++){
if(d[i]%2==0){
a[i]=d[i];
}
}
int max=-1;
for(int j=0;j<n;j++){
if(a[j]>max){
max=a[j];
}
}
return max;
}
6-2 高效查重
int fun(int n){
int b[n];
for(int i=0;i<n;i++){
b[i]=0;
}
for(int j=0;j<n;j++){
b[getDi(j)]++;
}
for(int k=0;k<n;k++){
if(b[k]>1){
return 1;
}
}
return 0;
}
6-3 高效查单I
int Fun(int n){
static int j=0;
for(int i=0;i<n;i++){
j^=getDi(i);
}
return j;
}
6-4 移除指定元素
int fun (int *A,int N,int val){
int j=0;
for(int i=0;i<N;i++){
if(A[i]!=val){
A[j]=A[i];
j++;
}
}
return j;
}
6-5 字符串高效去噪
#include "stdio.h"
int num=0;
void j2i(char *p,int i,int j);//p[i]<--p[j] 字符移动时必须调用的移动函数(移动并统计移动次数),此处不表
void stringDel(char *a);//函数接口
int main(){
int i,k;scanf("%d",&k);srand(k);
char A[888];gets(A);
stringDel(A);
puts(A);
printf("%d",num);
return 0;
}
/* 请在这里填写答案 */
答案:
void stringDel (char *st ){
int i=0;
int j=0;
for(i=0;st[i]!='\0';i++){
if(st[i]>='0'&&st[i]<='9')
continue;
else if(i==j){
j++;
continue;
}else{
j2i(st,j,i);
j++;
}
}
if(i!=j)
{
st[i]='\0';
j2i(st,j,i);
}else{
st[i]='\0';
}
}
6-6 数组去最值
int delMaxMin(int A[],int n){
int max=0,min=0;
int cnt=0;
for(int i=0;i<n;i++){
if(A[i]>=A[max])
max=i;
if(A[i]<=A[min])
min=i;
}
// printf("%d %d",index_max,index_min);
if(max<min){
for(int i=max+1;i<min;i++){
A[i-1]=A[i];
cnt++;
}
for(int i=min+1;i<n;i++){
A[i-2]=A[i];
cnt++;
}
}
if(max>min){
for(int i=min+1;i<max;i++){
A[i-1]=A[i];
cnt++;
}
for(int i=max+1;i<n;i++){
A[i-2]=A[i];
cnt++;
}
}
return cnt;
}
6-7 日期几何
struct DateG days2Date(int x) {
struct DateG QWQ;
QWQ.yy = 1;
QWQ.mm = 1;
QWQ.dd = 1;
while (x--) {
QWQ.dd++;
if ((QWQ.yy % 4 == 0 && QWQ.yy % 100 != 0) || QWQ.yy % 400 == 0) {//是闰年
if ((QWQ.mm == 1 || QWQ.mm == 3 || QWQ.mm == 5 || QWQ.mm == 7 || QWQ.mm == 8 || QWQ.mm == 10 || QWQ.mm == 12) && QWQ.dd > 31)
{
QWQ.dd = 1;
QWQ.mm++;
if (QWQ.mm > 12) {
QWQ.mm = 1;
QWQ.yy++;
}
}
else if (QWQ.mm == 2 && QWQ.dd > 29) {//闰年二月单独讨论
QWQ.dd = 1;
QWQ.mm++;
}
else if ((QWQ.mm == 4 || QWQ.mm == 6 || QWQ.mm == 9 || QWQ.mm == 11) && QWQ.dd > 30) {
QWQ.dd = 1;
QWQ.mm++;
}
}
else {//非闰年
if ((QWQ.mm == 1 || QWQ.mm == 3 || QWQ.mm == 5 || QWQ.mm == 7 || QWQ.mm == 8 || QWQ.mm == 10 || QWQ.mm == 12) && QWQ.dd > 31)
{
QWQ.dd = 1;
QWQ.mm++;
if (QWQ.mm > 12) {
QWQ.mm = 1;
QWQ.yy++;
}
}
else if (QWQ.mm == 2 && QWQ.dd > 28) {//二月单独讨论
QWQ.dd = 1;
QWQ.mm++;
}
else if ((QWQ.mm == 2 || QWQ.mm == 4 || QWQ.mm == 6 || QWQ.mm == 9 || QWQ.mm == 11) && QWQ.dd > 30) {
QWQ.dd = 1;
QWQ.mm++;
}
}
}
return QWQ;
}
int Days(struct DateG x) {
int res = 0, flag = 1;
while (flag == 1) {
res++;
x.dd--;
if ((x.yy % 4 == 0 && x.yy % 100 != 0) || x.yy % 400 == 0) {//闰年
if ((x.mm == 1 || x.mm == 3 || x.mm == 5 || x.mm == 7 || x.mm == 8 || x.mm == 10 || x.mm == 12) && x.dd == 0) {
if (x.mm == 8 || x.mm == 1)
x.dd = 31;
else if (x.mm == 3)
x.dd = 29;
else
x.dd = 30;
x.mm--;
if (x.mm == 0) {
x.mm = 12;
x.yy--;
}
}
else if ((x.mm == 2 || x.mm == 4 || x.mm == 6 || x.mm == 9 || x.mm == 11) && x.dd == 0) {
x.dd = 31;
x.mm--;
}
}
else {//非闰年
if ((x.mm == 1 || x.mm == 3 || x.mm == 5 || x.mm == 7 || x.mm == 8 || x.mm == 10 || x.mm == 12) && x.dd == 0) {
if (x.mm == 8 || x.mm == 1)
x.dd = 31;
else if (x.mm == 3)
x.dd = 28;
else
x.dd = 30;
x.mm--;
if (x.mm == 0) {
x.mm = 12;
x.yy--;
}
}
else if ((x.mm == 2 || x.mm == 4 || x.mm == 6 || x.mm == 9 || x.mm == 11) && x.dd == 0) {
x.dd = 31;
x.mm--;
}
}
if (x.yy == 1 && x.mm == 1 && x.dd == 1)
flag = 0;
}
return res;
}
6-8 强者几何
int* fun(int *d,int N){
int *s;
s=(int *)malloc(sizeof(int)*N);
int a[101]={0};
for(int i=0;i<N;i++){
a[d[i]]++;
}
int k=0;
for(int i=0;i<N;i++){
for(int j=100;j>d[i];j--)
k+=a[j];
s[i]=k;
k=0;
}
return s;
}
6-9 反转的最大矩阵
unsigned int fun(char** D, int M, int N)
{
for (int i = 0; i < M; i++)
{
if (D[i][0] != 1)
{
for (int j = 0; j < N; j++)
{
D[i][j] = !D[i][j];
}
}
}
for (int j = 1; j < N; j++)
{
int count = 0;
for (int i = 0; i < M; i++)
{
if (D[i][j]) count++;
}
if (count < M/2)
{
for (int i = 0; i < M; i++)
{
D[i][j] = !D[i][j];
}
}
}
int num = 0;
for (int j = 0; j < N; j++)
{
int P = pow(2, N - j - 1);
for (int i = 0; i < M; i++)
{
num += D[i][j] * P;
}
}
return num;
}
6-10 数据二进制的样子
小明知道数据在计算机中是以二进制方式存储的,不同的数据占用的字节数还不同,实际存储是什么情况并不十分清楚。请编写一个函数,将传入的数据按二进制的形式输出(返回字符串首地址)。 知道数据的初地址和数据的总字节数就可以读出相应的数据(一批由0,1组成的序列)。因不同数据的地址类型有别,考虑到函数的通用性(不同的数据类型均可调用它进行访问),故传入的是一个无类型的地址void*和它实际占用的字节数。
6-11 摘桃几何
//在这里描述函数接口。例如:
int fun ( int *nums,int N ){
int a[N];
int A,B;
a[0]=nums[0];
a[1]=nums[0]>nums[1]?nums[0]:nums[1];
for(int i=2;i<N;i++){
A=a[i-2]+nums[i];
B=a[i-1];
a[i]=A>B?A:B;
}
return a[N-1];
}
6-12 字符串转结构体
ST* InitData(int n){
ST *p=(ST*)malloc(sizeof(ST)*n);
for(int i=0;i<n;i++){
scanf("%s %s %d %d %lf",&p[i].name,&p[i].id,&p[i].gender,&p[i].age,&p[i].scored);
}
return p;
}
6-13 赛程安排 (10 分)
N个人报名参加羽毛球初赛,这里的初赛只进行两两的淘汰赛(N假定为偶数,淘汰一半的选手),赛程规定采用抽签决定比赛对手与比赛顺序。抽签的结果存储在数组A[]中,A[i]表示第i个选手抽得的编号(均在[0--N-1]中),并规定,编号0与1比,2与3比,……N-2与N-1比。显然 ,数组A[]中比赛的选手并不一定是相邻的,现要求你编写函数,通过交换两个选手位置的方式将两个比赛的选手交换到一块,计算最少的交换次数。你能否将时间复杂性控制在O(N)以内?
int fun ( int *A,int N ) {
int cnt=0;
int temp;
for(int i=0;i<N;i+=2){
if(A[i]/2!=A[i+1]/2){
for(int j=i+2;j<N;j++){
if(A[j]/2==A[i]/2){
temp=A[j];
A[j]=A[i+1];
A[i+1]=temp;
cnt++;
break;
}
}
}
}
return cnt;
}
6-14 数组就地循环左移(高效版) (10 分)
编写函数,对传入的指定长度的整数数组进行就地循环左移k(k>=0)位。就地的意思是不使用额外的数组转存,当然为了移动,长度为n的数组正常下标为0--n-1,我们允许使用下标为n的单元进行暂存数据。高效是指以尽量少的移动次数完成任务(除了暂存外,源数据向目标位置要求一次到位),为此,我们提供了一个数组内(下标范围:0-n)的数据移动函数j2i()供调用,必须通过调用该函数来实现数据的内部移动,以方便我们统计移动次数(包括向下标为n的单元进行暂存)。循环左移k位的意思是最前边的k个数据移动数组的最后,相对位置保持不变,具体见输出样例。
6-15 链表分区
struct ListNode* partition(struct ListNode* head, int x){//双指针
if(head==NULL||head->next==NULL)
return head;
struct ListNode*p1=(struct ListNode*)malloc(sizeof(struct ListNode));//申请双指针;
struct ListNode*p2=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode*temp1=p1;
struct ListNode*temp2=p2;
while(head){
if(head->data<x){
temp1->next=head;
temp1=temp1->next;
}
else{
temp2->next=head;
temp2=temp2->next;
}
head=head->next;
}
temp2->next=NULL;
temp1->next=p2->next;
temp1=p1->next;
free(p1);
free(p2);
return temp1;
}
————————————————
版权声明:本文为CSDN博主「✘✘皮卡丘✘✘」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_54907233/article/details/118371153