蓝桥杯练习题笔记--1031~1051

三、1031~1051

1031: [编程入门]自定义函数之字符串反转

题目:写一函数,使输入的一个字符串按反序存放,在主函数中输入并输出反序后的字符串(不包含空格)。

代码如下(C):

#include
#include
 
int exchange(char a[],char b[]);
 
int main()
{
    char a[1000],b[1000];
    gets(a);
    exchange(a,b);
    puts(b);
    return 0;
}

int exchange(char a[],char b[])
{
    int len, j = 0;
    len = strlen(a);
    for(int i=len-1; i>=0; i--) //此处可用while优化
    {
        b[j] = a[i];
        j++;
    }
    b[j] = '\0';               //给末尾加上结束符。
    return 0;
}

代码如下(C++):

#include 
#include             //包含字符串头文件
using namespace std;

int Reverse(char a[],char b[]);

int main ()
{
    char a[100],b[100];
    cin >> a;
    Reverse(a,b);
    cout << b << endl;
    return 0;
}

int Reverse(char a[],char b[])      //自定义函数
{
    int i=0,n;
    n=strlen(a);                    //获得字符串长度
    while (a[i]!='\0')
    {
        b[n-i-1]=a[i];              //a[]逆序存到吧[]中
        i++;
    }
    b[n]='\0';                      //别忘了加个结束符
    return 0;
}

1032: [编程入门]自定义函数之字符串连接

题目:写一函数,将两个字符串连接

代码如下(C):

#include
#include
int main()
{
    char a[1000], b[1000];
    gets(a);
    gets(b);//需回车

    strcat(a,b);
    puts(a);
    return 0;
}

代码如下(C++):

/***********************************
#include 
#include 
using namespace std;

int main()
{
    string str1, str2;
    getline(cin, str1);//让我们很方便的输入一串字符串
    getline(cin, str2);
    cout << str1 << str2 << endl;
    return 0;
}
***********************************/
#include 
#include 
using namespace std;

string connect(string str1, string str2);

int main()
{
    string str1, str2;
    cin >> str1 >> str2;
    cout<<connect(str1,str2); 

}

string connect(string str1, string str2) //string
{
    return str1+str2;
}

1033: [编程入门]自定义函数之字符提取**

题目:写一函数,将一个字符串中的元音字母复制到另一个字符串,然后输出。

代码如下(C):

#include 
#include 

int yuanyin(char a[],char b[]);

int main()
{
    char a[100],b[100];
    gets(a);
    yuanyin(a,b);
    puts(b);
    return 0;
}

int yuanyin(char a[],char b[])
{
    int j=0,ken,len;
    char mid;           //中间变量用于交换
    len = strlen(a);
    for(int i=0; i<len; i++)
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
        {
            b[j]=a[i];
            j++;
        }
    }
    b[j]='\0';
    ken=strlen(b);
    for(int i=0; i<ken; i++)//第二个数组执行元音字母的排序
    {
        for(j=i; j<ken; j++)
        {
            if(b[i]>b[j])
            {
                mid=b[j];
                b[j]=b[i];
                b[i]=mid; 
            }
         }
    }       
    return 0;
}

代码如下(C++):指针

#include

char* CharExtraction(char* p1,char const *p2);

int main(int argc, char** argv) 
{
    char c1[100],  c2[100];
    gets(c1);
    puts(CharExtraction(c2, c1));
    return 0;
}

char* CharExtraction(char* p1,char const *p2) 
{
    char* ret = p1;
    while (*p2) 
    {
        if (*p2 == 'a' || *p2 == 'e' || *p2 == 'i' || *p2 == 'o' || *p2 == 'u') 
        {
            *p1++ = *p2;
        }
        p2++;
    }
    *p1 = '\0'; //注意事项:末尾记得填充'\0';
    return ret;
}

1034: [编程入门]自定义函数之数字分离

题目:

代码如下(C):

#include 

void kongge (char a[4]);

int main()
{
    char a[4];
    int i;
    for(i=0;i<4;i++)
        scanf("%c",&a[i]);
    kongge(a);
    return 0;
}

void kongge (char a[4])
{
    int i,t=0;
    for(i=0;i<4;i++)
    {
        if(t!=0)           //用来输出空格
            printf(" ");
        t++;
        printf("%c",a[i]); //输入输出时记得用%c!
    }
}

代码如下(C++):

#include 
#include 

using namespace std;

int main()
{
    string str;
    cin >> str;
    for(int i=0; i<str.size(); i++)
        cout<<str[i]<<" ";
    return 0;
}

1035: [编程入门]自定义函数之字符类型统计

题目:

代码如下(C):

#include

int e=0, b=0, c=0, d=0;
void Statistics(char a[100]); 

int main()
{
    char a[100];
    gets(a);
    Statistics(a);
    printf("%d %d %d %d\n", e, b, c, d);
    return 0;
}

void Statistics(char a[100])
{
    int i;
    for(i=0; a[i]!='\0'; i++)
    {
        if('a'<=a[i] && a[i]<='z' || 'A'<=a[i] && a[i]<='Z')
            e++;
        else if('0'<=a[i] && a[i]<='9')
            b++;
        else if(a[i]==' ')
            c++;
        else
            d++;
    }
}

代码如下(C++):

#include
#define max 1000
using namespace std;

int main() 
{
    int a=0,b=0,c=0,d=0;
    char arr[max];
    gets(arr);
    int len=strlen(arr);
    for(int i=0; i<len; i++) 
    {
        if((arr[i]>='A'&&arr[i]<='Z')||(arr[i]>='a'&&arr[i]<='z'))
            a++;
        else if(arr[i]>='0'&&arr[i]<='9')
            b++;
        else if(arr[i]==32)
            c++;
        else
            d++;
    }
    cout << a <<' '<< b <<' '<< c <<' '<< d << endl;
    return 0;
}

1038: [编程入门]宏定义练习之三角形面积

题目:三角形面积=SQRT(S*(S-a)(S-b)(S-c)) 其中S=(a+b+c)/2,a、b、c为三角形的三边。 定义两个带参的宏,一个用来求area, 另一个宏用来求S。 写程序,在程序中用带实参的宏名来求面积area。

代码如下(C):

#include 
#include
#define S ((a+b+c)/2)
#define area sqrt(S*(S-a)*(S-b)*(S-c))

int main() 
{
    float a,b,c;
    scanf("%f %f %f", &a, &b, &c);
    printf("%.3f", area);
    return 0;
}

代码如下(C++):

#include
#define area sqrt(S*(S-a)*(S-b)*(S-c))
#define S ((a+b+c)/2)
using namespace std;

int main() 
{
    int a, b, c;
    cin >> a >> b >> c;
    cout.setf(ios::fixed);
    cout << setprecision(3) << area << endl;//直接用
    return 0;
}

1039: [编程入门]宏定义之闰年判断

题目:给年份year,定义一个宏,以判别该年份是否闰年。提示:宏名可以定义为LEAP_YEAR,形参为year,既定义宏的形式为 #define LEAP_YEAR(year) (读者设计的字符串)

代码如下(C):

/******************************************
*   能被4整除但不能被100整除或者能被400整除
*******************************************/
#include 
#define LEAP_YEAR(year) ((year%4==0 && year%100!=0) || (year%400==0)) ? "Yes":"No"

int main() 
{
    int year;
    scanf("%d",&year);
    printf("%s",LEAP_YEAR(year));
    return 0;
}

代码如下(C++):

#include 
#define LEAP_YEAR(y) leap(y) //替换为该子函数
using namespace std;
 
char leap(int year); //判断闰年

int main() 
{
    int y = 0;
    cin >> y;
    cout << LEAP_YEAR(y) << endl;
    return 0;
}
char leap(int year) {
    if((year % 4 == 0 && year % 100) || (year % 400 == 0)) {
        return 'L';//如何返回"Yes"
    } else {
        return 'N';
    }
}

1049: [编程入门]结构体之时间设计

题目:定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。

代码如下(C):行云流水

#include
#include
struct date 
{
    int day;
    int month;
    int year;
} time,*p;

int main() 
{
    int sumday = 0;    //总天数
    p=&time;
    scanf("%d%d%d", &(p->year), &(p->month), &(p->day)); //赋值
    int arr[12]= {31,28,31,30,31,30,31,31,30,31,30,31};  //每个月的天数
    if((p->year%4==0 && p->year%100!=0) || p->year%400==0) 
    {
        arr[1]=29;    //闰年2月29天
    }
    sumday += p->day; //本月天数
    for(int i=0; i<p->month-1; ++i) 
    {
        sumday += arr[i]; //本月前每个月的天数
    }
    printf("%d",sumday);
    return 0;
}

代码如下(C++):class

/****************************************************
 * 既然是C++,用class代替struct也是很正常的吧。
 * 计算第几天就是建一个数组存放每个月有几天,
 * 然后把前面的月的天数加起来,加上day,最后闰年+1。
****************************************************/
#include 
using namespace std;
 
const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 定义成13是为了方便对应数组下标
 
class Date 
{
    private:
        short year; // 年
        short month; // 月
        short day; // 日
    public:
        // 构造函数
        Date(short year, short month, short day) 
        {
            this->year = year;
            this->month = month;
            this->day = day;
        }
        // 析构函数
        virtual ~Date() 
        {

        }

        void countDay() 
        {
            short count = day; // 计数
            for(int i = 1; i < month; i++) 
            {
                count += days[i];
            }
            if((!(year % 4) && (year % 100)) || !(year % 400)) 
            {
                count++; // 闰年则+1
            }
            cout << count << endl; // 可以return但没必要
        }
};
 
int main() 
{
    short year = 0;
    short month = 0;
    short day = 0;
    cin >> year >> month >> day; // 读输入
    Date date(year, month, day);
    date.countDay();
    return 0;
}

1050: [编程入门]结构体之成绩记录

题目:现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数,读取N条记录输入,再按要求输出。 N<100

代码如下(C):

#include 
#include 
#include 
 
//定义一个学生结构体
typedef struct
{
    char *id;
    char *name;
    int score1;
    int score2;
    int score3;
} Stu;
 
//1 输入一个Stu信息 返回一个Stu指针
Stu *inputStu()
{
    Stu *stutmp = (Stu*)malloc(sizeof(Stu));
    if (stutmp == NULL)
    {
        return NULL;
    }
 
    char idtmp[100];
    scanf("%s", idtmp);
    stutmp->id = (char*)malloc(strlen(idtmp) + 1);
    if (stutmp->id == NULL)
    {
        return NULL;
    }
    strcpy(stutmp->id, idtmp);
 
    char nametmp[100];
    scanf("%s", nametmp);
    stutmp->name = (char*)malloc(strlen(nametmp) + 1);
    if (stutmp->name == NULL)
    {
        return NULL;
    }
    strcpy(stutmp->name, nametmp);
 
    scanf("%d", &stutmp->score1);
    scanf("%d", &stutmp->score2);
    scanf("%d", &stutmp->score3);
 
    return stutmp;
}
 
//2 打印学生信息
void printStu(Stu *stu)
{
    printf("%s,%s,%d,%d,%d\n", stu->id, stu->name, stu->score1, stu->score2, stu->score3);
}
 
//3 释放一个Stu结构
void freeStu(Stu *stu)
{
    if (stu == NULL)
        return;
 
    free(stu->id);
    free(stu->name);
    free(stu);
}
 
int main()
{
    int N;
    scanf("%d", &N);
    if (N >= 100)
    {
        return -1;
    }
 
    //定义一个结构体指针数组,存放N个指针
    Stu *student[N];
    //1 输入
    int i;
    for (i = 0; i < N; i++)
    {
        student[i] = inputStu();
    }
 
    //2 打印
    for (i = 0; i < N; i++)
    {
        printStu(student[i]);
    }
 
    //3 释放
    for (i = 0; i < N; i++)
    {
        freeStu(student[i]);
    }
 
    return 0;
}

代码如下(C++):

#include
#include
#include 
#include
using namespace std; //命名空间

class students       //学生类
{
public:   //公共部分声明输入输出方法
    void inputs();
    void prints();
private:  //私有部分定义学生的各项属性
    string num;  //学号
    string name; //名字
    int score1;  //分数1
    int score2;  //分数2
    int score3;  //分数3
};

int main()  //主函数
{
    students a[100];  //数组a具有students类的所有属性
    int n;
    cin>>n;           //控制输入的学生数
    for(int i=0; i<n; i++)  //循环进行读入数据
        a[i].inputs();
    for(int i=0; i<n; i++)  //循环进行输出数据
        a[i].prints();
    return 0;
}

void students::inputs()     //定义输入函数
{
    cin >> num >> name >> score1 >> score2 >> score3;   
}

void students::prints()    //定义输出函数
{
    cout << num << "," << name << "," << score1 << "," << score2 << "," << score3 << endl;
}

1051: [编程入门]结构体之成绩统计2

题目:有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)

代码如下(C):

#include
#include
//定义学生成绩结构体 改名为stu 
typedef struct student{
    char ch[20];
    char name[20];
    int grade[3];
}stu;
//程序入口main函数 
int main()
{
    //为学生输入数据的函数定义 
    void input(stu *sp,const int n);
    //输出学生平均成绩的函数 定义 
    void print(const stu *sp,const int n);
    int n=0;//n表示有多少个学生 
    scanf("%d",&n);
    //getchar()接收缓冲区的'\n'避免 对下面赋值造成影响 
    getchar();
    //向内存申请n个学生信息存储空间 
    stu *sp=(stu *)malloc(sizeof(stu)*n);
    //调用输入学生数据函数 
    input(sp,n);
    //调用输出平均成绩函数 
    print(sp,n);
    //使用完成释放申请的空间 
    free(sp);
    return 0;
}
//学生成绩输入函数的实现 
void input(stu *sp,const int n)
{
    int i=0;
    for(;i<n;i++)
    {
        scanf("%s %s %d %d %d", sp[i].ch, sp[i].name, &sp[i].grade[0], &sp[i].grade[1], &sp[i].grade[2]);
    }
}
//学生输出平均成绩函数的实现 
void print(const stu *sp,const int n)
{
    //定义三科各科总平均成绩 
    float sum1=0, sum2=0, sum3=0;
    //存放最高成绩的学生 
    int max=0;
    //lop保存最高成绩的学生下标 
    int i=0, lop=-1;
    for(; i<n; i++)
    {
        int tempmax = sp[i].grade[0] + sp[i].grade[1] + sp[i].grade[2];
        //如果当前成绩大于最高成绩,则将当前成绩放入最高成绩变量中 
        if(max<tempmax)
        {
            lop = i;
            max = tempmax;
        }
        //算出各科总平均成绩
        sum1+=sp[i].grade[0];
        sum2+=sp[i].grade[1];
        sum3+=sp[i].grade[2];
    }
    //打印各科总平均成绩 
    printf("%.0f %.0f %.0f\n", sum1/n, sum2/n, sum3/n);
    //打印最高成绩单的学生信息 
    printf("%s %s %d %d %d\n", sp[lop].ch, sp[lop].name, sp[lop].grade[0], sp[lop].grade[1], sp[lop].grade[2]);
}

代码如下(C++):

#include 
#include 
 
using namespace std;
 
class student 
{
    public:
        // 构造学生对象
        void setInfo(char *id, char *name, short *score) 
        {
            strcpy(this->id, id);
            strcpy(this->name, name);
            for(int i = 0; i < 3; i++) 
            {
                this->score[i] = score[i];
            }
        }
        // 打印学生信息
        void printInfo() 
        {
            cout << id << " " << name << " " << score[0] << " " << score[1] << " " << score[2] << endl;
        }
        // 总分,用于比较
        short sumScore() 
        {
            return score[0] + score[1] + score[2];
        }
    private:
        char id[10]; // 学号
        char name[10]; // 姓名
        short score[3]; // 成绩
};
 
int main() {
    short N = 0; // 学生数
    cin >> N;
     
    student stu[N]; // 构造N个学生对象
    short avgScore[3] = { 0 }; // 平均分,分为3门课
    short maxScore = 0; // 最高分
    short maxScoreIndex = 0; // 存储总分最高的学生在数组中的位置
     
    // 输入学生信息
    for(int i = 0; i < N; i++) 
    {
        char id[10];
        char name[10];
        short score[3] = { 0 };
        cin >> id >> name;
        for(int j = 0; j < 3; j++) 
        {
            cin >> score[j];
            avgScore[j] += score[j]; // 每输入一门课的分数就加到对应课程的avgScore上
        }
        stu[i].setInfo(id, name, score); // 存储学生信息,用于打印
    }
     
    for(int i = 0; i < N; i++) 
    {
        if(stu[i].sumScore() > maxScore) 
        {
            maxScore = stu[i].sumScore(); // 更新最高分
            maxScoreIndex = i; // 更新最高分的学生位置
        }
    }
     
    cout << avgScore[0] / N << " " << avgScore[1] / N << " " << avgScore[2] / N << endl; // 打印平均分
    stu[maxScoreIndex].printInfo(); // 打印最高分学生
    return 0;
}

你可能感兴趣的:(C,c++)