【C语言】寻找K的在数组中的位置,最多找两个,返回值返回找到几个K(0,1,2)如果大于两个,只找前两个

Mecamodore
  • 3
新手上路,请多包涵

具体要求如下
image.png
求各位大佬解答一下

#include<stdio.h>
#include<stdlib.h>
int DayofYear(int year,int month,int day);
void MonthDay(int year,int yearDay,int*pMonth,int*pDay);
const int a[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
int findK(char isK[],int n,char*firstK,char*secondK);

int main(){
    int year,month,day,yearDay;
    int pMonth,pDay;
    int ch;
    printf("Please enter your choice:\n");
    printf("1. year/ month/day → yearDay\n");
    printf("2. yearDay → year/ month/day\n");
    printf("3. FindK\n");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
                printf("请输入年/月/日:");
                scanf("%d %d %d",&year,&month,&day);
                printf("%d年%d月%d日是今年的第%d天\n",year,month,day,DayofYear(year,month,day));
                break;
        case 2:
                printf("请输入年数及天数:");
                scanf("%d %d",&year,&yearDay);
                MonthDay(year,yearDay,&pMonth,&pDay);
                printf("第%d天是%d年%d日!\n",yearDay,year,pMonth,pDay);
                break;
        case 3:
            char sign;
            while(1)
        {
            int n;
            int firstK,secondK;
            printf("请输入n的值:\n");
            scanf("%d",&n);
            char isK[n];
            printf("请输入数组元素的值:\n");
            for(int i=0;i<n;i++){
                scanf(" %c",&isK[i]);
            }
            if((findK(isK,n,&firstK,&secondK))==2){
                printf("找到两个K,位置是%d和%d\n",firstK+1,secondK+1);
            }else if((findK(isK,n,&firstK,&secondK))==1){
                printf("找到一个K,位置是%d\n",firstK+1);
            }else{
                printf("没有找到K!\n");
            }
            break;
            printf("是否继续?[Y/N]");
            fflush(stdin);
            scanf("%c",&sign);
            if(sign=='N'||sign=='n'){
                break;
            }
        }
        default:
            printf("不在选择范围内!\n");    
            
    }
    return 0;
}

int DayofYear(int year,int month, int day)
{
    int ret;
    if((year%400==0)||(year%4==0)&&(year%100!=0)){
        for(int i=0;i<month-1;i++){
            ret+=a[0][i];
        }
    }else{
        for(int i=0;i<month-1;i++){
            ret+=a[1][i];
        }    
    }
    ret+=day;
    return ret;
}
void MonthDay(int year,int yearDay,int*pMonth,int*pDay)
{
    int i;
    if((year%400==0)||(year%4==0)&&(year%100!=0)){
        for(i=0;yearDay>a[0][i];i++){
            yearDay-=a[0][i];
        }
    }else{
        for(i=0;yearDay>a[1][i];i++){
            yearDay-=a[1][i];
        }
    }
    *pMonth=i+1;
    *pDay=yearDay;
}
int findK(char isK[],int n,char*firstK,char*secondK)//问题所在,不知道怎么设计函数 
{
    
}
回复
阅读 161
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏