20220402简单题
20220402简单题
1.HJ21
1.1题目
1.2我的解法
python:
def lower2code(letter):
if letter in 'abc':
return '2'
elif letter in 'def':
return '3'
elif letter in 'ghi':
return '4'
elif letter in 'jkl':
return '5'
elif letter in 'mno':
return '6'
elif letter in 'pqrs':
return '7'
elif letter in 'tuv':
return '8'
elif letter in 'wxyz':
return '9'
s = input()
for i in s:
if i>='A' and i<='Z':
if i != 'Z':
i = chr(ord(i.lower())+1)
else:
i = 'a'
print(i,end='')
elif i>='a' and i<='z':
i = lower2code(i)
print(i,end='')
else:
print(i,end='')
C语言:
#include<stdio.h>
#include<string.h>
char lower2code(char a);
int main(){
char s[101];
scanf("%s",s);
int length = strlen(s);
for(int i=0;i<length;i++){
if(s[i]>='A'&&s[i]<='Z'){
if(s[i]=='Z'){
s[i] = 'a';
}
else{
s[i] = s[i] + 32 + 1;
}
}
else if(s[i]>='a'&&s[i]<='z'){
s[i] = lower2code(s[i]);
}
}
for(int i=0;i<length;i++){
printf("%c",s[i]);
}
return 0;
}
char lower2code(char a){
if (a>='a'&&a<='c'){
return '2';
}
else if(a>='d'&&a<='f'){
return '3';
}
else if(a>='g'&&a<='i'){
return '4';
}
else if(a>='j'&&a<='l'){
return '5';
}
else if(a>='m'&&a<='o'){
return '6';
}
else if(a>='p'&&a<='s'){
return '7';
}
else if(a>='t'&&a<='v'){
return '8';
}
else if(a>='w'&&a<='z'){
return '9';
}
return NULL;
}
1.3优化与改进
- python与C语言用的方法过于累赘,但是没必要为了这道题纠结
1.4python处理ASCII码
python在处理ASCII码时,需要使用两个函数方法:
-
chr():将十进制数转化为基础字符
-
ord():将字符转化为十进制数
-
‘A’ 65
-
‘a’ 97
2.HJ22
2.1题目
2.2我的解法
python:
n = int(input())
while n != 0:
count = 0
remain = 0
while n > 0:
if n == 2:
count += 1
break
elif n==1 or n==0:
break
else:
count += n//3
remain = n%3 + n//3
n = remain
print(count)
n = int(input())
C语言:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
while(n!=0){
int count=0;
while(n>0){
if (n == 2){
count += 1;
break;
}
else if(n < 2){
break;
}
else{
count += n/3;
n = n%3+n/3;
}
}
printf("%d\n",count);
scanf("%d",&n);
}
return 0;
}
2.3优化与改进
- C语言与python的使用方法相同,应该这道题是可以适应不同的语言拥有不同的解法,但我还没有找到比较好的方法通用实现,现在还是先理清题目的关系,直接使用数学表达式来写吧
3.HJ23
3.1题目
3.2我的解法
python:
s = input()
letter_count = [0]*26
for i in range(len(s)):
letter_count[ord(s[i])-97] += 1
min_count = sorted(set(letter_count))[1]
for i in range(len(s)):
if letter_count[ord(s[i])-97] != min_count:
print(s[i],end='')
C语言:
#include<stdio.h>
#include<string.h>
#include<limits.h>
int main(){
char s[21];
int letter_count[26] = {0};
scanf("%s",s);
for(int i=0;i<strlen(s);i++){
letter_count[s[i]-'a'] += 1;
}
int min = INT_MAX;
for(int i=0;i<26;i++){
if(letter_count[i]!=0&&letter_count[i]<min){
min = letter_count[i];
}
}
for(int i=0;i<strlen(s);i++){
if(letter_count[s[i]-'a']!=min){
printf("%c",s[i]);
}
}
return 0;
}
3.3改进与优化
- python可以不用C语言的思路,python可以更简单,先写C语言被限制住了思维
4.HJ31
4.1题目
4.2我的解法
python:
s = input()
words = []
temp = ''
for i in s:
if (i >='a' and i<='z') or (i >='A' and i<='Z'):
temp += i
else:
if temp not in words:
words.append(temp)
temp = ''
if temp not in words and temp != '':
words.append(temp)
for i in range(len(words)-1,-1,-1):
if i == 0:
print(words[i],end='')
else:
print(words[i],end=' ')
-
存储空间超了,难受::>_<::
-
更改方式:才通过
s = input()
words = []
temp = ''
for i in s:
if (i >='a' and i<='z') or (i >='A' and i<='Z'):
continue
else:
s = s.replace(i,' ')
s = s.split(' ')
s = reversed(s)
print(' '.join(s))
C语言:
- 怎样都超出内存限制,很难受,只能借鉴别人的,可是自己换方法还是出错,因为他用库了!!!作弊行为@!@
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char s[10000];
gets(s);
int len = strlen(s);
int p1 = len - 1, p2 = len;
while(p1 >= 0)
{
while(p1 >= 0 && !isalpha(s[p1]))
p1--;
p2 = p1;
while(p1 >= 0 && isalpha(s[p1]))
p1--;
for(int i = p1 + 1; i <= p2; i++)
printf("%c", s[i]);
printf(" ");
}
return 0;
}
- 这个很厉害,我服气,因为我完全没想到
#include <stdio.h>
int main(void)
{
char words[500][21]={0};
short i=0;
scanf("%*[^a-zA-Z]");
while(scanf("%20[a-zA-Z]", words[i]) == 1)
{
i++;
scanf("%*[^a-zA-Z]");
}
while(i>1)
printf("%s ", words[--i]);
printf("%s\n", words[--i]);
return 0;
}
5.HJ34
5.1题目
5.2我的解法
python:
s = list(input())
s.sort()
print(''.join(s))
C语言:
#include<stdio.h>
#include<string.h>
int main(){
char s[1001];
scanf("%s",s);
for(int i=0;i<strlen(s);i++){
for(int j=i;j<strlen(s);j++){
if(s[i]>s[j]){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for(int i=0;i<strlen(s);i++){
printf("%c",s[i]);
}
return 0;
}
6.HJ35
6.1题目
6.2我的解法
python:
n = int(input())
new_start = 1
temp = 0
for i in range(0,n):
new_start += i
temp = new_start
a_part = []
a_part.append(temp)
for j in range(i+2,n+1,1):
temp += j
a_part.append(temp)
print(' '.join(map(str,a_part)))
C语言:
#include<stdio.h>
int main(){
int n=0;
scanf("%d",&n);
int new_start=1,temp=0;
for(int i=0;i<n;i++){
new_start += i;
temp = new_start;
printf("%d ",temp);
for(int j=i+2;j<n+1;j++){
temp += j;
printf("%d ",temp);
}
printf("\n");
}
return 0;
}
6.3改进与优化
- 调试了好久,不知道如果在机试时遇到会不会做不出来,害,这种题还得找规律,要记住规律啊。
- 各有各的方法,最主要还是得找到数学表达式吧