“事物的发展不以人的意志为转移”. PEACE & LOVE
1031 查验身份证
思路: 权重数组最后添加一位1,这样就可以对十八位身份证求和而不用区别前十七位和十八位,取余后判断即可。
#include<stdio.h>
#include<ctype.h>
#define LEN 20
int main() {
int cnt, _cnt, weight[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
char m[] = "10X98765432";
scanf("%d\n", &cnt);
_cnt = 0;
while(cnt-- > 0) {
char id[LEN]="";
int ch, index, sum, flag;
index = sum = flag = 0;
while((ch=getchar()) != '\n') {
if(!isdigit(ch) && index!=17)
flag = 1;
sum += (ch-'0') * weight[index];
id[index++] = ch;
}
id[index] = '\0';
if(flag || id[--index]!=m[(sum-id[index]+'0')%11]) {
printf("%s\n", id);
_cnt++;
}
}
if(!_cnt)
printf("All passed");
return 0;
}
1032 挖掘机技术哪家强
思路: 学校编号作为下标存储各校成绩,在读数据过程中确定得分最高的学校编号。最后一个测试用例给的数据很多,10的五次不够用。
#include<stdio.h>
#define LEN 101000
int main(){
int arr[LEN], cnt, index, max = -1;
scanf("%d", &cnt);
while(cnt-- > 0){
scanf("%d %d", &index, &arr[0]);
arr[index] += arr[0];
if(arr[index] > arr[max] || max==-1)
max = index;
}
printf("%d %d", max, arr[max]);
return 0;
}
1033 旧键盘打字
思路: 首先对坏掉的键:通过getchar读入,若存在‘+’,将flag置为1,之后的大写字母都无法输出。 对标准输入的字符挨个处理,每读入一个字符遍历一次坏键数组,坏键数组中不存在则输出。
#include<stdio.h>
#include<ctype.h>
#define LEN 100000
int main() {
int ch, flag, index;
char keys[LEN], stdIn[LEN];
flag = index = 0;
while((ch=getchar()) != '\n') {
if(ch == '+')
flag = 1;
keys[index++] = ch;
}
while((ch=getchar()) != '\n') {
int i = 0;
if(flag && isupper(ch))
continue;
for(; i<index; i++) {
if(toupper(ch) == keys[i])
break;
}
if(i == index)
putchar(ch);
}
return 0;
}
1034 有理数四则运算
思路: 就是一个找最大公约数格式化输出的问题,但最后两个测试点用long也过不了, 想不明白了。
#include<stdio.h>
#include<math.h>
#define LEN 200
//21474837/1 21474847/1
long GetGcd(long a, long b) {
long d = a%b;
while(d) {
a = b;
b = d;
d = a%b;
}
return b;
}
long _abs(long n) {
if(n<0)
return -1*n;
return n;
}
void GetString(long numerator, long denominator, char num[]) {
long common, a, b, d, i = 0;
common = GetGcd(_abs(numerator), _abs(denominator));
numerator /= common;
denominator /= common;
a = _abs(numerator);
b = _abs(denominator);
d = a/b;
a %=b ;
if(numerator<0 || denominator<0) {
num[i++] = '(';
num[i++] = '-';
}
if(d) {
num[i++] = d+'0';
}
if(d && a) {
num[i++] = ' ';
num[i++] = a+'0';
} else if(!d)
num[i++] = a+'0';
if(b != 1) {
num[i++] = '/';
num[i++] = b+'0';
}
if(numerator<0 || denominator<0)
num[i++] = ')';
num[i] = '\0';
}
int main() {
char num1[LEN], num2[LEN], res[LEN];
long common, a1, b1, a2, b2;
scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
GetString(a1, b1, num1);
GetString(a2, b2, num2);
GetString(a1*b2+a2*b1, b1*b2, res);
printf("%s + %s = %s\n",num1, num2, res);
GetString(a1*b2-a2*b1, b1*b2, res);
printf("%s - %s = %s\n",num1, num2, res);
GetString(a1*a2, b1*b2, res);
printf("%s * %s = %s\n",num1, num2, res);
if(a2 == 0)
printf("%s / %s = Inf",num1, num2);
else {
GetString(a1*b2, b1*a2, res);
printf("%s / %s = %s",num1, num2, res);
}
return 0;
}
失败的一天 /叹气.jpg