Today's task, five questions a day, is the result of four days, and I plan to write it in Python and C.
Question source: https://www.luogu.com.cn/training/102#problems
1. [Deep basis 4. Example 2] Find the minimum value
Question link: https://www.luogu.com.cn/problem/P5718
Question description
gives nnn andnnn integersai a_iai, please this nnWhat is the smallest value among n integers?
Input format
The first line enters a positive integer nnn , represents the number of numbers.
Enter nn in the second linen non-negative integers, representinga 1 , a 2 … an a_1,a_2 \dots a_na1,a2…an, separated by spaces.
Output format
Output a non-negative integer representing this nnThe smallest value among n non-negative integers .
Example #1
Sample input #1
8
1 9 2 6 0 8 1 7
Sample output #1
0
hint
Data guaranteed, n ≤ 100 n\le100n≤100one0 ≤ ai ≤ 1000 0\ anda_i \and0≤ai≤1000。
code(python)
num = int(input())
a = list(map(int, input().split()))
a.sort()
print(a[0])
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int min = 1000;
for(int i = 1;i <= n;i++)
{
int num;
scanf("%d",&num);
if(num < min)
{
min = num;
}
}
printf("%d\n",min);
return 0;
}
2. [Deep Base 4. Example 3] Classification Average
Question link: https://www.luogu.com.cn/problem/P5719
Question description
Given nnn andkkk , will go from 1 tonnAll positive integers between n can be divided into two categories: Category A numbers can be kk is divisible (that is,kkmultiples of k ), while class B numbers cannot. Please output the average of these two types of numbers, accurate to1 11 digit, separated by spaces.
The data guarantees that the number of both types of numbers will not be 0 00。
Input format
Enter two positive integers nnn andkkk。
Output format
Output one line, two real numbers, representing the average number of class A and class B respectively. Accurate to one decimal place.
Example #1
Sample input #1
100 16
Sample output #1
56.0 50.1
hint
Data guarantee, 1 ≤ n ≤ 10000 1 \leq n\leq 100001≤n≤10000, 1 ≤ k ≤ 100 1 \leq k \leq 100 1≤k≤100。
code(python)
# num = int(input())
#
# a = list(map(int, input().split()))
# a.sort()
# print(a[0])
n, k = map(int, input().split(" "))
i = 1
B_num = 0
B_sum = 0
A_num = 0
A_sum = 0
while i <= n:
if i % k == 0:
A_num += 1
A_sum += i
i += 1
else:
B_sum += i
B_num += 1
i += 1
print(round(A_sum/A_num, 1),round(B_sum/B_num, 1))
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n, k;
scanf("%d%d",&n,&k);
int x = 0,y = 0;
int sum = 0,num = 0;
for(int i = 1;i <= n;i++)
{
if(i % k == 0)
{
sum = sum + i;
x++;
}
if(i % k != 0)
{
num = num + i;
y++;
}
}
double a = 1.0*sum/x*1.0;
double b = 1.0*num/y*1.0;
printf("%.1lf %.1lf",a,b);
return 0;
}
3. [Deep Foundation 4.Example 4] One-foot Chai
Question link: https://www.luogu.com.cn/problem/P5720
Question description
"Zhuangzi" said, "If you take half of a one-foot stick every day, it will be inexhaustible for eternity." On the first day there is a length of aaStarting from the next day, half of the stick must be cut off every day (dividing2 2 each time2 , rounded down). On the next few days, the length of the stick will become1 11?
Input format
Enter a positive integer aaa , represents the length of the stick.
Output format
Output a positive integer, indicating the number of days when the length of the stick will become 1 11。
Example #1
Sample input #1
100
Sample output #1
7
hint
Data guarantee, 1 ≤ a ≤ 1 0 9 1 \le a\le 10^91≤a≤109。
code(python)
# num = int(input())
#
# a = list(map(int, input().split()))
# a.sort()
# print(a[0])
import math
n = int(input())
sum = 1
half = n
while half != 1:
half = int(half / 2)
sum += 1
print(sum)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
long long n;
scanf("%lld",&n);
int sum = 1;
long long half = n;
while(half !=1)
{
half = half/2;
sum++;
}
printf("%d\n",sum);
return 0;
}
4. [Deep basis 4. Example 6] Digital right triangle
Question link: https://www.luogu.com.cn/problem/P5721
Question description
gives nnn , please output a right angle whose side length isnnn number right triangle. All numbers are2 2Composed of 2 digits, if there is no 2 22 digits add leading0 00。
Input format
Enter a positive integer nnn。
Output format
Output the numerical right triangle as required by the question.
Example #1
Sample input #1
5
Sample output #1
0102030405
06070809
101112
1314
15
hint
Data guarantee, 1 ≤ n ≤ 13 1\le n\le131≤n≤13。
code(python)
# num = int(input())
#
# a = list(map(int, input().split()))
# a.sort()
# print(a[0])
import math
n = int(input())
i = n
x = 1
while i >= 0:
j = i - 1
# print("i:",i)
while j >= 0:
# print("j:", j)
if x < 10:
print("0%d" % x, end="")
x += 1
else:
print("%d" % x, end="")
x += 1
j -= 1
print("")
i -= 1
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int x = 1;
for(int i = n;i >= 0;i--)
{
for(int j = i-1;j >= 0;j--)
{
if(x < 10)
{
printf("0%d",x);
x++;
}
else
{
printf("%d",x);
x++;
}
}
printf("\n");
}
return 0;
}
5. [NOIP1998 Popularization Group] Sum of factorials
Question link: https://www.luogu.com.cn/problem/P1009
Question description
Calculate with high accuracy S = 1 ! + 2 ! + 3 ! + ⋯ + n ! S = 1! + 2! + 3! + \cdots + n!S=1!+2!+3!+⋯+n ! (n ≤ 50 n \le 50n≤50)。
where !
represents factorial, defined as n ! = n × ( n − 1 ) × ( n − 2 ) × ⋯ × 1 n!=n\times (n-1)\times (n-2)\times \cdots \times 1n!=n×(n−1)×(n−2)×⋯×1 . For example,5 ! = 5 × 4 × 3 × 2 × 1 = 120 5! = 5 \times 4 \times 3 \times 2 \times 1=1205!=5×4×3×2×1=120。
Input format
a positive integer nnn。
Output format
a positive integer SSS , represents the calculation result.
Example #1
Sample input #1
3
Sample output #1
9
hint
【data range】
For 100 % 100 \%100% of the data,1 ≤ n ≤ 50 1 \le n \le 501≤n≤50。
【other instructions】
Note: This question is used as an example in "In-depth Basics", but its data range is only n ≤ 20 n \le 20n≤20 , this question cannot be passed using the code in the book.
If you want to pass this question, please continue to study the high-precision knowledge in Chapter 8.
code(python)
n = int(input())
b = 0
for i in range(1, n + 1):
c = i
a = 1
while c != 0:
a = a * c
c = c - 1
b = b + a
print(b)
Code (C)
#include<stdio.h>
int main() {
int n;
int temp;
int i, j;//进行阶乘和求和时的临时变量
while(scanf("%d", &n)!=EOF){
int m[100] = {
0};//存储阶乘结果
int num[1000] = {
0};//存储求和结果
int len = 1, count = 0, C = 0, D = 0;//len为阶乘结果的位数,count为求和结果的位数;C为求阶乘时的进位,D为求和时的进位
m[0] = 1;//0的阶乘为1
for (i = 1; i <= n; i++) {
for (j = 0; j < len; j++) {
temp = m[j];
m[j] = (temp * i + C) % 10;
C = (temp * i + C) / 10;
}
while (C != 0) {
m[len] = C % 10;
C = C / 10;
len++;
}//以上两个循环用来求阶乘结果
for (count = 0; count < len; count++) {
//求完阶乘直接加
temp = num[count];
num[count] = (temp + m[count] + D) % 10;
D = (temp + m[count] + D) / 10;
}
while (D != 0) {
m[count + 1] += D % 10;
D = D / 10;
count++;
}//以上两个循环用来求阶乘之和结果
}
for (i = count - 1; i >= 0; i--)
printf("%d", num[i]);
printf("\n");
}
return 0;
}
6. [NOIP2013 Popularization Group] Counting Issues
Question link: https://www.luogu.com.cn/problem/P1980
Question description
Try to calculate in the interval 1 11 tonnAmong all integers of n , the number xxx(0 ≤ x ≤ 9 0\le x\le90≤x≤9 ) How many times does it appear in total? For example, in1 11 to11 11In 11 , that is, in1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 1,2,3,4,5,6,7,8,9,10,111,2,3,4,5,6,7,8,9,10,In 11 , the number1 11 appeared4 44 times.
Input format
2 2 2 integersn, xn,xn,x , separated by a space.
Output format
1 1 1 integer, representingxxThe number of times x appears.
Example #1
Sample input #1
11 1
Sample output #1
4
hint
For 100 % 100\%100% of the data,1 ≤ n ≤ 1 0 6 1\le n\le 10^61≤n≤106,0 ≤ x ≤ 9 0\le x \le 90≤x≤9。
code(python)
n,x = list(map(int,input().split()))
# 计数的量
c = 0
# 整数序列
for i in range(1,n+1):
# count() 计数
c += str(i).count(str(x))
print(c)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main(){
int n, m;
scanf("%d%d",&n,&m);
int num, sum;
for(int i = 1;i <= n;i++)
{
int pot = i;
while(pot != 0)
{
num = pot%10;
pot = pot/10;
if(num == m) sum++;
}
}
printf("%d",sum);
return 0;
}
7. [NOIP2002 Popularization Group] Series Summation
Question link: https://www.luogu.com.cn/problem/P1035
Question description
Known: S n = 1 + 1 2 + 1 3 + … + 1 n S_n= 1+\dfrac{1}{2}+\dfrac{1}{3}+…+\dfrac{1}{n}Sn=1+21+31+…+n1. Obviously for any integer kkk , whennnWhen n is large enough,S n > k S_n>kSn>k。
Now given an integer kkk , required to calculate a minimumnnn , such thatS n > k S_n>kSn>k。
Input format
a positive integer kkk。
Output format
a positive integer nnn。
Example #1
Sample input #1
1
Sample output #1
2
hint
【data range】
For 100 % 100\%100% of the data,1 ≤ k ≤ 15 1\le k \le 151≤k≤15。
[Source of the question]
NOIP 2002 Popularization Group First Question
code(python)
Sn = 0
k = int(input())
i = 1
while True:
Sn = Sn+1.0/i
if Sn > k:
print(i)
break
i += 1
Code (C)
#include <stdio.h>
int main(){
double Sn=0;
int k;
scanf("%d",&k);
int i=1;
while (1)
{
Sn = Sn+1.0/i;
if(Sn>k)
{
printf("%d",i);
break;
}
i++;
}
8. [NOIP2015 Popularization Group] Gold Coins
Question link: https://www.luogu.com.cn/problem/P2669
Question background
NOIP2015 popularization group T1
Question description
The king paid gold coins as wages to loyal knights. On the first day, the knight received one gold coin; for the next two days (the second and third days), he received two gold coins every day; for the next three days (the fourth, fifth, and sixth days), he received three gold coins every day. ;In the next four days (the seventh, eighth, ninth, and tenth day), four gold coins will be received every day...; This wage payment model will continue like this: when nn consecutiveReceivednn every day for n daysAfter n gold coins, the knight will continue n + 1 n+1n+In 1 day,n + 1 n + 1n+1 gold coin.
Please count before kkIn k days, how many gold coins did the knight get in total?
Input format
a positive integer kkk , represents the number of days to issue gold coins.
Output format
A positive integer, the number of gold coins the knight received.
Example #1
Sample input #1
6
Sample output #1
14
Example #2
Sample input #2
1000
Sample output #2
29820
hint
[Explanation of Sample 1]
The knight receives one gold coin on the first day; two gold coins on the second and third days; three gold coins on the fourth, fifth and sixth days. Therefore, a total of 1 + 2 + 2 + 3 + 3 + 3 = 14 is received 1+2+2+3+3+3=141+2+2+3+3+3=14 gold coins.
For 100 % 100\%100% of the data,1 ≤ k ≤ 1 0 4 1\le k\le 10^41≤k≤104。
code(python)
k = int(input())
n,m = 1,0
while k > 0:
k -= n
if k > 0:
m += n*n
n += 1
else:
m += n*(k+n)
print(m)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int sqr[101];
int app[20001];
int main()
{
int k;//k天
scanf("%d",&k);
int num = 0;
int n = 0;
while(k > num)
{
n++;
num = (n+1)*n/2;
}
long long sum = 0;
if(num != k){
for(int i = 1;i < n;i++)
{
sum = sum + i*i;
}
int m = (n-1)*n/2;
sum = (k - m)*n + sum;
printf("%lld\n",sum);
}
else if(num == k)
{
for(int i = 1;i <= n;i++)
{
sum = sum + i*i;
}
printf("%d\n",sum);
}
return 0;
}
9. [Deep basis 4. Example 11] Sum of a sequence
Question link: https://www.luogu.com.cn/problem/P5722
Question description
Calculate 1 + 2 + 3 + ⋯ + ( n − 1 ) + n 1+2+3+\cdots+(n-1)+n1+2+3+⋯+(n−1)+The value of n , where the positive integernnn is not greater than 100. Since you are not as smart as Gauss, you are not allowed to use the arithmetic sequence summation formula to find the answer directly.
Input format
Enter a positive integer nnn。
Output format
Output a positive integer representing the final summed answer.
Example #1
Sample input #1
100
Sample output #1
5050
hint
Data guarantee, 1 ≤ n ≤ 100 1 \leq n \leq 1001≤n≤100。
code(python)
n = int(input())
sum1 = 0
for i in range(1, n+1):
sum1 = sum1 + i
print(sum1)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int sum = 0;
for(int i = 1;i <= n;i++)
{
sum = sum + i;
}
printf("%d",sum);
return 0;
}
10. [Deep basis 4. Example 13] Prime number pocket
Question link: https://www.luogu.com.cn/problem/P5723
Question description
Little A has a prime number pocket, which can contain various prime numbers. He went from 2 2Starting from 2 , it determines whether each natural number is a prime number in turn. If it is a prime number, it will put the number in the pocket.
The load capacity of a pocket is the sum of all the numbers in the pocket.
However, the load-bearing capacity of the pocket is limited, and the sum of the prime numbers cannot exceed LL.L. _ givesLLL , how many prime numbers can you fit in your pocket? Output these prime numbers from small to large, and then output the maximum number of prime numbers that can be accommodated. Use newlines to separate the numbers.
Input format
One positive integer LL per lineL。
Output format
Output these prime numbers from small to large, and then output the maximum number of prime numbers that can be accommodated, with a blank line between all numbers.
Example #1
Sample input #1
100
Sample output #1
2
3
5
7
11
13
17
19
23
9
Example #2
Sample input #2
5
Sample output #2
2
3
2
Example #3
Sample input #3
11
Sample output #3
2
3
5
3
hint
Data guarantee, 1 ≤ L ≤ 10 5 1 \le L \le {10}^51≤L≤105。
code(python)
lena = 0
cnt = 0
prime = [1 for i in range(100010)]
lena = int(input())
prime[1] = 0
prime[0] = 0
for i in range(2, 100010):
if prime[i] == 1:
j = i * 2
while j < 100010:
prime[j] = 0
j += i
for i in range(2, 100010):
if prime[i] == 1:
if lena >= i:
print(i)
lena -= i
cnt += 1
else:
break
print(cnt)
Code (C)
#include <stdio.h>
#include <stdlib.h>
int n,x;
long long sum=0;
int pd(int y) {
int i;
for(i=2; i*i<=y; ++i) {
if(y%i==0) return 0;
}
return 1;
}
int main() {
scanf("%d",&n);
if(n<2) {
printf("0\n");
return 0;
} else if(n==2) {
printf("2\n1\n");
return 0;
}
int i;
for(i=2; i<=n; ++i) {
if(i%2==0&&i!=2) continue;
if(sum+i>n) {
printf("%d\n",x);
return 0;
}
if(pd(i)) {
printf("%d\n",i);
sum+=i;
x++;
}
}
return 0;
}
11. [USACO1.5] Prime Palindromes
Question link: https://www.luogu.com.cn/problem/P1217
Question description
Because 151 151151 is both a prime number and a palindrome (looking the same from left to right and right to left), so151 151151 is a palindromic prime number.
Write a program to find the range [ a , b ] ( 5 ≤ a < b ≤ 100 , 000 , 000 ) [a,b] (5 \le a < b \le 100,000,000)[a,b](5≤a<b≤100,000,000 ) (one hundred million) all palindromic prime numbers.
Input format
The first line enters two positive integers aaa andbbb。
Output format
Output a list of palindromic primes, one per line.
Example #1
Sample input #1
5 500
Sample output #1
5
7
11
101
131
151
181
191
313
353
373
383
hint
Hint 1: Generate the palindromes and see if they are prime.
Tip 1: Find all the palindromes and determine whether they are prime numbers (prime numbers).
Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below.
Tip 2: To generate the correct number of palindromes, you may need several loops like the one below.
The title translation comes from NOCOW.
USACO Training Section 1.5
produces a length of 5 5The number of palindromes of 5 :
for (d1 = 1; d1 <= 9; d1+=2) {
// 只有奇数才会是素数
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
}
}
}
code(python)
# 输入范围
begin, end = map(int, input().split())
# 定义一个筛选素数的函数,时间复杂度为根号n/3
def prime_number(number):
if number <= 3:
return True
elif number % 2 == 0 or number % 3 == 0:
return False
else:
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False
i += 6
return True
# 定义生成回文数的函数
from itertools import product
def palind(n):
digit_palind = []
half = product(*([range(1, 10, 2)] + [range(10)] * ((n - 1) // 2)))
for i in half:
digit_palind.append(n * '%s' % tuple(list(i) + list(i[-(n % 2) - 1::-1])))
return digit_palind
# 判断终点的数位
numerical_digit = 1
while 10 ** numerical_digit < end:
numerical_digit += 1
for i in range(1, numerical_digit + 1):
for str_num in palind(i):
num = int(str_num)
if num >= begin and num <= end and prime_number(num):
print(num)
Code (C-88 points)
#include <stdio.h>
#include <stdlib.h>
long long a,b;
//判断是否是质数
int pd(long long y) {
long long i;
for(i=2; i*i<=y; ++i) {
if(y%i==0) return 0;
}
return 1;
}
//判断是否是回文数
int hui(long long n)
{
long long num = n;
long long m = 0, a;
while(1){
a = n%10;
m = m*10 + a;
n = n/10;
if( n == 0){
break;
}
}
if(m == num)
return 1;
else
return 0;
}
int main()
{
scanf("%d %d",&a,&b);
long long i = a;
if(a%2 ==0)
a ++;
for(i = a;i <= b;i+=2)
{
if(i%3 != 0)
{
int y = hui(i);
if(y == 1)
{
int x = pd(i);
if(x ==1)
printf("%lld\n",i);
}
}
}
return 0;
}
Code (C-100)
#include<stdio.h>
#include<math.h>
int prime(int n) {
//判定素数
if(n==1)//特判1
return 0;
if(n%2==0)//2的倍数就回家吧
return 0;
else {
//不然就暴力枚举
int i;
for(i=2; i<=sqrt(n); i++) {
if(n%i==0)
return 0;
}
return 1;
}
}
int hw(int n) {
//判定回文,不懂请参考数字反转
int sum=0;
int k=n;
while(n!=0) {
sum=sum*10+n%10;
n/=10;
}
if(sum==k)//判断是否回文
return 1;
else
return 0;
}
int main() {
int i,n,sum=0,m;
scanf("%d %d",&n,&m); //读入两个数
for(i=n; i<=m; i++) {
if(i==9989900) //如果到了这个数,就break
break;
if(hw(i)&&prime(i))//否则判断是否回文和素数
printf("%d\n",i);//输出每个回文质数
}
return 0;//结束程序
}
12. Xiaoyu is swimming
Question link : https://www.luogu.com.cn/problem/P1423
Question description
Xiaoyu was swimming happily, but she soon found out sadly that she was not strong enough and swimming was very tiring. It is known that Xiaoyu can swim 2 2 in the first step2 meters, but as she became more and more tired and her strength became less and less, she could only swim 98% 98\%of the distance of the previous step with each subsequent step.98% . Now Xiaoyu wants to know, if you want to swim to a distance ofssHow many steps does she need to swim in a place of s meters? Please program to solve this problem.
Input format
Enter a real number sss (unit: meter), indicates the target distance to be swam.
Output format
Output an integer, indicating how many steps Xiaoyu needs to swim in total.
Example #1
Sample input #1
4.3
Sample output #1
3
hint
Data guaranteed, 0 ≤ s < 100 0 \leq s < 1000≤s<100 , andsss has at most one decimal place.
code(python)
s = float(input())
num = 0
step = 2
while s > 0:
s = s - step
step = step *0.98
num += 1
print(num)
Code (C)
#include<stdio.h>
int main()
{
float a,b=2,k=2;/*距离可能非整*/
int i=1;/*由于数据弱就int即可*/
scanf("%f",&a);/*输入目标*/
for(i=1;i<=99999999;i++)
{
if(k>a) break;/*判断是否游到目标*/
else
{
b=b*0.98;/*每步能游的距离*/
k=b+k;/*每步结束后的已游总距离*/
}}
printf("%d",i);/*输出步数*/
return 0;
}
13. [NOIP2011 Popularization Group] Digital Reversal
Question link: https://www.luogu.com.cn/problem/P1307
Question description
Given an integer NNN , please reverse the digits in each digit of this number to get a new number. The new number should also satisfy the common form of integers, that is, the highest digit of the new number after inversion should not be zero unless the given original number is zero (see Example 2).
Input format
an integer NNN。
Output format
An integer representing the new number after inversion.
Example #1
Sample input #1
123
Sample output #1
321
Example #2
Sample input #2
-380
Sample output #2
-83
hint
【data range】
$-1,000,000,000\leq N\leq $1,000,000,000。
noip2011 popularization group first question
code(python)
num = input()
if num[0] == "-":
print("-", end="")
print(int(num[:0:-1]))
else:
print(int(num[::-1]))
Code (C)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
while(scanf("%d",&n)!=EOF){
int m = 0, a;
while(1){
a = n%10;
m = m*10 + a;
n = n/10;
if( n == 0){
break;
}
}
printf("%d\n",m);
}
return 0;
}
14. Money is counted when the moon sets and the crow crows (Fibonacci Sequence)
Question link: https://www.luogu.com.cn/problem/P1720
Question background
(There is no hidden song in this question... No need to guess...)
The final chapter of "The Story of Love and Sorrow Part 1·Heartache".
After eating the pizza, Yueluowudi knew that it was beyond her budget. In order not to show my shame in front of the God of Love and Sorrow, I had no choice but to bite the bullet and count the money...
Question description
After calculating the money, Yueluowu cried and thought: "You fucking tricked me, (hereinafter read in Hokkien) I will depend on my mother and father, (hereinafter read in English) it will be Yiteyou!" So when love and sorrow When the great god asked how much it was, Yueluowudi said a bunch of gibberish. The God of Love and Sorrow said: "Forget it, forget it, I only ask the nnHow much does n type of dishes cost? "The moon fell and the crow cried:
F n = ( 1 + 5 2 ) n − ( 1 − 5 2 ) n 5 F_n=\dfrac{\left(\frac{1+\sqrt{5}}{2}\right)^n-\left(\frac{1-\sqrt{5}}{2}\right)^n}{\sqrt{5}} Fn=5(21+5)n−(21−5)n
Since the Great God of Love and Sorrow has learned programming, I used 1 1F n F_n was found in 1 minute.Fnthe result of. Yueluowu Tiao was shocked by this. Can you learn love and sorrow? The master asks for F n F_nFnIs it worth it?
Input format
One natural number nn per linen。
Output format
only 1 11 row of a real numberF n F_nFn, to two decimal places.
Example #1
Sample input #1
6
Sample output #1
8.00
hint
For all data: 0 ≤ n ≤ 48 0 \leq n\leq 480≤n≤48。
code(python)
import math
def jia( n ):
num = float((1 + math.sqrt(5)) / 2)
sum = 1
while n > 0:
sum = float(sum * num)
n -= 1
return sum
def jian( n ):
num = float((1 - math.sqrt(5)) / 2)
sum = 1
while n > 0:
sum = float(sum * num)
n -= 1
return sum
if __name__ == '__main__':
n = int(input())
a = jia(n)
b = jian(n)
num = float((a-b)/math.sqrt(5))
# print(round(num,3))
print("{:.2f}".format(num))
Code (C)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//函数
double HANSHU(int n){
double num = (pow(((1 + sqrt(5.0))/2.0),n) - (pow(((1 - sqrt(5.0))/2.0),n)))/sqrt(5.0);
return num;
}
int main(int argc, char *argv[]) {
int n;
while(scanf("%d",&n)!=EOF){
double num;
num = HANSHU(n);
printf("%.2lf\n",num);
}
return 0;
}
15. [Deep Foundation 4. Exercise 5] Find the range/maximum span value
Question link: https://www.luogu.com.cn/problem/P5724
Question description
gives nnn andnnn integersai a_iai, please this nnWhat is the range of n integers? Range means the difference between the maximum value minus the minimum value in a set of numbers.
Input format
The first line enters a positive integer nnn , represents the number of integers.
Enter nn in the second linen integera 1 , a 2 … an a_1,a_2 \dots a_na1,a2…an, separated by spaces.
Output format
Output an integer representing this nnRange of n integers.
Example #1
Sample input #1
6
4 1 5 1 4 1
Sample output #1
4
hint
Data guarantee, 1 ≤ n ≤ 100 1 \leq n\leq 1001≤n≤100,0 ≤ ai ≤ 1000 0\le a_i \le 10000≤ai≤1000。
code(python)
n = int(input())
num = list(input().split(" "))
max1 = int(num[0])
min1 = int(num[0])
for i in range(0,n-1):
if int(num[i]) > max1:
max1 = int(num[i])
if int(num[i]) < min1:
min1 = int(num[i])
print(max1 - min1)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int min = 1000, max = 1;
for(int i = 1;i <= n;i++)
{
int num;
scanf("%d",&num);
if(num < min)
{
min = num;
}
if(num > max)
{
max = num;
}
}
printf("%d",max - min);
return 0;
}
16. The longest consecutive number
Question link: https://www.luogu.com.cn/problem/P1420
Question description
The input length is nnA sequence of positive integers for n , requiring the length of the longest consecutive number in the output sequence.
Serial numbers refer to consecutive natural numbers in a sequence from small to large.
Input format
The first line, an integer nnn。
Second line, nnn integersai a_iai, separated by spaces.
Output format
A number, the number of the longest consecutive numbers.
Example #1
Sample input #1
10
1 5 6 2 3 4 5 6 8 9
Sample output #1
5
hint
Data size and conventions
For 100 % 100\%100% data, guaranteed1 ≤ n ≤ 1 0 4 1 \leq n \leq 10^41≤n≤104, 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109。
code(python)
maxn = 0
n = int(input())
a = list(map(int, input().split()))
lena = len(a)
for i in range(n):
s = 0
for j in range(i + 1, n):
if a[j] == a[j - 1] + 1:
s += 1
else:
break
maxn = max(maxn, s)
print(maxn + 1)
Code (C)
#include <stdio.h>
#include <stdlib.h>
#define M 10000
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, i, j, count = 0,sum = 0;
scanf("%d",&n);
long long int NUM[M];
for(i = 0;i < n;i++){
scanf("%lld",&NUM[i]);
}
for(j = 0;j <= n;j++){
if(NUM[j] == NUM[j+1] - 1){
count++;
if(count > sum)
sum = count;
}
else count = 0;
}
printf("%d\n",sum+1);
return 0;
}
17. [NOIP2012 Popularization Group] Prime Factorization
Question link: https://www.luogu.com.cn/problem/P1075
Question description
Known positive integer nnn is the product of two different prime numbers, try to find the larger prime number of the two.
Input format
Enter a positive integer nnn。
Output format
Output a positive integer ppp , which is the larger prime number.
Example #1
Sample input #1
21
Sample output #1
7
hint
1 ≤ n ≤ 2 × 1 0 9 1 \le n\le 2\times 10^91≤n≤2×109
NOIP 2012 Popularization Group First Question
code(python)
n = int(input())
m = 2
while n % m != 0:
m += 1
p = int(n / m)
if p <= m:
p = m
print(p)
Code (C)
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int m = 2;
while(n%m != 0)
{
m++;
}
int p = n/m;
if(p <= m) p = m;
printf("%d",p);
return 0;
}
18. [Deep Basis 4. Exercise 8] Find Triangles
Question link: https://www.luogu.com.cn/problem/P5725
Question description
Imitate the example, print out squares in different directions, and then print out a triangular matrix. There is a blank line in the middle.
Input format
The size of the input matrix, not exceeding 9 99。
Output format
Output rectangles and squares
Example #1
Sample input #1
4
Sample output #1
01020304
05060708
09101112
13141516
01
0203
040506
07080910
code(python)
cnt = 0
n = int(input())
i = 1
while i <= n * n:
if i % n == 1 and i != 1:
print()
# print("\n")
if i < 10:
print("0", end="")
print(i, end="")
i += 1
print()
print()
# print("\n\n")
i = 2 * n
while i > 0:
i -= 2
for j in range(i):
print(" ", end="")
j = 0
while j < (2 * n - i) // 2:
cnt += 1
if cnt < 10:
print("0", end="")
print("%d" % cnt, end="")
j += 1
print()
# print("\n")
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int x = 1;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(x < 10)
{
printf("0%d",x);
x++;
}
else
{
printf("%d",x);
x++;
}
}
printf("\n");
}
printf("\n");
int m = 1;
for(int i = n-1;i >= 0;i--)
{
for(int j = 0;j < i;j++)
{
printf(" ");
}
for(int t = i;t <= n-1;t++)
{
if(m < 10)
{
printf("0%d",m);
m++;
}
else
{
printf("%d",m);
m++;
}
}
printf("\n");
}
return 0;
}
19. [Deep Basics 4. Practice 9] Scoring
Question description
Now there are n ( n ≤ 1000 ) n(n \le 1000)n(n≤1000 judges give scores to the contestants, with scores ranging from 0 00 to10 1010 . You need to remove the highest score and the lowest score (if there are multiple highest or lowest scores, you only need to remove one), and the average of the remaining ratings is the player's score. Now enter the number of judges and their scores. Please output the final score of the contestants, accurate to2 22nd place decimal.
Input format
The first line enters a positive integer nnn , indicating that there isnnn judges.
Enter nn in the second linen positive integers,iii positive integer represents theiithThe score given by i judges.
Output format
Output one line of two decimals, indicating the player's final score.
Example #1
Sample input #1
5
9 5 6 8 9
Sample output #1
7.67
hint
Data guarantee, 3 ≤ n ≤ 1000 3 \leq n \leq 10003≤n≤1000 , each judge’s score is0 00 to10 1010 (including0 00 and10 1010 ) an integer between.
code(python)
n = int(input())
grades = list(map(int, input().split()))
new = sorted(grades)
sum = 0
for i in range (1,n-1):
sum = sum + new[i]
print(float(round(sum/(n-2),2)))
20. [COCI2017-2018#6] Davor
Question link: https://www.luogu.com.cn/problem/P4956
Title translation
After conquering Antarctica, Davor embarked on a new challenge. The next step is an Arctic Circle expedition in Siberia, Greenland, Norway. He will be in 2018 20181212 2018December 3131Starting on the 31st, we need to raise a total of nn beforen dollars. He plans to raise xxevery Monday$ x , raisex+k x+kx+k yuan,..., raisedx+6k x+6kx+6k yuan and raised52 5252 weeks. wherex , kx,kx,k is a positive integer and satisfies1 ≤ x ≤ 100 1 \le x \le 1001≤x≤100。
Now please help me calculate x, kx,kx,When k is, how muchnnn yuan.
If there are multiple answers, output xxx is as large as possible,kkk is as small as possible. Attentionkk_k must be greater than0 00。
Question description
After successfully conquering the South Pole, Davor is preparing for new challenges. Next up is the Arctic expedition to Siberia, Greenland and Norway. He begins his travels on 31 December 2018, and needs to collect N kunas (Croatian currency) by then. In order to do this, he has decided to put away X (X ≤ 100) kunas every Monday to his travel fund, X + K kunas every Tuesday, X + 2* K every Wednesday, and so on until Sunday, when he will put away X + 6* K kunas. This way, he will collect money for 52 weeks, starting with 1 January 2018 (Monday) until 30 December 2018 (Sunday).
If we know the amount of money N, output the values X and K so that it is possible to collect the exact money amount in the given timespan. The solution will always exist, and if there are multiple, output the one with the greatest X and smallest K .
Input format
The first line of input contains the integer N (1456 ≤ N ≤ 145600), the number from the task.
Output format
The first line of output must contain the value of X (0 < X ≤ 100 ), and the second the value of
K (K > 0 ).
Example #1
Sample input #1
1456
Sample output #1
1
1
Example #2
Sample input #2
6188
Sample output #2
14
1
Example #3
Sample input #3
40404
Sample output #3
99
4
code(python)
num = int(input())
n = 1
k = 1
price = int(num / 364)
n = price - 3 * k
k += 1
while n > 100:
n = price - 3 * k
k += 1
print(n)
print(k - 1)
Code (C)
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int n, k = 1;
long long num;
scanf("%lld",&num);
int price = num / 364;
do
{
n = price - 3*k;
k++;
}
while(n > 100);
printf("%d\n",n);
printf("%d\n",k - 1);
return 0;
}
21. [NOIP2004 Improvement Group] Jinjin’s Savings Plan
Question link: https://www.luogu.com.cn/problem/P1089
Question description
Jinjin has always managed her own pocket money. At the beginning of every month, my mother gives Jinjin 300 300With 300 yuan, Jinjin will budget the expenses for this month, and he can always make the actual expenses consistent with the budget.
In order to let Jinjin learn how to save, her mother proposed that Jinjin can deposit the entire hundred money with her at any time, and at the end of the year she will add 20% 20\%20% is returned to Jinjin. Therefore, Jinjin formulated a savings plan: at the beginning of each month, after receiving the pocket money from her mother, if she expected to have more than 100 100by the end of the month,100 yuan or exactly100 100For 100 yuan, she will deposit the entire hundred with her mother and keep the remaining money in her own hands.
For example 11 11At the beginning of November, Jinjin still had83 8383 yuan, mother gave Jinjin300 300300 yuan. Jinjin is expected to be11 11The expenses in November are180 180180 yuan, then she will save 200 200with her mother200 yuan, keep183 183183 yuan. It’s11 11At the end of November , Jinjin will have3 33 yuan.
Jinjin discovered that the main risk of this savings plan was that the money deposited with her mother could not be withdrawn before the end of the year. It is possible that at the beginning of a certain month, the money in Jinjin's hands plus the money given by her mother this month is not enough to meet the original budget for this month. If this happens, Jinjin will have to save money and compress the budget this month.
Now please base on 2004 20042004 11January toDecember 12Use the monthly allowance budget in December to determine whether this situation will occur . If not, calculate to2004 2004At the end of 2004 , my mother added 20%to the money Jinjin usually saved.After 20% is returned to Jinjin, how much money will Jinjin have in his hands.
Input format
12 12 12 rows of data, each row contains a value less than350 350350 is a non-negative integer, representing1 1January toDecember 12December Jinjin’s budget.
Output format
an integer. If there is not enough money for a certain month during the implementation of the savings plan, output − X -X−X, X X X indicates the first month in which this occurred; otherwise output to2004 2004How much money will Jinjin have in his hands at the end of 2004 ?
Note that Luogu does not need to perform file input and output, but standard input and output.
Example #1
Sample input #1
290
230
280
200
300
170
340
50
90
80
200
60
Sample output #1
-7
Example #2
Sample input #2
290
230
280
200
300
170
330
50
90
80
200
60
Sample output #2
1580
code(python)
x = 0
y = 0
save = 0
flag = 1
month = 0
for i in range(1, 13):
x += 300
y = int(input())
x -= y
if (x < 0):
flag = 0
month = i
break
save += int(x / 100)
x = x % 100
if (flag == 1):
print(120 * save + x)
else:
print(-month)
Code (C)
#include <stdio.h>
int main()
{
int n;
int sum = 0; //初始化,用于记录每个月剩余有多少钱
int sum0 = 0; //如果中间没有出现缺钱的情况,那么就用这个sum0来存储每个月在妈妈手里有多少钞票
for (int i = 1; i <= 12; i++) //一年十二个月,不多做解释了
{
sum += 300; //妈妈给钱
scanf("%d", &n);
sum = sum - n;
if (sum < 0) //如果消费大于开支则为负数,直接输出ok
{
printf("-%d\n", i); //特别注意负号
return 0;
}
else
sum0 += sum / 100 * 100; //如果不是就往妈妈手里存钱
sum = sum - sum / 100 * 100; //每个孩子手里剩余多少钱
}
printf("%d\n", sum0 * 12 / 10 + sum); //如果上述未有输出,那么就可以打印输出多少钱了
return 0;
}