变长数组&结构体sort&string

PAT1006 Sign In and Sign Out (25 分)

#include<bits/stdc++.h>
using namespace std;
struct Stu{
    char s[20];
    int a,b;
};
const int N=1000000;
Stu stu[N];

bool cmp1(Stu s1,Stu s2){
    return s1.a<s2.a;
}
bool cmp2(Stu s1,Stu s2){
    return s1.b<s2.b;
}
int main(){
    int i,n,a[4],b[4];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",stu[i].s);
        scanf("%d:%d:%d",&a[1],&a[2],&a[3]);stu[i].a=a[1]*3600+a[2]*60+a[3];
        scanf("%d:%d:%d",&b[1],&b[2],&b[3]);stu[i].b=b[1]*3600+b[2]*60+b[3];
    }
    sort(stu,stu+n,cmp1);
    printf("%s ",stu[0].s);
    sort(stu,stu+n,cmp2);
    printf("%s",stu[n-1].s);
    return 0;
}

1008 Elevator (20 分)变长数组

细心,其实这个题用不到

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,res=0,n;
    scanf("%d",&n);
    vector<int> array;
    while(1){
    	scanf("%d",&a);
    	array.push_back(a);
    	if(getchar()=='\n') break;
	}
	res+=5*array.size();
	res+=6*array[0];
	for(int i=0;i<array.size()-1;i++){
		if(array[i+1]>array[i]) res+=6*(array[i+1]-array[i]);
		else res+=4*(array[i]-array[i+1]);
	}
	printf("%d",res);
	return 0;
}
  • int型的数据范围为[ − 2 31 -2^{31} 231, 2 31 − 1 2^{31}-1 2311]
  • pow(10,c1)

1026 程序运行时间

要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间。这个时间单位是 clock tick,即“时钟打点”。同时还有一个常数 CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数 f 的运行时间,我们只要在调用 f 之前先调用 clock(),获得一个时钟打点数 C1;在 f 执行完成后再调用 clock(),获得另一个时钟打点数 C2;两次获得的时钟打点数之差 (C2-C1) 就是 f 运行所消耗的时钟打点数,再除以常数 CLK_TCK,就得到了以秒为单位的运行时间。

这里不妨简单假设常数 CLK_TCK 为 100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

输入格式:
输入在一行中顺序给出 2 个整数 C1 和 C2。注意两次获得的时钟打点数肯定不相同,即 C1 < C2,并且取值在 [0,10^7]

输出格式:
在一行中输出被测函数运行的时间。运行时间必须按照 hh:mm:ss(即2位的 时:分:秒)格式输出;不足 1 秒的时间四舍五入到秒。

#include<bits/stdc++.h>
using namespace std;
int main(){
	float c1,c2;
	int t1,t2,t3,t;
	scanf("%f%f",&c1,&c2);
	t=round((c2-c1)/100);
	//t=((c2-c1)/100 > 0.0);
	t1=t/3600;
	t2=(t-3600*t1)/60;
	t3=t%60;
	printf("%02d:%02d:%02d",t1,t2,t3);
	return 0;
}

%02d:%02d:%02d 格式的控制,round四舍五入取整

1038 Recover the Smallest Number (30 分)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤10 4 ) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

Sample Input:
5 32 321 3214 0229 87
结尾无空行
Sample Output:
22932132143287

#include <bits/stdc++.h>
using namespace std;

const int maxn = 10e5;
string s[maxn];
bool cmp(string a, string b)
{
    return a + b < b + a;
}
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        //scanf("%s", s[i]);
        cin >> s[i];
    }
    sort(s, s + n, cmp);
    string ans;
    for (int i = 0; i < n; i++)
    {
        ans += s[i]; //将排序后的数字串进行拼接
    }
    while (ans.size() != 0 && ans[0] == '0')
    {
        ans.erase(ans.begin());
    }
    if (ans.size() == 0)
        printf("0");
    else
        cout << ans;
    return 0;
}

A1022 Digital Library

#include <bits/stdc++.h>
using namespace std;
map<string, set<int> > mtitle, mauthor, mkey, mpublisher, myear;
void search(map<string, set<int> > &m, string &s, int i)//不是引用的话,有一组会超时
{
    //printf("%d: %s\n", i, s);
    cout<<i<<": "<<s<<endl;
    if (m.find(s) == m.end())
        printf("Not Found\n");
    else
    {
        for (set<int>::iterator it = m[s].begin(); it != m[s].end(); it++)
        {
            printf("%07d\n", *it);
        }
    }
}
int main()
{
    int n, id;
    string str;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &id);getchar();//必须要加 
        getline(cin, str);
        //cout<<str;
        mtitle[str].insert(id); //set的操作insert
        getline(cin, str);
        mauthor[str].insert(id);
        while (cin >> str)
        {
            mkey[str].insert(id);
            char ch = getchar();
            if (ch == '\n')
                break;
        }
        getline(cin, str);
        mpublisher[str].insert(id);
        getline(cin, str);
        myear[str].insert(id);
    }
    int m, type;
    scanf("%d",&m);
    for (int i = 0; i < m; i++)
    {
        scanf("%d:", &type);
        getchar();
        getline(cin, str);
        if (type == 1)
            search(mtitle, str, type);
        else if (type == 2)
            search(mauthor, str, type);
        else if (type == 3)
            search(mkey, str, type);
        else if (type == 4)
            search(mpublisher, str, type);
        else
            search(myear, str, type);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
结构体数组指向结构体数组是指一个结构体数组的元素是另一个结构体数组的指针。这种情况通常用于需要在结构体数组中存储其他结构体数组的情况,可以实现更复杂的数据结构和数据组织方式。 下面是一个简单的示例来介绍结构体数组指向结构体数组的概念: 假设我们有两个结构体类型:Student和Class,其中Student表示学生的信息,Class表示班级的信息。 ```c typedef struct { char name[20]; int age; } Student; typedef struct { Student* students; int numStudents; } Class; ``` 在上面的示例中,Class结构体包含了一个指向Student结构体数组的指针students,以及一个表示学生数量的整数numStudents。 现在我们可以创建一个Class结构体数组,并为每个班级分配一定数量的学生: ```c int main() { int numClasses = 3; int numStudentsPerClass = 5; // 创建Class结构体数组 Class classes[numClasses]; // 为每个班级分配学生数组 for (int i = 0; i < numClasses; i++) { classes[i].students = malloc(numStudentsPerClass * sizeof(Student)); classes[i].numStudents = numStudentsPerClass; } // 对每个班级的学生进行操作 for (int i = 0; i < numClasses; i++) { for (int j = 0; j < numStudentsPerClass; j++) { // 对学生进行赋值操作 strcpy(classes[i].students[j].name, "John"); classes[i].students[j].age = 18; } } // 释放内存 for (int i = 0; i < numClasses; i++) { free(classes[i].students); } return 0; } ``` 在上面的示例中,我们首先创建了一个Class结构体数组,然后为每个班级分配了一个学生数组。通过访问classes[i].students[j],我们可以对每个学生进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值