1、输入为多组n、m,第一项为n,之后每一项为前一项的平方根,求前m项目的和。
import math
def get_sum(n,m):
sum=n
a=n
for i in range(m-1):
a = math.sqrt(a)
sum+=a
return round(sum,2)
try:
while True:
n,m = input().split(" ")
sum = get_sum(int(n),int(m))
print(sum)
except:
pass
2、输入m,n(三位数),求所有m到n范围内的水仙花数。“水仙花数”是指一个3位数,它的各位数字的立方和等于其本身,
比如,153 = 13 + 53 + 33.
import math
def count(n):
a=int(n%10)
b=int(n/10%10)
c=int(n/100)
if math.pow(a,3)+math.pow(b,3)+math.pow(c,3)==n:
return 0
else:
return n
try:
m,n=input().split(" ")
m=int(m)
n=int(n)
if m>n:
t=n
n=m
m=t
if m>=100 and n<1000:
for i in range(m,n+1):
if count(i)==0:
print(i)
except:
pass
3、将字符串按照字符大小顺序输出
input_s = str(input())
result_list=list(input_s)
#result_list.sort() #不能区分大小写
for index,i in enumerate(result_list):
for x in range(index+1,len(result_list)):
if ord(result_list[index]) > ord(result_list[x]):
t=result_list[index]
result_list[index] = result_list[x]
result_list[x] = t
result_s="".join(result_list)
print(result_s)
4、统计字符串中的字符个数,并按照字符顺序输出
import operator
input_s = str(input())
result={}
for i in input_s:
# if i in result:
# result[i] = result[i]+1
# else:
# result[i] = 1
result[i] = result.get(i, 0) + 1
# 按照key排序
for i in sorted(result):
print('%s %d'%(i,result[i]))
# 按照键值排序
# res = sorted(res.items(),key = operator.itemgetter(1),reverse = True)
print(sorted(result.items(), key = lambda kv:(kv[1], kv[0])))
5、C++提取数字
#include <iostream>
#include <string>
using namespace std;
void main() {
string s = "sgcd1#10.0 ///中文1\
sgcd22#20.0 ///中文2 \
sgcd33#30.0 //中文3";
string flag = "#";
int position = 0;
int i = 1;
string s1 = s;
while ((position = s.find(flag, position)) != string::npos)
{
cout << "position " << i << " : " << position << endl;
cout << "result :" << s1.substr(position+1, 4) << endl;
position++;
i++;
}
# 提取出所有#后面的四位数字10.0,20.0,30.0
getchar();
}
—————————更新中—————————