算法学习--Day7

今天多做一些杂题练习一下。

第一题:

题目描述

在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍然能防止情报被轻易的识别。我们给出一种最简的的加密方法,对给定的一个字符串,把其中从a-y,A-Y的字母用其后继字母替代,把z和Z用a和A替代,则可得到一个简单的加密字符串。

输入描述:

读取这一行字符串,每个字符串长度小于80个字符

输出描述:

对于每组数据,输出每行字符串的加密字符串。
示例1

输入

Hello! How are you!

输出

Ifmmp! Ipx bsf zpv!


//
// Created by 陈平 on 2018/4/17.
//

#include <iostream>
#include "string.h"
#include <string.h>
using namespace std;

int main(){

    string test;
    getline(cin,test);
        int i = 0;
        while (test[i]!='')
        {
            if(test[i]<='z'&&test[i]>='a' ) cout<<char((test[i]-'a'+1)%26+'a');

            else if (test[i]>='A'&& test[i]<='Z') cout<<char((test[i]-'A'+1)%26+'A');
            else cout<<test[i];
            i++;
        }
        cout<<endl;

    return 0;
}

这道题坑在使用getline上,不然他会遇到空格就跳过了。

题目描述

N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。帽子的颜色用“red”,“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。

输入描述:

多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。
下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。

注意:白鼠的重量各不相同。

输出描述:

每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。
示例1

输入

3
30 red
50 blue
40 green

输出

blue
green
red

//
// Created by 陈平 on 2018/4/17.
//

#include <iostream>
#include "string.h"
#include <string.h>
#include "algorithm"
using namespace std;
struct E{
    int num;
    char color[1000];
};
bool cmp(E a,E b){
    return a.num>b.num;
}
int main(){
int n;
while (scanf("%d",&n)!=EOF){
    E buf[101];
    for (int i = 0; i < n; ++i) {
        cin>>buf[i].num>>buf[i].color;
    }
    sort(buf,buf+n,cmp);
    for (int j = 0; j <n ; ++j) {
        cout<<buf[j].color<<endl;
    }

}

    return 0;
}

题目描述

已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k.

输入描述:

若干个非负整数c,c的位数<=30
每行一个c

输出描述:

每一个c的结果占一行
1) 若存在满足 c%k == 0 的k,输出所有这样的k,中间用空格隔开,最后一个k后面没有空格。
2) 若没有这样的k则输出"none"

注意整数溢出问题
不要对-1进行计算
示例1

输入

30
72
13

输出

2 3 5 6
2 3 4 6 8 9
none


//
// Created by 陈平 on 2018/4/17.
//

#include <iostream>
#include "string.h"
#include <string.h>
#include "algorithm"
using namespace std;

int main(){
char input[50];
while (scanf("%s",input)!=EOF){
    int noflag=0;
    int len = strlen(input);
    if(input[0]=='-') continue;
    int flag =1;
    for (int k = 2; k <=9 ; ++k) {
        int mid=0;

        for (int i = 0; i < len; ++i) {
            int cut = input[i] - '0';
            mid = (mid*10+cut)%k;
        }

        if(mid==0) {
            noflag = 1;
            if(flag==1) {cout<<k;
            flag=0;}
            else{
                cout<<" "<<k;
            }
        }


    }
    if(noflag==0) cout<<"none";
    cout<<endl;
}

    return 0;
}

这道题目关联高精度题目,所以我们自定义了除法方法去计算。关键一步在于mid的那一行,处理好这一行就ok。

原文地址:https://www.cnblogs.com/Pinging/p/8950454.html