PAT/字符串处理习题集(一)
B1006. 换个格式输出整数 (15)
Description:
让我们用字母B来表示“百”、字母S表示“十“,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数。例如234应该被输出为BBSSS1234,因为它有2个“百”、3个“十”、以及个位的4。
Input:
每个测试输入包含1个测试用例,给出正整数n(<1000)。
Output:
每个测试用例的输出占一行,用规定的格式输出n。
Sample Input1:
234
Sample Output1:
BBSSS1234
Sample Input2:
23
Sample Output2:
SS123
#include <cstdio> int main()
{
int n;
scanf("%d", &n); int num = , ans[];
while(n != ) {
ans[num++] = n%;
n /= ;
} for(int i=num-; i>=; --i) {
if(i == ) {
for(int j=; j<ans[i]; ++j)
printf("B");
} else if(i == ) {
for(int j=; j<ans[i]; ++j)
printf("S");
} else {
for(int j=; j<=ans[i]; ++j)
printf("%d", j);
}
} return ;
}
B1021. 个位数统计 (15)
Description:
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。
Input:
每个输入包含1个测试用例,即一个不超过1000位的正整数N。
Output:
对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。
Sample Input:
100311
Sample Output:
0:2
1:3
3:1
#include <cstdio>
#include <cstring> #define MaxSize 1010
char List[MaxSize]; int main()
{
//freopen("E:\\Temp\\input.txt", "r", stdin); gets(List); int len = strlen(List), ans[] = {};
for(int i=; i<len; ++i)
++ans[List[i]-'']; for(int i=; i<; ++i) {
if(ans[i] != )
printf("%d:%d\n", i, ans[i]);
} return ;
}
B1031. 查验身份证 (15)
Description:
一个合法的身份证号码由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
现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。
Input:
输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。
Output:
按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出“All passed”。
Sample Input1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
Sample Output1:
12010X198901011234
110108196711301866
37070419881216001X
Sample Input2:
2
320124198808240056
110108196711301862
Sample Output2:
All passed
#include <cstdio>
#include <cstring> int w[] = {, , , , , , , , , , , , , , , , };
char change[] = {'', '', 'X', '', '', '', '', '', '', '', ''}; int main()
{
int n;
char str[];
bool flag = true;
scanf("%d", &n);
for(int i=; i<n; ++i) {
scanf("%s", str);
int j, last = ;
for(j=; j<; ++j) {
if(!(str[j]>='' && str[j]<='')) break;
last += (str[j]-'')*w[j];
}
if(j < ) {
flag = false;
printf("%s\n", str);
} else {
if(change[last%] != str[]) {
flag = false;
printf("%s\n", str);
}
}
}
if(flag == true) printf("All passed\n"); return ;
}
B1002. 写出这个数 (20)
Description:
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
Input:
每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
Output:
在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
Sample Input:
1234567890987654321123456789
Sample Output:
yi san wu
#include <cstdio>
#include <cstring> int main()
{
char str[];
gets(str); int ans[], num = , len = strlen(str);
int sum = ;
for(int i=; i<len; ++i)
sum += str[i]-'';
while(sum != ) {
ans[num++] = sum%;
sum /= ;
}
char change[][] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; for(int i=num-; i>=; i--) {
printf("%s", change[ans[i]]);
if(i != ) printf(" ");
else printf("\n");
} return ;
}
B1009. 说反话 (20)
Description:
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
Input:
测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
Output:
每个测试用例的输出占一行,输出倒序后的句子。
Sample Input:
Hello World Here I Come
Sample Output:
Come I Here World Hello
#include <cstdio> #define MaxSize 100
char List[MaxSize][MaxSize]; int main()
{
//freopen("E:\\Temp\\input.txt", "r", stdin); int num = ;
while(scanf("%s", List[num]) != EOF)
num++; for(int i=num-; i>=; --i) {
printf("%s", List[i]);
if(i != ) printf(" ");
else printf("\n");
} return ;
}
#include <cstdio>
#include <cstring> #define MaxSize 100
char List[MaxSize], ans[MaxSize][MaxSize]; int main()
{
//freopen("E:\\Temp\\input.txt", "r", stdin); gets(List); int num = , counter = , len = strlen(List);
for(int i=; i<len; ++i) {
if(List[i] != ' ')
ans[num][counter++] = List[i];
else {
counter = ;
++num;
}
} for(int i=num; i>=; --i) {
printf("%s", ans[i]);
if(i != ) printf(" ");
else printf("\n");
} return ;
}
B1014. 福尔摩斯的约会 (20)
Description:
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
Input:
输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。
Output:
在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。
Sample Input:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
Sample Output:
THU 14:04
#include <cstdio>
#include <cstring> int main()
{
char week[][] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
char str1[], str2[], str3[], str4[];
gets(str1), gets(str2), gets(str3), gets(str4); int len1 = strlen(str1), len2 = strlen(str2), len3 = strlen(str3), len4 = strlen(str4);
int i;
for(i=; 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]>='' && str1[i]<='') {
printf("%02d:", str1[i]-'');
break;
} else if(str1[i]>='A' && str1[i]<='N') {
printf("%02d:", str1[i]-'A'+);
break;
}
}
}
for(i=; 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 ;
}
A1061. Dating (20)
Description:
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:
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:
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
#include <cstdio>
#include <cstring> #define MaxSize 100
char str1[MaxSize], str2[MaxSize], str3[MaxSize], str4[MaxSize];
char date[][] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; int main()
{
//freopen("E:\\Temp\\input.txt", "r", stdin); gets(str1), gets(str2), gets(str3), gets(str4); int i = , len1 = strlen(str1), len2 = strlen(str2), len3 = strlen(str3), len4 = strlen(str4);
for(; i<len1&&i<len2; ++i) {
if(str1[i]==str2[i] && str1[i]>='A'&& str1[i]<='G') {
printf("%s ", date[str1[i]-'A']);
break;
}
}
for(++i; i<len1&&i<len2; ++i) {
if(str1[i]==str2[i]) {
if(str1[i]>='' && str1[i]<='') {
printf("%02d:", str1[i]-'');
break;
} else if(str1[i]>='A' && str1[i]<='N') {
printf("%02d:", str1[i]-'A'+);
break;
}
}
}
for(i=; i<len3&&i<len4; ++i) {
if(str3[i] == str4[i]) {
if((str3[i]>='a'&&str1[i]<='z') || (str3[i]>='A'&&str3[i]<='Z')) {
printf("%02d", i);
break;
}
}
} return ;
}
PAT/字符串处理习题集(一)的更多相关文章
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PAT 字符串-02 删除字符串中的子串
/* 2 *PAT 字符串-02 删除字符串中的子串 3 *2015-08-09 4 作者:flx413 5 */ #include<stdio.h> #include<string ...
- PAT/查找元素习题集
B1004. 成绩排名 (20) Description: 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. Input: 每个测试输入包含1个测试用例,格式为: 第1行: ...
- pat 团体赛练习题集 L2-008. 最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- PAT/简单模拟习题集(一)
B1001.害死人不偿命的(3n+1)猜想 (15) Description: 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉 ...
- PAT/图形输出习题集
B1027. 打印沙漏 (20) Description: 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ...
- PAT/简单模拟习题集(二)
B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...
- pat 团体赛练习题集 L2-007. 家庭房产
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产: ...
- pat 团体赛练习题集 L2-006. 树的遍历
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点的个数.第二行给出其后序遍历序 ...
随机推荐
- [转] Oracle sql 查询突然变慢 -- 案例分析
转自:http://jingyan.baidu.com/article/8275fc868ce57946a03cf692.html 一条sql突然执行变慢,耗时9秒,应用是不能改的,只能从数据库方面下 ...
- 匿名函数 lambda表达式(lambda expression)
阅读g2log时,发现有两行代码居然看不懂. 1. auto bg_call = [this, log_directory]() {return pimpl_->backgroundChang ...
- sublime插件 cssComb实现css自动排序及格式化
cssComb是一个实现css代码自动排序,当然顺便也实现了代码的格式化 安装: 首先需要打开sublime搜索安装csscomb插件(前提是已经安装了sublime的package control) ...
- JAVA第4次作业
package fuzhi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExc ...
- 几个开源XMPP Android客户端简单比较
想做个基于xmpp的即时通讯工具,服务端已经基本成型了.当然需要客户端需要配合,PC端基于spark进行改造,手机端先从Android入手(IOS估计一个人是搞不过来了). 原本Android开发 ...
- iOS Xcode添加ios10.0的路径
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
- redis的 rdb 和 aof 持久化的区别 [转]
aof,rdb是两种 redis持久化的机制.用于crash后,redis的恢复. rdb的特性如下: Code: fork一个进程,遍历hash table,利用copy on write,把整个d ...
- Ext开场表单布局设计
var form = new Ext.form.FormPanel({ labelAlign: 'right', labelWidth: 60, buttonAlign: 'center', titl ...
- (shell )函数
一.定义格式 [function] 函数名() { 命令表 } 二.调用方法 先定义,后使用,直接输入函数名,不需要圆括号() 三.函数参数传递方法 可以利用位置参数或者变量进行传递 #! /bin/ ...
- js execCommand
JavaScript中的execCommand()命令详解及实例展示 标签: javascriptbuttonfunctioninputobjectdelete 2012-05-07 16:08 14 ...