检查密码 (15 分)
NOTE :
- 检测点4是一个长字符,字符数组设大些。
- 监测点应该有含空格的字符串,别用scanf。
#include<stdio.h>
#include<string.h>
int main() {
int n, nFlag, wFlag, pFlag, iFlag;
char password[90];
scanf("%d", &n);
scanf("\n",&password);
for(int i=0; i<n; i++) {
nFlag = wFlag = pFlag = iFlag = 0;
fgets(password,90,stdin);
if(password[strlen(password)-1] == '\n')
password[strlen(password)-1] = '\0';
if(strlen(password) < 6)
printf("Your password is tai duan le.");
else {
for(int j=0; password[j]!='\0'; j++) {
int ch = password[j];
if(ch<='9' && ch>='0')
nFlag = 1;
else if((ch<='z' && ch>='a')|| (ch<='Z' && ch>='A'))
wFlag = 1;
else if(ch == '.')
pFlag = 1;
else //一个字符既不是字母也不是数字也不是点
iFlag = 1;
}
if(iFlag)//有非法字符
printf("Your password is tai luan le.");
else if(!wFlag)//只有数字没字母
printf("Your password needs zi mu.");
else if(!nFlag)//只有字母没数字
printf("Your password needs shu zi.");
else
printf("Your password is wan mei.");
}
if(i != n-1)
printf("\n");
}
}
射击比赛 (20 分)
NOTE : 不需要开平方计算距离,只需要用xx + y y比大小。id只有4位所以可以用int记录id,最后格式化输出就好了 。
#include<stdio.h>
int main() {
int n, id, x, y, maxId, minId;
int max = 0;
int min = 20000;
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%d %d %d", &id, &x, &y);
if(x*x + y*y > max) {
max = x*x + y*y;
maxId = id;
}
if(x*x + y*y <min) {
min = x*x + y*y;
minId = id;
}
}
printf("%.4d %.4d", minId, maxId);
return 0;
}
是否存在相等的差 (20 分)
NOTE : 数字1 到 N 的差的范围为 0 到 N-1,定义一个长度为N的数组,循环过程中将相减的结果当下标,数组自增1。
#include<stdio.h>
#include<math.h>
int main() {
int n, x, flag =0;
scanf("%d", &n);
int arr[10000]= {0};
for(int i=0 ; i<n; i++) {
scanf("%d", &x);
arr[abs(x-i-1)]++;//编号从1开始。
}
//1到N的差的范围 0 到N-1;
for(int i=n-1; i>=0; i--) {
if(arr[i] > 1) {
if(flag)//有一个输出之后,之后每行前边输出一个换行符
printf("\n");
printf("%d %d",i, arr[i]);
flag = 1;
}
}
}
外观数列 (20 分)
思路:两个指针一前一后,front,和rear,front和rear指向字符相同,则front向前移动,cnt++;若不同,在在字符数组中依次存入rear指向的内容和cnt 的值。
NOTE : 最后一个测试点很大,定义一个大字符数组。
#include<stdio.h>
#include<string.h>
#define LEN 100000
void f(char res[]);
int main() {
int n;
char res[LEN];
scanf("%s %d", &res, &n);
while(n-- > 1)
f(res);
printf("%s",res);
return 0;
}
void f(char res[]) {
int i = 0;
int cnt = 0;
int rear = 0;
int front = 0;
char _res[LEN] = "";
while(res[rear] != '\0') {
if(res[front] != res[rear]) {
_res[i++] = res[rear];
_res[i++] = cnt+'0';
rear = front;
cnt = 0;
}
front++;
cnt++;
}
_res[i] = '\0';
for(i=0; _res[i]!='\0'; i++)
res[i] = _res[i];
res[i] = '\0';
}