1081. 检查密码 (15)
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点".",还必须既有字母也有数字。
输入格式:
输入第一行给出一个正整数 N(<=100),随后 N 行,每行给出一个用户设置的密码,为不超过80个字符的非空字符串,以回车结束。
输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
- 如果密码合法,输出“Your password is wan mei.”;
- 如果密码太短,不论合法与否,都输出“Your password is tai duan le.”;
- 如果密码长度合法,但存在不合法字符,则输出“Your password is tai luan le.”;
- 如果密码长度合法,但只有字母没有数字,则输出“Your password needs shu zi.”;
- 如果密码长度合法,但只有数字没有字母,则输出“Your password needs zi mu.”。
5 123s zheshi.wodepw 1234.5678 WanMei23333 pass*word.6输出样例:
Your password is tai duan le. Your password needs shu zi. Your password needs zi mu. Your password is wan mei. Your password is tai luan le.
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int N,have_number,have_yinwen,have_other,length; char s[110]; int check(){ have_number =0; have_yinwen = 0;have_other = 0; int yes = 1; for(int i=0;i<length;i++){ if(s[i]>='0' && s[i]<='9') have_number = 1; else if((s[i]>='a' && s[i]<='z')||(s[i]>='A'&& s[i]<='Z')){ have_yinwen = 1; } else if(s[i]=='.'){} else { have_other = 1; } } yes = !have_other && have_number && have_yinwen; return yes; } int main(void){ cin >> N; gets(s); for(int i=0;i<N;i++){ gets(s); length = strlen(s); if(length<6){ cout<<"Your password is tai duan le."; } else { if(check()){ cout<<"Your password is wan mei."; } else if(!check()&& have_other){ cout<<"Your password is tai luan le."; } else if(!check()&& !have_number){ cout<<"Your password needs shu zi."; } else if(!check()&& !have_yinwen){ cout<<"Your password needs zi mu."; } } if(i!=N-1) cout<<endl; } return 0; }
1082. 射击比赛 (20)
本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟。我们假设靶心在原点(0,0)。
输入格式:
输入在第一行中给出一个正整数 N(<= 10 000)。随后 N 行,每行按下列格式给出:
ID x y
其中 ID 是运动员的编号(由4位数字组成);x 和 y 是其打出的弹洞的平面坐标(x,y),均为整数,且 0 <= |x|, |y| <= 100。题目保证每个运动员的编号不重复,且每人只打 1 枪。
输出格式:
输出冠军和菜鸟的编号,中间空 1 格。题目保证他们是唯一的。
输入样例:3 0001 5 7 1020 -1 3 0233 0 -1输出样例:
0233 0001
#include<iostream> #include<algorithm> using namespace std; int N; struct point{ string name; int a,b; double dis; }A[10010]; int cmp(struct point x,struct point y){ return x.dis < y.dis; } int main(void){ cin >> N; for(int i=0;i<N;i++){ cin >> A[i].name >> A[i].a >>A[i].b; A[i].dis = A[i].a*A[i].a+A[i].b*A[i].b; } sort(A,A+N,cmp); if(N == 1){ cout << A[0].name << ' ' <<A[0].name; } else { cout << A[0].name << " " <<A[N-1].name; } return 0; }
1083. 是否存在相等的差 (20)
给定 N 张卡片,正面分别写上 1、2、……、N,然后全部翻面,洗牌,在背面分别写上 1、2、……、N。将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差?
输入格式:
输入第一行给出一个正整数 N(2 <= N <= 10000),随后一行给出 1 到 N 的一个洗牌后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字。
输出格式:
按照“差值 重复次数”的格式从大到小输出重复的差值及其重复的次数,每行输出一个结果。
输入样例:8 3 5 8 6 2 1 4 7输出样例:
5 2 3 3 2 2
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int N,a[10010],vis[10010]; int main(void){ cin >> N; for(int i=1;i<=N;i++){ cin >> a[i]; vis[(int)abs(a[i]-i)]++; } int cnt = 0; for(int i=0;i<=N;i++){ if(vis[i]>1) cnt++; } int sum = 0; if(cnt==0){ } else for(int i=N;i>=0;i--){ if(vis[i]>1){ sum++; cout<< i <<" "<< vis[i]; if(sum!=cnt) cout<< endl; } } return 0; }
1084. 外观数列 (20)
外观数列是指具有以下特点的整数序列:
d, d1, d111, d113, d11231, d112213111, ...
它从不等于 1 的数字 d 开始,序列的第 n+1 项是对第 n 项的描述。比如第 2 项表示第 1 项有 1 个 d,所以就是 d1;第 2 项是 1 个 d(对应 d1)和 1 个 1(对应 11),所以第 3 项就是 d111。又比如第 4 项是 d113,其描述就是 1 个 d,2 个 1,1 个 3,所以下一项就是 d11231。当然这个定义对 d = 1 也成立。本题要求你推算任意给定数字 d 的外观数列的第 N 项。
输入格式:
输入第一行给出[0,9]范围内的一个整数 d、以及一个正整数 N(<=40),用空格分隔。
输出格式:
在一行中给出数字 d 的外观数列的第 N 项。
输入样例:1 8输出样例:
1123123111
#include<cmath> #include<algorithm> #include<cstdio> #include<iostream> using namespace std; int N,len[50]; char s[50][100000]; int main(void){ int a; cin >> a >> N; len[1] = 1; s[1][1]=a+'0'; for(int i=2;i<=N;i++){ int cnt = 1; for(int j=1;j<=len[i-1];j++){ if(j==1 || s[i-1][j]!=s[i-1][j-1]){ s[i][cnt++]=s[i-1][j]; int sum = 1; while(j+1<=len[i-1] && s[i-1][j+1]==s[i-1][j]){ sum++; j++; } s[i][cnt++]=sum+'0'; } } len[i] = cnt-1; // len[i] = ? } for(int i=1;i<=len[N];i++) cout << s[N][i]; }
1085. PAT单位排行 (25)
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。
输入格式:
输入第一行给出一个正整数N(<=105),即考生人数。随后N行,每行按下列格式给出一个考生的信息:
准考证号 得分 学校
其中“准考证号”是由6个字符组成的字符串,其首字母表示考试的级别:“B”代表乙级,“A”代表甲级,“T”代表顶级;“得分”是 [0,100] 区间内的整数;“学校”是由不超过6个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。
输出格式:
首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:
排名 学校 加权总分 考生人数
其中“排名”是该单位的排名(从1开始);“学校”是全部按小写字母输出的单位码;“加权总分”定义为“乙级总分/1.5 + 甲级总分 + 顶级总分*1.5”的整数部分;“考生人数”是该属于单位的考生的总人数。
学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。
输入样例:10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypu输出样例:
5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2
自己这题超时了,两个测试点没有通过
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; struct school{ char name[100]; double sum; int cnt; int r; }sc[100010]; int N,cnt; int cmp(struct school x,struct school y){ if(x.sum != y.sum) return x.sum > y.sum; if(x.cnt != y.cnt) return x.cnt<y.cnt; return strcmp(x.name,y.name); } int main(void){ cin >> N; if(N==0) return 0; for(int i=0;i<N;i++){ char id[100]; int mark; char school_name[100]; scanf("%s%d%s",id,&mark,school_name); int len = strlen(school_name); //change for(int j=0;j<len;j++) if(school_name[j] >= 'A' &&school_name[j]<='Z') school_name[j]=school_name[j]+32; //find int have = 0,pos; for(int j=0;j<cnt && !have;j++){ if(strcmp(school_name,sc[j].name)==0){ pos = j; have=1; } } if(!have){ strcpy(sc[cnt].name,school_name); sc[cnt].sum=0; pos = cnt; cnt++; } //find if(id[0]=='A') sc[pos].sum+=mark*1.0; else if(id[0]=='B') sc[pos].sum+=mark*1.0/1.5; else if(id[0]=='T') sc[pos].sum+=mark*1.5; sc[pos].cnt++; } sort(sc,sc+cnt,cmp); /* for(int i=0;i<cnt;i++){ if(i==0) sc[i].r=1; else if((int)(sc[i].sum)==(int)(sc[i-1].sum)) { sc[i].r= sc[i-1].r; } else { sc[i].r= i+1; } } */ printf("%d ",cnt); int r = 1; for(int i=0;i<cnt;i++){ if(i==0) r = 1; else if((int)(sc[i].sum)!=(int)(sc[i-1].sum)) r = i+1; printf("%d %s %d %d",r,sc[i].name,(int)(sc[i].sum),sc[i].cnt); if(i!=cnt-1) printf(" "); } return 0; }
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int has[402321276+100]; struct school{ char name[100]; double sum; int cnt; }sc[100010]; int N,cnt; int cmp(struct school x,struct school y){ if(x.sum != y.sum) return x.sum > y.sum; if(x.cnt != y.cnt) return x.cnt<y.cnt; return strcmp(x.name,y.name); } int main(void){ cin >> N; if(N==0) return 0; for(int i=0;i<N;i++){///N char id[100]; int mark; char school_name[100]; scanf("%s%d%s",id,&mark,school_name); int len = strlen(school_name); //change int h = 0; for(int j=0;j<len;j++) { if(school_name[j] >= 'A' &&school_name[j]<='Z') school_name[j]=school_name[j]+32; h = h * 27 + school_name[j]-'a'; } for(int j=0;j<6-len;j++) h = h * 27 + 27; //find int pos; if(has[h]==1){ for(int j=0;j<cnt;j++){ if(strcmp(school_name,sc[j].name)==0){ pos = j; break; } } } else { strcpy(sc[cnt].name,school_name); sc[cnt].sum=0; pos = cnt; cnt++; has[h]=1; } //find if(id[0]=='A') sc[pos].sum+=mark*1.0; else if(id[0]=='B') sc[pos].sum+=mark*1.0/1.5; else if(id[0]=='T') sc[pos].sum+=mark*1.5; sc[pos].cnt++; } sort(sc,sc+cnt,cmp); printf("%d ",cnt); int r = 1; for(int i=0;i<cnt;i++){ if(i==0) r = 1; else if((int)(sc[i].sum)!=(int)(sc[i-1].sum)) r = i+1; printf("%d %s %d %d",r,sc[i].name,(int)(sc[i].sum),sc[i].cnt); if(i!=cnt-1) printf(" "); } return 0; }
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; struct school{ char name[100]; double sum; int cnt; int r; }sc[100010]; int N,cnt; /* int cmp(struct school x,struct school y){ if(x.sum != y.sum) return x.sum > y.sum; if(x.sum == y.sum && x.cnt != y.cnt) return x.cnt<y.cnt; return strcmp(x.name,y.name); } */ struct cmpFunctor{ bool operator()(const school &x,const school &y){ if(x.sum != y.sum) return x.sum > y.sum; if(x.sum == y.sum && x.cnt != y.cnt) return x.cnt<y.cnt; return strcmp(x.name,y.name); } }; int main(void){ cin >> N; if(N==0) return 0; for(int i=0;i<N;i++){ char id[100]; int mark; char school_name[100]; scanf("%s%d%s",id,&mark,school_name); int len = strlen(school_name); //change for(int j=0;j<len;j++) if(school_name[j] >= 'A' &&school_name[j]<='Z') school_name[j]=school_name[j]+32; //find int have = 0,pos; for(int j=0;j<cnt && !have;j++){ if(strcmp(school_name,sc[j].name)==0){ pos = j; have=1; } } if(!have){ strcpy(sc[cnt].name,school_name); sc[cnt].sum=0; pos = cnt; cnt++; } //find if(id[0]=='A') sc[pos].sum+=mark*1.0; else if(id[0]=='B') sc[pos].sum+=mark*1.0/1.5; else if(id[0]=='T') sc[pos].sum+=mark*1.5; sc[pos].cnt++; } sort(sc,sc+cnt,cmpFunctor()); /* for(int i=0;i<cnt;i++){ if(i==0) sc[i].r=1; else if((int)(sc[i].sum)==(int)(sc[i-1].sum)) { sc[i].r= sc[i-1].r; } else { sc[i].r= i+1; } } */ printf("%d ",cnt); int r = 1; for(int i=0;i<cnt;i++){ if(i==0) r = 1; else if((int)(sc[i].sum)!=(int)(sc[i-1].sum)) r = i+1; printf("%d %s %d %d",r,sc[i].name,(int)(sc[i].sum),sc[i].cnt); if(i!=cnt-1) printf(" "); } return 0; }