Cattle class network - Huawei machine exercises Trial

Please refer to Gangster answer:

https://blog.csdn.net/FBB360JAVA/ by reference please java implementation of this program bloggers

The following is an example exercise python implementation:

1. Given a string describing arithmetic expressions, calculation results crude
print(eval(input()))

2. Calculate the last word length of the string, the word separated by a space

x = input()
print(len(x.split()[-1]))

3. Write a program that accepts a string of letters and numbers, and a character, the number of input and output of the character string contained. not case sensitive.

a=input().upper()
b=input().upper()
print(a.count(b))

4. obviously want to invite some students in the school together to do a survey, in order to test the objectivity, his first with the computer-generated random integer (N≤1000) between 1 and 1000 N number, for which duplicate numbers , but one, the rest the same number removed, different numbers corresponding to different students learn numbers. Then put these numbers in ascending order, to get the students to do research in accordance with good row order. Please help obviously complete "de-duplication" and "sort" work (with a test case where there may be multiple sets of data, we hope to be able to handle correctly).

The Param INPUT
n-number of input random number
array inputArray n random integers
Return Value
after OutputArray output processing random integer
Note: input parameters to ensure the correctness test, respondents without authentication. More than one set of test cases.
Sample input interpretation:
The sample has two tests
a first group of three digits, are: 2.2.1.
11 is a second set of numbers, namely: 10,20,40,32,67,40,20,89,300,400,15.

while True:
    try:
        n = int(input())
        l = []   #list = []   空列表
        for i in range(n):
            l.append(int(input()))
        for j in sorted(set(l)):  #sorted() 函数对所有可迭代的对象进行排序操作。默认升序
            print(j)  #集合(set)是一个无序的不重复元素序列。集合在Python内部通过哈希表实现,其本征无序,输出时所显示的顺序具有随机性
 
    except:
        break

The continuous input string, by the length of the output string array to a new resolution after each string 8; 8 is not an integer multiple of the length of a string of numbers 0 Please back up, not the empty string processing.

def str(s):
    if len(s) < 8 and len(s) > 0:
        s =s+ '0'*(8 - len(s))
        print(s)
    elif len(s) == 8:
        print(s)
    elif len(s) > 8:
        print(s[:8])
        str(s[8:])
        
while True:
    try:
        s = input()
        str(s)
    except:
        break

6. write a program that accepts a hexadecimal number, the output value of the decimal representation. (Plurality of sets of simultaneous input)

while True:
    try:
        print(int(input(),16))
    except:
        break

7. functions: enter a positive integer, in accordance with all of the prime factors of the order of small to large outputs it (e.g., prime factors is 180 22 335)

Finally, a number also have space behind

Description:
Function Interface Description:
public String the getResult (Long ulDataInput)
Input parameters:
Long ulDataInput: positive integer entered
Return value:
String

a=int(input())
def qiuzhishu(x):
    iszhi=1
    for i in range(2,int(x**0.5+2)):
        if x%i==0:
            iszhi=0
            print(str(i),end=" ")
            qiuzhishu(int(x/i))
            break
    if iszhi==1:
        print(str(x),end=" ")
qiuzhishu(a)

8. write a program, receiving a positive floating-point value, the output value of the approximate integer values. If the value is greater than or equal to 5 after the decimal point, rounding up; less than 5, it is rounded down.

a=float(input())
print(int(a+0.5))v

A data table and a table index record contains the values (integer int range), make the same record table index combined value is about the same index summing operation, the output value output in accordance with the key in ascending order.
Input Description:
to enter the number of key-value pairs
and pairs of input value and the index value, separated by a space

Output Description: The
key value of the combined output (multi-line)

#!/usr/bin/env python
# coding:utf-8
count = input()
d = {}
for i in range(count):
    index,value = map(int,raw_input().split())
    if index in d:
        d[index] += value
    else:
        d[index] = value
for key,value in d.items():
    print key,value
print d

With c ++ implementation:

#include <iostream>
#include <map>
#include <utility>
#include <algorithm>

using namespace std;

void print(pair <int, int> mp)
{
    cout << mp.first << " " << mp.second << endl;
}

int main(int argc, char **argv)
{
    int num;
    int key, val;
    map <int, int> mp;
    map <int, int> ::iterator it;

    while(cin >>num)
    {
        mp.clear();

        for(int i=0; i<num; i++) {
            cin >> key >> val;

            it = mp.find(key);
            if(it == mp.end()) {
                mp[key] = val;
            } else {
                mp[key] = mp[key] + val;
            }
        }

        for_each(mp.begin(), mp.end(), print);
    }

    return 0;
}

10. Enter a integer int type, according to the reading order from right to left, returns a new integer number no repeating

a=input()[::-1]
l=[]
for i in a:
    if i not in l:
        l.append(i)
str=''
for j in l:
    str=str+j
print(str)

11. Write a function of the number of different character strings contained in the calculation. ACSII character codes in the range (0 to 127), indicating the end of line feed character, the character is not in the inside. It is not beyond the scope of statistics

a=input()
res=set()
for i in a:
    if ord(i) in range(128):
        res.add(i)
print(len(res))

12. Enter a integer, the integer output as a string in reverse order

Does not consider the case of negative numbers, if the number containing 0, 0 also contain the reverse form, such as input 100, the output is 001

a=input()
if int(a)<0:
    a=a[1:]
    b=-int(a[::-1])
else:
    b=int(a[::-1])
print(b)

Guess you like

Origin www.cnblogs.com/liuwei-xd/p/12335183.html