经典入门


排序


补充:

< >引用的是编译器的类库路径里面的头文件
” “引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继续在类库路径里搜寻该头文件。

1.题目1202:排序

时间限制:1 秒内存限制:32 兆特殊判题:否提交:25465解决:8168
题目描述:
对输入的n个数进行排序并输出。
输入:
输入的第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。
输出:
可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。
每组测试数据的结果占一行。
样例输入:
4
1 4 3 2
样例输出:
1 2 3 4
来源:
2006年华中科技大学计算机保研机试真题

代码:

#include<stdio.h>

int main()
{
    //整数个数
    int n=0;
    //保存数字
    int num[100];
    //1.输入数字个数
    while(scanf("%d",&n)!=EOF){
        //2.输入每个数字
        for(int i=0;i<n;i++){
            scanf("%d",num+i);
        }
        //3.进行排序
        for(int i=0;i<n;i++){
            for(int j=0;j<n-i-1;j++){
                if(num[j]>num[j+1]){
                    int tem=num[j];
                    num[j]=num[j+1];
                    num[j+1]=tem;
                }
            }
        }
        //4.排序后输出
        for(int i=0;i<n;i++){
            printf("%d ",num[i]);
        }
        printf("\n");
    }
    return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;

int main()
{
    //整数个数
    int n=0;
    //保存数字
    int num[100];
    //1.输入数字个数
    while(scanf("%d",&n)!=EOF){
        //2.输入每个数字
        for(int i=0;i<n;i++){
            scanf("%d",num+i);
        }
        //3.进行排序
        sort(num,num+n);
        //4.排序后输出
        for(int i=0;i<n;i++){
            printf("%d ",num[i]);
        }
        printf("\n");
    }
    return 0;
}

补充:

关于 cmp 函数的定义规则我们只需简单的记得,当 cmp 的返回值为 true 时,
即表示 cmp 函数的第一个参数将会排在第二个参数之前(即在我们定义的规则
中, cmp 表示第一个参数是否比第二个参数大,若是,则排在前面)

sort函数(默认是升序)降序扩展

#include<stdio.h>
#include<algorithm>
using namespace std;

//sort函数变为降序
bool cmp(int x,int y){
    return x>y;//(结果为true,则x在前)
}

int main()
{
    //整数个数
    int n=0;
    //保存数字
    int num[100];
    //1.输入数字个数
    while(scanf("%d",&n)!=EOF){
        //2.输入每个数字
        for(int i=0;i<n;i++){
            scanf("%d",num+i);
        }
        //3.进行排序(降序)
        sort(num,num+n,cmp);
        //4.排序后输出
        for(int i=0;i<n;i++){
            printf("%d ",num[i]);
        }
        printf("\n");
    }
    return 0;
}

2.题目1061:成绩排序

时间限制:1 秒内存限制:32 兆特殊判题:否提交:21610解决:6040
题目描述:
有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。
输入:
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。
输出:
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩
样例输入:
3
abc 20 99
bcd 19 97
bed 20 97
样例输出:
bcd 19 97
bed 20 97
abc 20 99
提示:
学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。
来源:
2000年清华大学计算机研究生机试真题

代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

//学生结构体
struct stu{
    char name[100];
    int age;
    int score;
}stus[1000];

//排序辅助函数
bool cmp(stu a,stu b){
    //1.成绩由低到高
    if(a.score!=b.score){
        return a.score<b.score;
    }
    //姓名排序
    int rlt=strcmp(a.name,b.name);
    if(rlt!=0){
        return rlt<0;
    }
    //年龄排序
    return a.age<b.age;


}

int main(){
    //学生个数
    int n=0;
    //1.输入学生个数
    while(scanf("%d",&n)!=EOF){
        //2.输入每个学生的信息
        for(int i=0;i<n;i++){
         scanf("%s%d%d",stus[i].name,&stus[i].age,&stus[i].score);
        }
        //3.进行排序
        sort(stus,stus+n,cmp);
        //4.进行输出
        for(int i=0;i<n;i++){
            printf("%s %d %d",stus[i].name,stus[i].age,stus[i].score);
            printf("\n");
        }

    }
    return 0;
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

//学生结构体
struct stu{
    char name[100];
    int age;
    int score;
    bool operator < (const stu & b)const{
        //1.成绩由低到高
        if(score!=b.score){
            return score<b.score;
        }
        //2.姓名按字母序
        int rlt=strcmp(name,b.name);
        if(rlt!=0){
            return rlt<0;
        }
        //3.按年龄由低到高
        return age<b.age;
    }
}stus[1000];


int main(){
    //学生个数
    int n=0;
    //1.输入学生个数
    while(scanf("%d",&n)!=EOF){
        //2.输入每个学生的信息
        for(int i=0;i<n;i++){
            scanf("%s%d%d",stus[i].name,&stus[i].age,&stus[i].score);
        }
        //3.进行排序
        sort(stus,stus+n);
        //4.进行输出
        for(int i=0;i<n;i++){
            printf("%s %d %d\n",stus[i].name,stus[i].age,stus[i].score);
        }

    }
    return 0;
}

日期类问题


1.题目1096:日期差值

时间限制:1 秒内存限制:32 兆特殊判题:否提交:10512解决:3508
题目描述:
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
输入:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出:
每组数据输出一行,即日期差值
样例输入:
20110412
20110422
样例输出:
11
来源:
2009年上海交通大学计算机研究生机试真题

#include<stdio.h>
#define isRYear(x) x%400==0||(x%100!=0&&x%4==0)?1:0

//保存特定日期到每一时间点的日期差
int date_size[5001][13][32];

//保存每个月有多少天
int dayInMoth[][2]={
    0,0,
    31,31,
    28,29,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31
};

//求绝对值
int abs(int a){
    if(a<0)
        return (-1)*a;
    return a;
}

//保存时间的结构体
struct date{
    int year;
    int month;
    int day;
    //计算下一天的方法
    void nextDay(){
        day++;
        if(day>dayInMoth[month][isRYear(year)]){
            day=1;
            month++;
            if(month>12){
                year++;
                month=1;
            }
        }
    }
};

int main(){
    int count=1;
    //1.第一天的值
    date d;
    d.year=1;
    d.month=1;
    d.day=1;
    //2.计算之后的天数
    while(d.year<5001){
        date_size[d.year][d.month][d.day]=count;
        d.nextDay();
        count++;
    }
    //3.读取天数
    int y1=0;
    int m1=0;
    int d1=0;
    int y2=0;
    int m2=0;
    int d2=0;

    while(scanf("%4d%2d%2d",&y1,&m1,&d1)!=EOF){
        scanf("%4d%2d%2d",&y2,&m2,&d2);
        //4.输出天数差
        printf("%d\n",abs(date_size[y1][m1][d1]-date_size[y2][m2][d2])+1);
    }

    return 0;

}

补充:

以 a % b 语句为例,我们先不加说明的指出该运算的特点。其运算在行为上
好像是按如下步骤进行的,首先计算出 a 的绝对值被 b 的绝对值除所得的余数,
再使该余数的符号与 a 保持一致。即若 a 为正数,则该表达式结果必为非负数(可
能为 0);若 a 为负数,则表达式结果必为非正数(可能为 0)。


解决:可以使用加取余数后再取余数,例如:
-3%7=-3
((-3%7)+7)%7=4

2.题目1043:Day of Week

时间限制:1 秒内存限制:32 兆特殊判题:否提交:6985解决:2458
题目描述:
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
输入:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
样例输入:
9 October 2001
14 October 2001
样例输出:
Tuesday
Sunday
提示:
Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
来源:
2008年上海交通大学计算机研究生机试真题

#include<stdio.h>
#include<string.h>
#define isRYear(x) x%400==0||(x%4==0&&x%100!=0)?1:0

using namespace std;

//保存每月有多少天
int dayInMonth[13][2]={
    0,0,
    31,31,
    28,29,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31
};

//保存日期与1000年1月1日的差值
int date_size[3001][13][32];

//保存英文月份
char MONTH[13][20]={
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

//保存星期
char WEEK[7][20]={
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

//计算日期到1000年1月1日大小结构体
struct Date{
    int day;
    int month;
    int year;
    void nextDay(){
        day++;
        if(day>dayInMonth[month][isRYear(year)]){
            day=1;
            month++;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
} ;


int main(){
    int size=1;

    //初始化第一天
    Date tem;
    tem.day=1;
    tem.month=1;
    tem.year=1000;

    //1.计算到1000年1月1日的差值
    while(tem.year<=3000){
        date_size[tem.year][tem.month][tem.day]=size;
        tem.nextDay();
        size++;
    }

    //2.今天到1000年1月1日的差值
    int today=date_size[2001][10][9];

    //3.输入日期数
    int d=0;
    char m[20];
    int y=0;
    while(scanf("%d%s%d",&d,m,&y)!=EOF){
        //4.计算月份
        int i=1;
        for(i=1;i<=12;i++){
            if(strcmp(m,MONTH[i])==0){
                break;
            }
        }
        //5.得到输入日期与1000年1月1日的差值
        int inDay=date_size[y][i][d];
        //6.得到星期差值
        int dif=(inDay-today)%7;
        //7.今天为周二,计算输入日期的星期数
        int rlt=(2+dif)%7;
        printf("%s\n",WEEK[(rlt+7)%7]);
    }

    return 0;
}

Hash的应用


1.题目1018:统计同成绩学生人数

时间限制:1 秒内存限制:32 兆特殊判题:否提交:10676解决:5583
题目描述:
读入N名学生的成绩,将获得某一给定分数的学生人数输出。
输入:
测试输入包含若干测试用例,每个测试用例的格式为
第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数
当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
输出:
对每个测试用例,将获得给定分数的学生人数输出。
样例输入:
3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0
样例输出:
1
0
2
来源:
2006年浙江大学计算机及软件工程研究生机试真题

#include<stdio.h>
using namespace std;

int main(){
    //学生人数
    int n=0;
    scanf("%d",&n);
    while(n!=0){
        //保存学生成绩的hash
        int Hash[101]={0};
        //1.输入学生成绩
        int tem;
        for(int i=0;i<n;i++){
            scanf("%d",&tem);
            Hash[tem]++;
        }
        //2.输入要查询的成绩
        int queryscore=0;
        scanf("%d",&queryscore);
        //3.输出查询成绩的人数
        printf("%d\n",Hash[queryscore]);
        //4.输入下一次要查询的学生数
        scanf("%d",&n);
    }
    return 0;
}

2.题目1431:Sort

时间限制:1 秒内存限制:128 兆特殊判题:否提交:8212解决:2663
题目描述:
给你n个整数,请按从大到小的顺序输出其中前m大的数。
输入:
每组测试数据有两行,第一行有两个数n,m(n>0,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
输出:
对每组测试数据按从大到小的顺序输出前m大的数。
样例输入:
5 3
3 -35 92 213 -644
样例输出:
213 92 3

#include<stdio.h>
#define OFFSET 500000

using namespace std;

//保存数据的ash,较大,必须在main函数外部
int Hash[1000001]={0};

int main(){
    //数字个数与前多少数字
    int n,m;
    //1.输入n m
    while(scanf("%d%d",&n,&m)!=EOF){
        //清空保存数据的hash
        for(int i=0;i<1000000;i++){
            Hash[i]=0;
        }
        //2.输入n个数据
        int tem=0;
        for(int i=0;i<n;i++){
            scanf("%d",&tem);
            Hash[tem+OFFSET]++;
        }
        //3.输出结果
        int cnt=OFFSET;
        //输出m个数字
        for(int i=0;i<m;i++){
            //如果为0,则继续向下
            while(Hash[cnt+OFFSET]==0){
                cnt--;
            }
            Hash[cnt+OFFSET]--;
            printf("%d",cnt);
            //限制空格的输出
            if(i!=m-1){
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

注意事项:
100 0000的数据,应考虑时间复杂度
输出的空格应该符合要求


排版题


1.题目1065:输出梯形

时间限制:1 秒内存限制:32 兆特殊判题:否提交:6716解决:3650
题目描述:
输入一个高度h,输出一个高为h,上底边为h的梯形。
输入:
一个整数h(1<=h<=1000)。
输出:
h所对应的梯形。
样例输入:
4
样例输出:
这里写图片描述
提示:
梯形每行都是右对齐的,sample中是界面显示问题
来源:
2001年清华大学计算机研究生机试真题(第II套)

#include<stdio.h>
using namespace std;

int main(){
    //梯形的高度
    int h=0;
    //1.输入梯形的高度
    while(scanf("%d",&h)!=EOF){
        //2.遍历输出梯形
        //空格的个数(h-1)*2
        for(int i=0;i<h;i++){
            //输出空格
            for(int j=0;j<(h-i-1)*2;j++){
                printf(" ");
            }
            //输出*号
            for(int j=0;j<(h+i*2);j++){
                printf("*");
            }
            //输出换行
            printf("\n");
        }
    }
    return 0;
}

2.题目1432:叠筐

时间限制:1 秒内存限制:128 兆特殊判题:否提交:5968解决:1388
题目描述:
把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。
输入:
输入是一个个的三元组,分别是,外筐尺寸n(n为满足0到80之间的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符;
输出:
输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。
样例输入:
11 B A
5 @ W
样例输出:
AAAAAAAAA
ABBBBBBBBBA
ABAAAAAAABA
ABABBBBBABA
ABABAAABABA
ABABABABABA
ABABAAABABA
ABABBBBBABA
ABAAAAAAABA
ABBBBBBBBBA
AAAAAAAAA
@@@
@WWW@
@W@W@
@WWW@
@@@

#include<stdio.h>

int main(){
    //框边的大小,字符1,字符2
    int n=0;
    char c1,c2;
    //1.输入框边的大小
    while(scanf("%d %c %c",&n,&c2,&c1)!=EOF){
        //得出边缘字符与中心字符
        if((n+1)/2%2==1){
            char tem=c1;
            c1=c2;
            c2=tem;
        }

        //中心点大小
        int center=(n+1)/2;
        //2.循环遍历每一行
        for(int i=0;i<n;i++){
            //2.1是第一行
            if(i==0){
                printf(" ");
                for(int j=0;j<n-2;j++){
                    printf("%c",c1);
                }
                printf(" ");
                printf("\n\n");
                continue;
            }
            //2.2中间上部分
            if(i<center){
                int j;
                for(j=0;j<i;j++){
                    if(j%2==0)
                        printf("%c",c1);
                    else
                        printf("%c",c2);
                }
                for(int k=0;k<n-2*i;k++){
                    if(j%2==0)
                        printf("%c",c1);
                    else
                        printf("%c",c2);
                }
                for(int m=0;m<i;m++){
                    if(j%2==0)
                        printf("%c",c2);
                    else
                        printf("%c",c1);
                    j++;
                }
                printf("\n\n");
                continue;
            }else if(i!=n-1){
                //2.3中间下部分
                int j;
                for(j=0;j<(n-i-1);j++){
                    if(j%2==0)
                        printf("%c",c1);
                    else
                        printf("%c",c2);
                }
                for(int k=0;k<(n-2*(n-i))+2;k++){
                    if(j%2==0)
                        printf("%c",c1);
                    else
                        printf("%c",c2);
                }
                for(int m=0;m<(n-i)-1;m++){
                    if(j%2==0)
                        printf("%c",c2);
                    else
                        printf("%c",c1);
                    j++;
                }
                printf("\n\n");
                continue;
            }
            //2.4是最后一行
            if(i==n-1){
                printf(" ");
                for(int j=0;j<n-2;j++){
                    printf("%c",c1);
                }
                printf(" ");
                printf("\n");
            }
        }
    }
    return 0;
}


查找


1.题目1052:找x

时间限制:1 秒内存限制:32 兆特殊判题:否提交:8882解决:4579
题目描述:
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
输入:
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出:
对于每组输入,请输出结果。
样例输入:
2
1 3
0
样例输出:
-1
来源:
2010年哈尔滨工业大学计算机研究生机试真题

#include<stdio.h>

//用来保存输入的数字
int num[201]={0};

int main(){
    //数字个数n
    int n=0;
    //1.输入数字个数n
    while(scanf("%d",&n)!=EOF){
        //2.输入每一个数字
        for(int i=0;i<n;i++){
            scanf("%d",num+i);
        }
        //3.输入需要查找的数字m
        int m;
        scanf("%d",&m);
        //4.遍历查找数字m的下标
        int index=-1;
        for(int i=0;i<n;i++){
            if(num[i]==m){
                index=i;
                break;
            }
        }
        //5.输出结果
        printf("%d\n",index);
    }
    return 0;
}

2.题目1069:查找学生信息

时间限制:1 秒内存限制:32 兆特殊判题:否提交:13950解决:3771
题目描述:
输入N个学生的信息,然后进行查询。
输入:
输入的第一行为N,即学生的个数(N<=1000)
接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04
输出:
输出M行,每行包括一个对应于查询的学生的信息。
如果没有对应的学生信息,则输出“No Answer!”
样例输入:
4
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
5
02
03
01
04
03
样例输出:
02 刘唐 男 23
03 张军 男 19
01 李江 男 21
04 王娜 女 19
03 张军 男 19
来源:
2003年清华大学计算机研究生机试真题

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

//查询id的数组
char ids[10001][100];
//学生信息结构体
struct S{
    //01 李江 男 21
    char id[100];
    char name[100];
    char sex[10];
    int age;

    bool operator <(const S &b)const{
        //升序排序
        return strcmp(id,b.id)<0;
    }
}Stus[1001];

//二分查找的函数
int query(S s[1001],int N,char id[100]){
    //二分查找的左与右
    int left=0;
    int right=N-1;
    //开始二分查找
    while(left<=right){
        //中间数字
        int mid=(left+right+1)/2;
        //中间值与需要查找的比较
        int rlt=strcmp(s[mid].id,id);
        //查找到值
        if(rlt==0){
            return mid;
        }
        //查找值小于中间值
        if(rlt>0)
            right=mid-1;
        else
            left=mid+1;
    }
    return -1;
}

int main(){
    //学生的个数N
    int N=0;
    //1.输入学生的个数N
    while(scanf("%d",&N)!=EOF){
        //2.输入每一个学生的信息
        for(int i=0;i<N;i++){
            scanf("%s %s %s %d",Stus[i].id,Stus[i].name,Stus[i].sex,&Stus[i].age);
        }
        //3.输入查询的个数
        int M=0;
        scanf("%d",&M);
        //4.输入每个查询的id
        for(int i=0;i<M;i++){
            scanf("%s",ids[i]);
        }
        //5.对学生信息进行排序
        sort(Stus,Stus+N);
        //6.对每个id进行查询
        for(int i=0;i<M;i++){
            int index=query(Stus,N,ids[i]);
            if(index==-1)
                printf("No Answer!\n");
            else
                printf("%s %s %s %d\n",Stus[index].id,Stus[index].name,Stus[index].sex,Stus[index].age);
        }
    }
    return 0;
}

注意事项:
一定要保证输入字符串的大小充足!


贪心


1.题目1433:FatMouse

时间限制:1 秒内存限制:128 兆特殊判题:否提交:3508解决:1544
题目描述:
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
输入:
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.
输出:
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
样例输入:
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出:
13.333
31.500

#include<stdio.h>
#include<algorithm>
using namespace std;

//保存性价比
struct E{
    double f;
    double j;
    double fj;
    //降序
    bool operator <(const E &b)const{
        return fj>b.fj;
    }

}FJ[1001];

int main(){
    //数字m与n
    double m=0;
    int n=0;
    //1.输入数字m与n
    while(scanf("%lf %d",&m,&n)!=EOF){
        //2.-1 -1则退出
        if(m==-1&&n==-1)
            break;
        //3.输入n个性价比
        for(int i=0;i<n;i++){
            scanf("%lf %lf",&FJ[i].j,&FJ[i].f);
            FJ[i].fj=FJ[i].j/FJ[i].f;
        }
        //4.计算结果
        //排序
        sort(FJ,FJ+n);
        //结果
        double rlt=0;
        //索引
        int index=0;
        while(m>0&&index<n){
            //如果能全部买下
            if(m>FJ[index].f){
                rlt+=FJ[index].j;
                m-=FJ[index].f;
            }else{
                //不能全部买下
                rlt+=m*FJ[index].fj;
                m=0;
            }
            index++;
        }
        //5.输出结果
        printf("%.3lf\n",rlt);
    }
    return 0;
}

2.题目1434:今年暑假不AC

时间限制:1 秒内存限制:128 兆特殊判题:否提交:3294解决:1727
题目描述:
“今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%…”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
输入:
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
输出:
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
样例输入:
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
样例输出:
5

#include<stdio.h>
#include<algorithm>
using namespace std;

//保存节目信息的结构体
struct E{
    int s;
    int e;
    //排序,升序
    bool operator < (const E &b)const{
        return e<b.e;
    }
}Times[101];

int main(){
    //节目数
    int n=0;
    //1.输入节目数n
    while(scanf("%d",&n)!=EOF){
        //2.节目数为0,则跳出
        if(n==0)
            break;
        //3.输入每个节目的开始、结束时间
        for(int i=0;i<n;i++){
            scanf("%d %d",&Times[i].s,&Times[i].e);
        }
        //排序
        sort(Times,Times+n);
        //4.计算可以观看的节目数
        int rlt=0;
        int curTime=0;
        for(int i=0;i<n;i++){
            if(curTime<=Times[i].s){
                rlt++;
                curTime=Times[i].e;
            }
        }
        //5.输出结果
        printf("%d\n",rlt);
    }
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值