catalogue
1. Sum of consecutive numbers
thinking
This question is very watery. It may seem a little mysterious at first glance, but it's very simple. For example, 6 = - 5 + - 4 + - 3 + - 2 + - 1 + 0 + 1 + 2 + 3 + 4 + 5 + 6
Each number can be expressed in this way.
Code display
C language:
#include<stdio.h> int main() { int t,l,r; int n; scanf("%d",&t); while(t!=0) { scanf("%d",&n); l=n; r=-n+1; printf("%d %d\n",r,l); t--; } return 0; }
python:
t=int(input()) i=0 for i in range(t): n=int(input()) l=-n+1 r=n print("%d %d"%(l,r))
2. Award a medal
/*After a long war, the kingdom of bonari finally ushered in the dawn of victory.
So the king decided to honor the generals who fought in the war on victory day.
N generals have to be decorated. Each of them has a merit p[i].
The king prepared different kinds of medals, which represent 1, 2, 4, 8, 16 (i.e. the power of 2).
The king will award them medals equal to the merit of each general,
And each general will be awarded only one medal of the same kind.
Now please help the king figure out how many medals he needs to prepare for each general*/
Input | Output |
---|---|
3 15 1 22 | 4 1 3 |
Train of thought
Related to 2, medals are power of 2. You can take the remainder of 2 until 1, and record the number of remainder
Code display
#include<stdio.h> int main() { int n,m; scanf("%d",&n); while(n--) { int count=0; scanf("%d",&m); while(m!=0) { if(m%2==1) count+=1; m/=2; } printf("%d\n",count); } return 0; }
3. Perfect cube
/*Perfect cube
Input format
A positive integer N(N ≤ 100).
Output format
Each row outputs a perfect cube. The output format is:
Cube = a, Triple = (b,c,d)
The positions of a, B, C and D are respectively substituted with the actual Quad values.
Please output from small to large according to the value of a.
When the value of a in two perfect cubic equations is the same, the output with small value of b is preferred;
If it is still the same, the priority output with small c value; If it is the same again, the one with small d value will be output first*/
Train of thought
This problem is not difficult, but we should pay attention to that the minimum number of perfect cubes is 6, 3, 4 and 5, which can reduce the number of cycles.
Code display
#include<stdio.h> int lifang(int a) { return a*a*a; } int main() { int a=6,b=3,c=4,d=5,n;//Minimum perfect cubic number scanf("%d",&n); for(int i=6;i<=n*n*n*n;i++) { if(lifang(a)==lifang(b)+lifang(c)+lifang(d)&&b<=c&&c<=d) printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d); d++; if(b>n){ b=2; a++; } if(c>n){ c=2; b++; } if(d>n){ d=2; c++; } if(a>n) return 0; } return 0; }
4. Buying chicken
/*The problem of buying a hundred chickens for a hundred dollars: a rooster for five cents, a hen for three cents and a chick for three cents,
Buy 100 chickens with 100 Wen. How many cocks, hens and chicks do you buy?
The problem required to be solved in this program is: given a positive integer n,
Buy n chickens with n Wen money and ask how many cocks, hens and chicks each buy*/
thinking
Two cycles of violence are solved. Yes, there are two cycles. You know the number of cocks and hens and the number of chicks
Code display
#include<stdio.h> int main() { int n,c=0,k,sum; scanf("%d",&n); for(int i=0;i<=n/5;i++) for(int j=0;j<=((n-5*i)/3);j++) { k=(n-5*i-3*j)*3; sum=k+i+j; if(sum==n) { printf("%d %d %d\n",i,j,k); c=1; } } if(c==0) printf("No Answer."); return 0; }
5. Cow story
/*There is a cow. It gives birth to a heifer every year.
Each heifer starts from the fourth year and gives birth to a heifer in the middle of each year.
Please program it in the nth year (excluding the heifer born in the nth year),
How many cows are there?
Input format
The input includes an integer n (0 < n < 55)
Output format
Output the number of cows in year n.
Sample 1
Input
2
Output
2
Sample 2
Input
5
Output
6
*/
thinking
The first is recursion. If the cow's "age" < = 3, return yourself. Otherwise, return yourself and a new cow
If the number of years is n, n < = 3, return n, otherwise return niu(n-1)+niu(n-3)
Code display
#include<stdio.h> int niu(int n); int niu(int n) { if(n<=3) return n; else return niu(n-1)+niu(n-3); } int main() { int n; scanf("%d",&n); printf("%d",niu(n)); return 0; }
There's another way to do it with a loop
#include<stdio.h> int main() { int a[55]={1,2,3,4},n; scanf("%d",&n); if(n<=4) printf("%d",n); else{ for(int i=4;i<n;i++) { a[i]=a[i-1]+a[i-3]; } printf("%d",a[n-1]); } return 0; }
6. The heating is broken
/*The heating of garlic gentleman's house often goes wrong. Whenever the heating breaks down,
Garlic gentleman will continue to catch a cold for m days (from the bad day, the overlapping time of two colds will not be accumulated).
Garlic king went to seek the help of the prophet, who told him the time when the radiator broke down next n times.
According to this time, garlic king can know the total number of days he will get a cold in the future.
Input format
The two integers n and m in the first line represent the number of times the radiator breaks down and the duration of each cold.
The second line contains n integers ai, indicating the date when the radiator broke down.
Data range: 1 < = n ≤ 10000, 1 < = m, ai ≤ 10 ^ 9, to ensure that ai is strictly increasing.
Output format
An integer that represents the total number of days that the garlic gentleman has a cold.
Sample Input
4 3
1 2 4 8
Sample Output
9*/
thinking
Circulation, note that the cold days are not superimposed
Code display
#include<stdio.h> int main() { int n,m,a[10000],sum=0; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&a[i]);; for(int i=0;i<n-1;i++) { if(a[i+1]<=a[i]+m) { sum+=a[i+1]-a[i]; }else{ sum+=m; } } sum+=m; printf("%d",sum); return 0; }