数学实验
数学实验题目链接
题目描述
牛牛在做数学实验。
老师给了牛牛一个数字 n n n ( 1 ≤ n ≤ 1 0 18 1≤n≤10^{ 18} 1≤n≤1018),牛牛需要不断地将所有数位上的值做乘法运算,直至最后数字不发生变化为止。
请你帮牛牛计算一下,最后生成的数字为多少?
输入样例
55
输出样例
0
55 -> 5 * 5 = 25 -> 2 * 5 = 10 -> 1 * 0 = 0
按照题意直接写,最后返回值在 0 ~ 9 之间。
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param n long长整型 老师给牛牛的数字
* @return int整型
*/
int mathexp(long long n) {
// write code here
int a[20] = {
0};
if(n >= 0 && n <= 9) return n;
else
{
while( n > 9)
{
long long t = n;
int idx = 0;
while(t > 0)
{
a[idx++] = t%10;
t /= 10;
}
int cnt = 1;
for(int i = 0; i < idx; i++)
cnt *= a[i];
n = cnt;
}
return n;
}
}
};
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# @param n long长整型 老师给牛牛的数字
# @return int整型
#
class Solution:
def mathexp(self , n ):
# write code here
ans = n;
while ans > 9:
t = ans
now = 1
while t:
now *= t%10
t //= 10
ans = now
return ans
奇怪的排序问题
奇怪的排序问题题目链接
题目描述
操场上有 n n n 个人排成一队,这 n n n 个人身高互不相同,可将他们的身高视为一个 1 到 n n n 的排列。
这时需要把队伍变成升序,也就是从矮到高排序。
每次可以选择一个人,让这个人和在他身后的人比高矮,如果比对方高,则交换位置并继续下一次比较,直到比对方矮或者已经在队尾。
现在给出数n和一个 1 到 n n n 的排列,求最少的选择次数,使队伍变为升序。
输入样例
4,[4,1,2,3]
输出样例
1
n ≤ 1 0 6 n \leq 10^6 n≤106
数据包含一个整数 n n n 和一个含有 n n n 个元素的数组,表示从队头到队尾的人的身高。输出一个整数表示答案。
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param n int整型
* @param a int整型vector
* @return int整型
*/
int wwork(int n, vector<int>& a) {
// write code here
int result = 0;
for(int i=0;i<a.size();i++)
{
for(int j=i+1;j<a.size();j++)
{
if(a[j]<a[i])
{
result++;
break;
}
}
}
return result;
}
};
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# @param n int整型
# @param a int整型一维数组
# @return int整型
#
class Solution:
def wwork(self , n , a ):
# write code here
minn = n + 1
ans = 0
for i in range(n-1, -1, -1):
if a[i] > minn:
ans += 1
minn = min(minn, a[i])
return ans
XOR和
XOR和题目链接
题目描述
牛牛最近学会了异或操作,于是他发现了一个函数 f ( x ) = x ⊕ ( x − 1 ) f(x)=x\oplus (x-1) f(x)=x⊕(x−1),现在牛牛给你一个数 n \mathit n n,他想知道 ∑ i = 1 n f ( i ) \sum_{i=1}^n f(i) ∑i=1nf(i)的值是多少,请你告诉他。
输入样例
4
输出样例
12
1 ≤ n ≤ 1 0 9 1≤n≤10^9 1≤n≤109
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param n int整型
* @return long长整型
*/
long long Sum(int n) {
// write code here
long long ans = 0;
for(int i = 1; i <= n; i++)
ans = ans + (i^(i-1));
return ans;
}
};
结论: f ( x ) = 2 × f ( x / 2 ) + 1 f(x) = 2\times f(x/2) + 1 f(x)=2×f(x/2)+1
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param n int整型
* @return long长整型
*/
long long Sum(int n) {
// write code here
if(n == 1) return 1;
else return n + 2*Sum(n/2);
}
};
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# @param n int整型
# @return long长整型
#
class Solution:
def Sum(self , n ):
# write code here
if n == 1:
return 1
return n + 2*self.Sum(n//2)
总结
看了大三下的课表,考个 j b jb jb 研,能上就上,学习英语,有书读就读,一场上白银,还是太菜了。暴力出奇迹,c 过了我就想 b 也许暴力也可以过, O ( n 2 ) O(n^2) O(n2),现在看来准备保研的事情从大一认真准备,大三必然会有结果,基本上都保上了,也是最容易的事情,双非保研真的是大学里面最简单的事情。