假期编程

此博客链接:https://www.cnblogs.com/ping2yingshi/p/12285269.html

1.Palindromes _easy version(32min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2029

Problem Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
题解:此题就是回文数的判断,定义一个字符数组来存储字符串,但是一开始我没有想好是用scanf()还是getchar(),所以写的慢了一点。
代码如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
   
    int n; 
    scanf("%d",&n); 
    while(n>0)
    { 
        int i;
        char ch[100];
        scanf("%s",&ch);
        int str=strlen(ch);
        int flag=1;
        for(i=0;i<str;i++)
        {
            if(ch[i] !=ch[str-1-i]){
                flag=0;
                printf("no");
                break;
            }
            
                
        }
        if(flag==1)
            printf("yes");
        printf("\n");
        n--;            
        }
  
    return 0;
    
}

 2.An easy problem(27min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2055

Problem Description
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).
Input
On the first line, contains a number T.then T lines follow, each line is a case.each case contains a letter and a number.
Output
for each case, you should the result of y+f(x) on a line.
Sample Input
6
R 1
P 2
G 3
r 1
p 2
g 3
Sample Output
19
18
10
-17
-14
-4
题解:此题还是比较简单的,我用了两种方法
方法1:定义两个数组来存放大小写字母的值,把输入的字母减去a或者A的ASCII值得到相应字母的数组下标,再用数组中的值加上y,就可以得到结果了。
方法2:不定义数组,直接把输入的字母减去a或者A,这里还能进一步优化,当输入字母是小写字母时,可以用a减去输入的字母再减去1,得到题目给的函数值,当输入字母时大写字母时,用输入的字母减去A再加上1,得到题目给的函数值,最后再加上y得到所求结果。
代码1如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int ct[26]={-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22,-23,-24,-25,-26};
    int cp[26]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
    
    int n; 
    scanf("%d",&n); 
    while(n>0)
    { 
        char ch;
        int y=0;
        int sum;
        int i;
        getchar();
        scanf("%c %d",&ch,&y);
        if(ch>='a'&&ch<='z')
        {
            i=ch-'a';
            sum=ct[i]+y;
        }
        if(ch>='A'&&ch<='Z')
        {
            i=ch-'A';
            sum=cp[i]+y;
        }
        printf("%d",sum);
        printf("\n");
        n--;            
        }
  
    return 0;
    
}

 

代码2如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int n; 
    scanf("%d",&n); 
    while(n>0)
    { 
        char ch;
        int y=0;
        int i;
        getchar();
        scanf("%c %d",&ch,&y);
        if(ch>='a')
        {
            i='a'-ch-1;
        }
        else 
        {
            i=ch-'A'+1;
          
        } 
        
        printf("%d",i+y);
        printf("\n");
        n--;            
        }
  
    return 0;
    
}

 

posted @ 2020-02-08 21:42  萍2樱释  阅读(226)  评论(0编辑  收藏  举报