文章目录
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 231−1]
- 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;
}