上机笔记3.6

*3.6 PAT B1006

让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。

输入格式:

每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。

输出格式:

每个测试用例的输出占一行,用规定的格式输出 n

输入样例 1:

234

输出样例 1:

BBSSS1234

输入样例 2:

23

输出样例 2:

SS123
#include<stdio.h>
int main() {
	int n;
	scanf("%d", &n);
	int a, b, c;
	a = n / 100;
	b = (n - 100 * a) / 10;
	c = n - 100 * a - 10 * b;
	for (int i = 0; i < a; i++) {
		printf("B");
	}
	for (int i = 0; i < b; i++) {
		printf("S");
	}
	for (int i = 1; i <= c; i++) {
		printf("%d", i);
	}
}

*3.6 PAT B1021

给定一个 k 位整数 N=d**k−110k−1+⋯+d1101+d0 (0≤d**i≤9, i=0,⋯,k−1, d**k−1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。

输入格式:

每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N

输出格式:

N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。

输入样例:

100311

输出样例:

0:2
1:3
3:1
#include<stdio.h>
#include<string.h>
int main() {
	char arr[1010];
	int count[10] = {};
	gets(arr);
	int len = strlen(arr);
	for (int i = 0; i < len; i++) {
		//char的0~9对应是数字的0~9 用 c-'0'
		//char R -> r  'R'-'A'+'a'
		count[arr[i] - '0']++;
	}
	for (int i = 0; i < 10; i++) {
		if (count[i] != 0) {
			printf("%d:%d\n", i, count[i]);
		}
	}
	return 0;
}

*3.6 PAT B1031

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入格式:

输入第一行给出正整数N(≤100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

输出格式:

按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出All passed

输入样例1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出样例1:

12010X198901011234
110108196711301866
37070419881216001X

输入样例2:

2
320124198808240056
110108196711301862

输出样例2:

All passed
#include<stdio.h>
#include<stdbool.h>
int main() {

	int n;
	scanf("%d", &n);

	char id[20];
	int w[20] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
	char change[11] = { '1','0','X','9','8','7','6','5','4','3','2' };
	bool flag = true;
	

	for (int i = 0; i < n; i++) {
		scanf("%s", id);
		int j, last = 0;
		bool dec = true;
		for (j = 0; j < 17; j++) {
			if (!(id[j] >= '0' && id[j] <= '9')) {
				flag = false;
				dec = false;
				break;
			}
			last = last + w[j] * (id[j] - '0');

		}

		if (!dec) {
			printf("%s\n", id);
		}
		//if (j < 17) {
		//	flag = false;
		//	printf("%s\n", id);
		//}
		else  if (change[last % 11] != id[17]) {
			flag = false;
			printf("%s\n", id);

		}
	}
	if (flag) {
		printf("All passed\n");
	}
	return 0;
}

*3.6 PAT B1002

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:

每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10100。

输出格式:

在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1234567890987654321123456789

输出样例:

yi san wu
#include<stdio.h>
#include<string.h>
int main() {

	char arr[110];
	gets(arr);
	int len = strlen(arr);
	int sum = 0;

	for (int i = 0; i < len; i++) {
		sum += arr[i] - '0';
	}
	

	int ans[10], num = 0;

	while (sum != 0){
		ans[num++] = sum % 10;
		sum /= 10;
	} 


	char change[10][5] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };

	for (int i = num - 1; i >= 0; i--) {
		printf("%s", change[ans[i]]);
		if (i != 0)
			printf(" ");
		else {
			printf("\n");
		}
	}
	return 0;

}

?*3.6 PAT B1014/A1061

B1014

大侦探福尔摩斯接到一张奇怪的字条:

我们约会吧! 
3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 23 点由数字 0 到 9、以及大写字母 AN 表示);后面两字符串第 1 对相同的英文字母 s 出现在第 4 个位置(从 0 开始计数)上,代表第 4 分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。

输入格式:

输入在 4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。

输出格式:

在一行中输出约会的时间,格式为 DAY HH:MM,其中 DAY 是某星期的 3 字符缩写,即 MON 表示星期一,TUE 表示星期二,WED 表示星期三,THU 表示星期四,FRI 表示星期五,SAT 表示星期六,SUN 表示星期日。题目输入保证每个测试存在唯一解。

输入样例:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

输出样例:

THU 14:04

A1061

Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week – that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

Sample Output:

THU 14:04
//多出关于ASCII码与数字的转换
#include<stdio.h>
#include<string.h>
int main() {

	char week[7][5] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
	char str1[70], str2[70], str3[70], str4[70];

	gets(str1);
	gets(str2);
	gets(str3);
	gets(str4);

	int len1 = strlen(str1);
	int len2 = strlen(str2);
	int len3 = strlen(str3);
	int len4 = strlen(str4);

	int i;
	for (i = 0; i < len1 && i < len2; i++) {
		if (str1[i] == str2[i] && str1[i] >= 'A' && str1[i] <= 'G') {
			printf("%s ", week[str1[i] - 'A']);
			break;
		}
	}
	for (i++; i < len1 && i < len2; i++) {
		if (str1[i] == str2[i]) {
			if (str1[i] >= '0' && str1[i] <= '9') {
				printf("%02d:", str1[i] - '0');
				break;
			}
			else if (str1[i] >= 'A' && str1[i] <= 'N') {
				printf("%02d:", str1[i] - 'A' + 10);
				break;
			}
		}
	}
	for (i = 0; i < len3 && i < len4; i++) {
		if (str3[i] == str4[i]) {
			if ((str3[i] >= 'A' && str3[i] <= 'Z') || (str3[i] >= 'a' && str3[i] <= 'z')) {
				printf("%02d", i);
				break;
			}
		}
	}
	return 0;
}

?*3.6 PAT B1024/A1073

B1024

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [±][1-9].[0-9]+E[±][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式:

每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

输出格式:

对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

输入样例 1:

+1.23400E-03

输出样例 1:

0.00123400

输入样例 2:

-1.2E+10

输出样例 2:

-12000000000

A1073

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [±][1-9].[0-9]+E[±][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent’s signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent’s absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000
//自己对于小数点后移操作比较难想到
#include<stdio.h>
#include<string.h>
int main() {

	char str[10010];
	gets_s(str);
	int len = strlen(str);

	if (str[0] == '-') {
		printf("-");
	}


	int pos = 0;

	while (str[pos] != 'E') {
		pos++;
	}


	int exp = 0;

	for (int i = pos + 2; i < len; i++) {
		exp = exp * 10 + (str[i] - '0');
	}
	//指数为0
	if (exp == 0) {
		for (int i = 1; i < pos; i++) {
			printf("%c", str[i]);
		}
	}


	if (str[pos + 1] == '-') {
		printf("0.");
		for (int i = 0; i < exp - 1; i++) {
			printf("0");
		}
		printf("%c", str[1]);
		for (int i = 3; i < pos; i++) {
			printf("%c", str[i]);
		}
	}
	else {
		for (int i = 1; i < pos; i++) {
			if (str[i] == '.') {
				continue;
			}
			printf("%c", str[i]);
			if (i == exp + 2 && pos - 3 != exp) {
				printf(".");
			}
		}
		for (int i = 0; i < exp - (pos - 3); i++) {
			printf("0");
		}
	}
	return 0;
}

*3.6 PAT B1048

本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。

输入格式:

输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:

1234567 368782971

输出样例:

3695Q8118
#include<stdio.h>
#include<string.h>
void reverse(char s[]) {
	
	int len = strlen(s);

	for (int i = 0; i < len / 2; i++) {
		int temp = s[i];
		s[i] = s[len - i - 1];
		s[len - i - 1] = temp;
	}

}

int main() {

	//char change[12] = { '1','2','3','4','5','6','7','8','9','J','Q','K' };
	char a[110] , b[110], ans[110] = {};
	scanf("%s %s", a, b);
	reverse(a);
	reverse(b);
	int lenA = strlen(a);
	int lenB = strlen(b);
	int len = lenA > lenB ? lenA : lenB;

	for (int i = 0; i < len; i++) {
		int numA = i < lenA ? a[i] - '0' : 0;
		int numB = i < lenB ? b[i] - '0' : 0;

		if (i % 2 == 0) {
			int temp = (numA + numB) % 13;
			if (temp == 10) {
				ans[i] = 'J';
			}
			else if (temp == 11) {
				ans[i] = 'Q';
			}
			else if (temp == 12) {
				ans[i] = 'K';
			}
			else {
				//数字0~9 转 字符0~9
				ans[i] = temp + '0';
			}
		}
		else {
			int temp = numB - numA;
			if (temp < 0) {
				temp += 10;
			}
			ans[i] = temp + '0';
		}
	}
	reverse(ans);
	puts(ans);
	return 0;

}

?*3.6 PAT A1001

Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991
#include<stdio.h>
#include<string.h>
void reverse(char s[]) {
	
	int len = strlen(s);

	for (int i = 0; i < len / 2; i++) {
		int temp = s[i];
		s[i] = s[len - i - 1];
		s[len - i - 1] = temp;
	}

}
int main() {

	long long a, b;
	scanf("%lld%lld", &a, &b);
	long sum = a + b;

	if (sum < 0) {
		printf("-");
		sum = -sum;
	}
	
	char str1[20];
	char str2[20];

	sprintf(str1, "%d", sum);
	printf("%s", str1);

	reverse(str1);
	printf("%s", str1);

	int len1 = strlen(str1);
	int i = 0, j = 0, count = 1;
	while ( i < len1) {
		if (count %3==0) {

			str2[j++] = ',';
			count++;
		}
		else {
			str2[j++] = str1[i++];
			count++;
		}
	}
	reverse(str2);
	printf("%s", str2);

}

*3.6 PAT A1005

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five
#include<stdio.h>
#include<string.h>
int main() {

	char num[10][10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
	char s[110];
	int digit[10];

	gets(s);
	int len = strlen(s);
	int sum = 0, numLen = 0;
	for (int i = 0; i < len; i++) {
		sum += s[i] - '0';
	}
	if (sum == 0) {
		printf("%s", num[0]);
	}
	else {
		while (sum != 0) {
			digit[numLen++] = sum % 10;
			sum /= 10;
		}
		for (int i = numLen - 1; i >= 0; i--) {
			printf("%s", num[digit[i]]);
			if (i != 0) {
				printf(" ");
			}
		}
	}
	return 0;
}

*3.6 PAT A1035

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:

For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

Sample Input 1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output 1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input 2:

1
team110 abcdefg332

Sample Output 2:

There is 1 account and no account is modified

Sample Input 3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output 3:

There are 2 accounts and no account is modified
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
struct node {
	char name[20], password[20];
	bool ischange;
}T[1005];

void crypt(node& t, int& cnt) {
	int len = strlen(t.password);
	for (int i = 0; i < len; i++) {
		if (t.password[i] == '1') {
			t.password[i] = '@';
			t.ischange = true;
		}
		else if (t.password[i] == '0') {
			t.password[i] = '%';
			t.ischange = true;
		}
		else if (t.password[i] == 'l') {
			t.password[i] = 'L';
			t.ischange = true;
		}
		else if (t.password[i] == 'O') {
			t.password[i] = 'o';
			t.ischange = true;
		}
	}
	if (t.ischange) {
		cnt++;
	}
}


int main() {

	int n;
	scanf("%d", &n);
	int cnt = 0;

	for (int i = 0; i < n; i++) {
		scanf("%s %s", T[i].name, T[i].password);
		T[i].ischange = false;
	}

	for (int i = 0; i < n; i++) {
		crypt(T[i], cnt);
	}

	if (cnt == 0) {
		if (n == 1) {
			printf("There is %d account and no account is modified", n);
		}
		else {
			printf("There are %d accounts and no account is modified", n);
		}
	}
	else {
		printf("%d\n", cnt);
		for (int i = 0; i < n; i++) {
			if (T[i].ischange) {
				printf("%s %s\n", T[i].name, T[i].password);
			}
		}
	}
	return 0;
}

?*3.6 PAT A1077

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle “nyan~” is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)
  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input Specification:

Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character’s spoken line. The spoken lines are case sensitive.

Output Specification:

For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.

Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int main() {
	
	int n, minLen = 256, ans = 0;
	char s[100][256];
	
	scanf("%d", &n);
	getchar();//接收回车
	for (int i = 0; i < n; i++) {
		gets(s[i]);
		int len = strlen(s[i]);
		if (len < minLen) {
			minLen = len;
		}

		for (int j = 0; j < len / 2; j++) {
			char temp = s[i][j];
			s[i][j] = s[i][len - j - 1];
			s[i][len - j - 1] = temp;
		}
	}

	for (int i = 0; i < minLen; i++) {
		char c = s[0][i];
		bool same = true;
		for (int j = 1; j < n; j++) {
			if (c != s[j][i]) {
				same = false;
				break;
			}
		}
		if (same)ans++;
		else break;
	}

	if (ans) {
		for (int i = ans - 1; i >= 0; i--) {
			printf("%c", s[0][i]);
		}
	}
	else {
		printf("nai");
	}
	return 0;

}

?*3.6 PAT A1082

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai
#include<stdio.h>
#include<string.h>
char num[10][5] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
char wei[5][5] = { "Shi","Bai","Qian","Wan","Yi" };

int main() {
	
	char str[15];
	gets(str);
	int len = strlen(str);
	int left = 0, right = len - 1;
	if (str[0] == '-') {
		printf("Fu");
		left++;
	}
	while (left + 4 <= right) {
		right -= 4;
	}
	while (left < len) {
		bool flag = false;
		bool isPrint = false;
		while (left <= right) {
			if (left > 0 && str[left] == '0') {
				flag = true;
			}
			else {
				if (flag == true) {
					printf(" ling");
					flag = false;
				}
				if (left > 0) printf(" ");
				printf("%s", num[str[left]] - '0');
				isPrint = true;
				if (left != right) {
					printf(" %s", wei[right - left - 1]);
				}
			}
			left++;
		}
		if (isPrint == true && right != len - 1) {
			printf(" %s", wei[(len - 1 - right) / 4 + 2]);
		}
		right += 4;
	}
	return 0;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值