https://www.patest.cn/contests/pat-a-practise/1001
语言 | 用时ms | 内存 |
---|---|---|
Java (javac 1.6.0) | 63 | 10832 |
C (gcc 4.7.2) | 3 | 384 |
C++ (g++ 4.7.2) | 3 | 484 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.printf("%,d",a+b);
}
}
# include
# include
# include
main()
{
int a,b;
scanf("%d %d", &a, &b);
char s[20];
char *p = s;
sprintf(s,"%d",a+b);
int len = strlen(s);
int i = 0;
if(*p=='-'){
printf("-");
len--;
p++;
}
while(i++printf("%c",*p++);
if((len-i)%3==0&&(len-i)!=0)
printf(",");
}
}
只需改一下include文件即可。
# include
# include
# include
main()
{
int a,b;
scanf("%d %d", &a, &b);
char s[20];
char *p = s;
sprintf(s,"%d",a+b);
int len = strlen(s);
int i = 0;
if(*p=='-'){
printf("-");
len--;
p++;
}
while(i++printf("%c",*p++);
if((len-i)%3==0&&(len-i)!=0)
printf(",");
}
}
https://www.patest.cn/contests/pat-a-practise/1002
语言 | 用时ms | 内存 |
---|---|---|
Java (javac 1.6.0) | 97 | 12424 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String[] nums1 = bf.readLine().split(" ");
String[] nums2 = bf.readLine().split(" ");
float[] cons;
if(Integer.valueOf(nums1[1])>Integer.valueOf(nums2[1]))
cons = new float[Integer.valueOf(nums1[1])+1];
else
cons = new float[Integer.valueOf(nums2[1])+1];
for(int i=1;i0])*2;i+=2)
cons[Integer.valueOf(nums1[i])]+=Float.valueOf(nums1[i+1]);
for(int i=1;i0])*2;i+=2)
cons[Integer.valueOf(nums2[i])]+=Float.valueOf(nums2[i+1]);
int count = 0;
String str = "";
for (int i=cons.length-1;i>=0;i--)
if(cons[i]!=0){
str=str+" "+i+" "+String.format("%.1f",cons[i]);
count+=1;
}
str=count+str;
System.out.println(str);
}
}
https://www.patest.cn/contests/pat-a-practise/1003
主要用了Dijkstra算法,但由于要知道最短路径的条数,则需进行修改,当某一步选出一个未访问的顶点后,我们需要更新到其他点的最短距离,此时,如果距离比进行比较的短,则数量置为比较的那个的,如果一样,则相加。看了代码会更好理解什么意思。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 2 | 1336 |
#include
#include
#define MAX_DIS 10000
using namespace std;
int main()
{
int city_count,edge_count,from,to;
scanf("%d %d %d %d",&city_count,&edge_count,&from,&to);
int teams[city_count];
for (int i=0;i> teams[i];
int graph[city_count][city_count];
for(int i=0;i1;
for(int j=i+1;jfor (int i=0;iint t_f,t_t,t_l;
scanf("%d %d %d",&t_f,&t_t,&t_l);
graph[t_f][t_t] = t_l;
graph[t_t][t_f] = t_l;
}
int dis[city_count];//从出发点到各个点的最短距离
int total_teams[city_count];//从出发点到各个点所能汇集的队伍数
int nums[city_count];//最短路径数量
bool visited[city_count];
visited[from] = true;
dis[from] = 0;
nums[from] = 1;
total_teams[from] = teams[from];
for(int i=0; iif(i==from)continue;
visited[i] = false;
if(graph[from][i]!=MAX_DIS){
total_teams[i] = teams[from]+teams[i];
dis[i] = graph[from][i];
nums[i] = 1;
}
else{
total_teams[i] = 0;
dis[i] = MAX_DIS;
}
}
for (int i=0;i1;++i){
int min_i = -1,min_dis = MAX_DIS+1;
for (int j=0;jif(visited[j])continue;
if (dis[j]true;
for (int j=0; jif(visited[j])continue;
if (min_dis+graph[j][min_i]//注意是和新的一样,而不是为1。
total_teams[j] = total_teams[min_i] + teams[j];
}
else if(min_dis+graph[j][min_i]==dis[j]){
nums[j] = nums[j] +nums[min_i];//注意是两者相加,因为这两种路径都是最短的。
if(total_teams[j]<(total_teams[min_i] + teams[j]))
total_teams[j] = total_teams[min_i] + teams[j];
}
}
}
cout << nums[to] <<" "<
https://www.patest.cn/contests/pat-a-practise/1004
先是老老实实建立一棵树,再用层次遍历,然后发现“段错误”,找了好久没找到问题,贴上代码,有空再研究研究,最后还是简单的用数组实现了。
一段又长又有问题的代码1:
#include
#include
#include
#define MAX_NODES 100
using namespace std;
struct Node{
int child_count;
Node* sibling;
Node* first_child;
Node():child_count(0),sibling(NULL),first_child(NULL){}
};
int main()
{
int n,m;
scanf("%d %d",&n,&m);
Node *content[MAX_NODES];
for (int i=0;iint p_id,fc_id,childnum;
Node root;
for(int k=0;kscanf("%d %d %d",&p_id,&childnum,&fc_id);
if(content[p_id]==NULL){
content[p_id] = &root;
}
content[p_id]->child_count = childnum;
Node c_node;
content[p_id]->first_child = &c_node;
content[fc_id] = &c_node;
int last_sibing_id = fc_id;
int sibing_id;
for(int i=0;i1;++i){
Node s_node;
scanf("%d",&sibing_id);
content[sibing_id] = &s_node;
content[last_sibing_id]->sibling = &s_node;
last_sibing_id = sibing_id;
}
}
queue *que1 = new queue ;
queue *que2 = new queue ;
queue *tmp;
que1->push(&root);
int sum=0;
Node *p;
while(!que1->empty()){
p = que1->front();
que1->pop();
if(p != &root)
cout<<" ";
if(p->child_count==0)
++sum;
else{
que2->push(p->first_child);
p = p->first_child;
while(p->sibling!=NULL){
que2->push(p->sibling);
p = p->sibling;
}
}
tmp = que1;
que1 = que2;
que2 = tmp;
cout<0;
}
}
部分正确的代码2:
#include
using namespace std;
int main()
{
int n, m, k, node, c;
scanf("%d %d", &n, &m);
int level[n+1],output[n],maxlevel=-1;
bool have_child[n+1];
for (int i=0; i1; ++i){
level[i] = -1;
output[i] = 0;
have_child[i] = false;
}
level[1] = 0;
for(int i=0; iscanf("%d %d", &node, &k);
if(k>0)
have_child[node] = true;
for(int j=0; jscanf("%d",&c);
level[c] = level[node]+1;
if(level[c]>maxlevel)
maxlevel = level[c];
}
}
for(int i=1; i1; ++i){
if(!have_child[i])
output[level[i]]+=1;
}
printf("%d",output[0]);
for(int i=1; i<=maxlevel; ++i)
printf(" %d",output[i]);
}
别人家的正确代码3:
#include
#include
#include
#include
using namespace std;
int level[100], book[100], maxlevel = -1;
vector<int> v[100];
void bfs() {
queue<int> q;
q.push(1);
level[1] = 0;
while(!q.empty()) {
int index = q.front();
q.pop();
maxlevel = max(level[index], maxlevel);
if(v[index].size() == 0)
book[level[index]]++;
for(int i = 0; i < v[index].size(); i++) {
q.push(v[index][i]);
level[v[index][i]] = level[index] + 1;
}
}
}
int main() {
int n, m, k, node, c;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d",&node, &k);
for(int j = 0; j < k; j++) {
scanf("%d", &c);
v[node].push_back(c);
}
}
bfs();
printf("%d", book[0]);
for(int i = 1; i <= maxlevel; i++)
printf(" %d", book[i]);
return 0;
}
https://www.patest.cn/contests/pat-a-practise/1005
字符串与整数之间的转换。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 2 | 480 |
#include
#include
#include
using namespace std;
int main()
{
string str;
string en[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
getline(cin,str,'\n');
char *p = &str[0];
int sum = 0;
int len = str.length();
for(int i=0; i'0');
stringstream res;
string ss;
res << sum;
ss = res.str();
p = &ss[0];
cout << en[*p++-'0'];
len = ss.length();
for(int i=0; i1; ++i)
cout << " "<< en[*p++-'0'];
}
https://www.patest.cn/contests/pat-a-practise/1006
字符串处理。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 2 | 492 |
#include
#include
using namespace std;
int get_time(char *str)
{
int hour, minute, second;
sscanf(str, "%d:%d:%d",&hour, &minute, &second);
return hour*3600+minute*60+second;
}
int main()
{
char *id = new char[15];
char *in = new char[8];
char *out = new char[8];
int c;
scanf("%d", &c);
scanf("%s %s %s", id, in, out);
char earliest[15];
char latest[15];
int e_time,l_time;
e_time = get_time(in);
l_time = get_time(out);
strcpy(&earliest[0],id);
strcpy(&latest[0],id);
int tmp;
for(int i=0; i1; ++i){
scanf("%s %s %s", id, in, out);
tmp = get_time(in);
if(tmpstrcpy(&earliest[0],id);
}
tmp = get_time(out);
if(tmp>l_time){
l_time = tmp;
strcpy(&latest[0],id);
}
}
cout << earliest << " " << latest;
}
https://www.patest.cn/contests/pat-a-practise/1007
两个循环,外循环从一个非0数开始作为子集起点,内循环往后找最小的下标使得和最大,然后比较这次循环和有史以来最大的和的大小,若大于则替换。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 23 | 484 |
#include
using namespace std;
int main()
{
int n,c;
scanf("%d",&n);
int nums[n];
for(int i=0; i"%d",&c);
nums[i] = c;
}
int max_begin,max_end,max_value;
int t_max_begin,t_max_end,t_max_value,tmp;
max_begin = max_end = -1;
t_max_begin = t_max_end = -1;
t_max_value = max_value = tmp = 0;
for(int i=0; iif(nums[i]<0) continue;
t_max_begin = t_max_end = i;
t_max_value = tmp = nums[i];
for(int j=i+1;jif(tmp>t_max_value){
t_max_end = j;
t_max_value = tmp;
}
}
if(t_max_value>max_value){
max_begin = t_max_begin;
max_end = t_max_end;
max_value = t_max_value;
}
}
if(t_max_begin==-1)
printf("%d %d %d",0,nums[0],nums[n-1]);
else
printf("%d %d %d",max_value,nums[max_begin],nums[max_end]);
}
https://www.patest.cn/contests/pat-a-practise/1008
简单题。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 3 | 480 |
#include
using namespace std;
int main()
{
int n,now;
scanf("%d",&n);
int last = 0;
int time = 0;
for(int i=0; i"%d",&now);
if(now>last)
time += (6*(now-last)+5);
else
time += (4*(last-now)+5);
last = now;
}
printf("%d",time);
}
https://www.patest.cn/contests/pat-a-practise/1009
开始是打算用一个字符串来存整个输出值的,然后第一位字符随着循环而每次执行加一操作,后来发现这是个坑…字符加一并不是整数加一啊,如字符'9'
加一变成A
而不是10
,需注意。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 2 | 484 |
#include
#include
#include
using namespace std;
int main()
{
int k1, k2, e;
float c;
scanf("%d", &k1);
int p1_exp[k1];
float p1_coe[k1];
for (int i=0; iscanf("%d %f", &e, &c);
p1_exp[i] = e;
p1_coe[i] = c;
}
scanf("%d", &k2);
int p2_exp[k2];
float p2_coe[k2];
for (int i=0; iscanf("%d %f", &e, &c);
p2_exp[i] = e;
p2_coe[i] = c;
}
float coe[p1_exp[0]+p2_exp[0]+1];
for(int i=0; i0]+p2_exp[0]+1; ++i)
coe[i] = 0;
for(int i=0; ifor(int j=0; jchar *s= new char[400];
char *p = s;
int cou = 0;
for(int i=p1_exp[0]+p2_exp[0]; i>=0; --i){
if(coe[i] != 0.0){
cou += 1;
sprintf(p, " %d %.1f", i, coe[i]);
p += strlen(p);//p指针增加的是后续字符串的长度,与里面的数据类型(int,float)无关,不能写成p += (sizeof(int)+sizeof(float)+2)
}
}
*p = '\0';
printf("%d%s", cou, s);
}
https://www.patest.cn/contests/pat-a-practise/1010
此题巨坑..
暴力遍历会在测试点7超时。 二分搜索后,如果不考虑溢出会在测试点10报错。
最后测试10还是没有通过…有待解决。
#include
#include
#include
#include
using namespace std;
int to_num(char *n)
{
if (*n <= '9')
return (*n-'0');
else
return (*n-'a'+10);
}
unsigned long long to_dec(char *n, unsigned long long rad)
{
int l = strlen(n);
unsigned long long sum = 0;
for (int i=0; ipow(rad,l-i-1);
++n;
}
return sum;
}
int get_largest_digit(char *n)
{
int d = to_num(n);
int l = strlen(n);
++n;
for (int i=0; i1; ++i){
d = max(d,to_num(n++));
}
return d;
}
unsigned long long bin_search(char *n,unsigned long long low,unsigned long long high,unsigned long long dec_n)
{
unsigned long long res = high+1;
unsigned long long tmp = res;
unsigned long long val;
while(low <= high){
val = to_dec(n,(low+high)/2);
if(val>dec_n){
high = (low+high)/2 - 1;
}else if(val == dec_n){
res = (low+high)/2;
high = (low+high)/2 - 1;
}else{
low = (low+high)/2 + 1;
}
}
if (res==tmp)return -1;
return res;
}
int main()
{
int tag;
unsigned long long rad1, rad2;
char *n1 = new char[11];
char *n2 = new char[11];
scanf("%s %s %d %llu", n1, n2, &tag, &rad1);
if(tag==2){
char *tmp = n2;
n2 = n1;
n1 = tmp;
}
unsigned long long dec_n = to_dec(n1, rad1);
unsigned long long low = get_largest_digit(n2)+1;
unsigned long long high = max(dec_n+1,low);
unsigned long long res = bin_search(n2, low, high, dec_n);
if (res==-1)printf("Impossible");
else
printf("%llu",res);
return 0;
}
https://www.patest.cn/contests/pat-a-practise/1011
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 3 | 484 |
#include
using namespace std;
int main()
{
char c[4] = {"WTL"};
float sum = 1.0;
for (int i=0; i<3; ++i){
int maxchar = 0;
float maxvalue = 0.0;
for (int j=0; j<3; ++j){
float tmp;
scanf("%f",&tmp);
if (maxvalue <= tmp){
maxvalue = tmp;
maxchar = j;
}
}
sum *= maxvalue;
printf("%c ", c[maxchar]);
}
printf("%.2f", (sum*0.65-1)*2);
return 0;
}
https://www.patest.cn/contests/pat-a-practise/1012
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 10 | 616 |
#include
#include
#include
using namespace std;
int main()
{
char c[5] = {"CMEA"};
map<string,vector<int>> mp;
int m,n;
scanf("%d%d", &m, &n);
for (int i=0; istring id;
int s, a=0;
vector<int> score;
cin >> id;
for (int j=0; j<3; ++j){
scanf("%d",&s);
score.push_back(s);
a += s;
}
score.push_back(a);
mp.insert(pair<string, vector<int>>(id, score));
}
for(int i=0; iint rank[4] = {1,1,1,1};
string id;
cin >> id;
map<string, vector<int>>::iterator it;
int score[4];
it = mp.find(id);
if (it != mp.end()){
for (int j=0; j<4; ++j)
score[j] = (it->second)[j];
for(map<string, vector<int>>::iterator itr = mp.begin(), itr_end = mp.end(); itr!=itr_end; itr++){
for(int j=0; j<4; ++j){
if((itr->second)[j] > score[j])
++ rank[j];
}
}
int best_r = m+1;
int best_j;
for (int j=3,k=0; k<4; j=(j+1)%4,++k){
if (rank[j] < best_r){
best_j = j;
best_r = rank[j];
}
}
printf("%d %c\n", best_r, c[best_j]);
}
else
printf("N/A\n");
}
return 0;
}
https://www.patest.cn/contests/pat-a-practise/1013
图的遍历,需要加的路数是最大连通子图的个数减一。由于每一次check城市都调用至少一次DFS,所需时间有点长(最后一个测试点肯定很复杂….)。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 111 | 4204 |
#include
#include
using namespace std;
int main()
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int mp[n+1][n+1];
bool maketo[n+1];
for (int i=0; i1; ++i){
maketo[i] = false;
for (int j=0; j1; ++j)
mp[i][j] = 0;
}
int from, to;
for (int i=0; iscanf("%d%d", &from, &to);
mp[from][to] = 1;
mp[to][from] = 1;
}
int lost;
for (int l=0; lscanf("%d",&lost);
int lines = 0;
for (int i=1; i1; ++i){
if(i==lost || maketo[i]) continue;
maketo[i] = true;
stack<int> sta;
sta.push(i);
while(!sta.empty()){
int now = sta.top();
sta.pop();
for (int j=1; j1; ++j){
if(j!=lost && mp[now][j] && !maketo[j]){
sta.push(j);
maketo[j] = true;
}
}
}
++lines;
}
printf("%d\n",lines-1);
for (int i=0; i1; ++i)
maketo[i] = false;
}
}
https://www.patest.cn/contests/pat-a-practise/1014
有个小陷阱就是:如果是在17:00前开始服务的,无论多晚,都要输出结束时间,而不是只看最后结束时间是不是大于17:00。
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 3 | 488 |
#include
#include
using namespace std;
//检查是不是所有的窗口都服务完了
bool all_over(int *p, int n)
{
bool over = true;
for (int i=0; iif(*p>0){
over = false;
break;
}
++p;
}
return over;
}
int main()
{
int n, m ,k ,q;
scanf("%d%d%d%d", &n, &m, &k, &q);
int leave_time[k+1],time_need[k+1];//结束时间,处理时间
queue<int> lines[n];//每一排的队伍
int remaining_time[n], processing[n];//每一个窗口处理完当前顾客还需时间
int tmp;
for(int i=1; i1; ++i){
scanf("%d", &tmp);
time_need[i] = tmp;
}
int next = 1;
for (int i=0; ifor (int j=0; jfor(int i=0; iif(!lines[i].empty()){
processing[i] = lines[i].front();
remaining_time[i] = time_need[lines[i].front()];
}
else{
processing[i] = -1;
remaining_time[i] = -1;
}
}
int base_time = 480;
while(!all_over(remaining_time,n)){
++base_time;
for(int i=0; i1;
if(remaining_time[i]==0){
leave_time[processing[i]] = base_time;
lines[i].pop();
if(next<=k){
lines[i].push(next);
++next;
}
if(!lines[i].empty()){
processing[i] = lines[i].front();
remaining_time[i] = time_need[lines[i].front()];
}
else{
processing[i] = -1;
remaining_time[i] = -1;
}
}
}
}
for (int i=0; iint q_id;
scanf("%d", &q_id);
int hour = leave_time[q_id]/60;
int minute = leave_time[q_id]%60;
if(leave_time[q_id]-time_need[q_id]>=1020){
printf("Sorry\n");
}
else{
printf("%02d:%02d\n",hour,minute);
}
}
}
https://www.patest.cn/contests/pat-a-practise/1147
主要是堆的使用,
语言 | 用时ms | 内存 |
---|---|---|
C++ (g++ 4.7.2) | 27 | 752 |
#include
#include
#include
using namespace std;
struct Node{
int value;
Node *left;
Node *right;
Node *parent;
Node():value(0),left(NULL),right(NULL),parent(NULL){}
};
Node nodes[1000];
int m, n;
vector<int> output;
void post_order(int index)
{
if(2*index+12*index+1);
if(2*index+22*index+2);
output.push_back(nodes[index].value);
}
int main()
{
scanf("%d %d", &m, &n);
int c;
for(int i=0; ifor (int j=0; jscanf("%d", &c);
nodes[j].value = c;
if((j-1)/2>=0){
nodes[j].parent = &nodes[(j-1)/2];
if((j-1)%2==0)
nodes[(j-1)/2].left = &nodes[j];
else
nodes[(j-1)/2].right = &nodes[j];
}
}
bool tag = true;
bool is_max = true;
if (nodes[0].value>nodes[1].value)
is_max = true;
else
is_max = false;
for(int j=n-1; j>0; --j){
if(is_max){
if(nodes[j].parent->valuefalse;
break;
}
}else{
if(nodes[j].parent->value>nodes[j].value){
tag = false;
break;
}
}
}
if(is_max && tag)
printf("Max Heap\n");
else if(!is_max && tag)
printf("Min Heap\n");
else
printf("Not Heap\n");
post_order(0);
printf("%d",output[0]);
for(int j=1; jprintf(" %d",output[j]);
printf("\n");
output.clear();
}
}