当前位置:网站首页>Introduction to Luogu 3 [circular structure] problem list solution
Introduction to Luogu 3 [circular structure] problem list solution
2022-07-02 04:37:00 【Yang Yi】
Catalog
I. Judge the palindrome number
When the moon falls and the crow dies, the money is
Find the minimum
- Pay attention to finding the minimum value , The minimum value should be initialized to a larger number ;
- Find the maximum , The maximum value should be initialized to a smaller number .
int n,a[100],min=1000; // Initialize to a larger number .
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
{
if (a[i] < min)
min = a[i];
}
cout << min;
Classification average
int n, k,sum1=0,sum2=0;
double ave1=0, ave2=0;
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
if (i%k == 0)
{
ave1 += i;
sum1++;
}
if (i%k != 0)
{
ave2 += i;
sum2++;
}
}
ave1 /= sum1;
ave2 /= sum2;
printf("%.1lf %.1lf", ave1, ave2);
A foot's weight
int a,sum=1;
cin >> a;
while (1)
{
if (a == 1)
break;
a /= 2;
sum++;
}
cout << sum;
Digital right triangle
- Leading 0 It can be used %02d
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main() {
int n, sum = 0, k;
cin >> n;
k = n;// Set up k Or the following i<=(n The expression of ) there n It changes with it .
// Pay attention to this intermediate setting , There will be many questions related to this original value change in the future , But later operations also use the original value , At this time , Be sure to start with a variable equal to the original value .
for (int i = 1; i <= (n+1)*n/2; i++)
{
printf("%02d", i);
sum++;
if (sum == k)
{
sum = 0;// Be careful sum=0 The location of
cout << endl;
k--;
}
}
return 0;
}
Sum of factorials
High precision
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<stdio.h>
using namespace std;
int a[101] = { 0 }, b[101] = { 0 };
void mul(int x)// ride
{
int y = 0;
for (int i = 100; i >= 0; i--)
{
a[i] = a[i] * x + y; //a[101] except a[100] Others are initialized to 0, After that, each carry must only be based on the previous digit, that is y carry . In addition to the first to use x.
y = a[i] / 10;//y It's carry
a[i] = a[i] % 10;
}
}
void add( )// Add
{
int y = 0;
for (int i = 100; i >= 0; i--)
{
b[i] = b[i] +a[i] + y;
y = b[i] / 10; //y It's carry
b[i] = b[i] % 10;
}
}
int main()
{
int n,i,w;
cin >> n;
a[100] = b[100] = 1;
for (i = 2; i <= n; i++)
{
mul(i); // Run from small to large , Add 2! The next operation starts from 2!*3 namely 3! To calculate .
add();
}
int m = 0;
while (b[m] == 0) m++;// Remove the end 0
for (int i = m; i <= 100; i++)
cout << b[i];
return 0;
}
Counting problem
int sum = 0;
int n, x,a;
cin >> n >> x;
for (int i = 1; i <=n; i++)
{
a = i;
while (a)// If you don't set a=i, Back i Arithmetic , change i The value of the ,i The next time for Cycle from 0 Start .
{
if (a % 10 == x)// Single digit ,
sum++;
a /= 10;// After using single digits , Change ten digits into single digits , Then judge why the number of single digits .
}// Determine what number each bit of this number is We must know this while loop .
}
cout << sum;
Sum of series
int k;
double n = 0;
double i = 1;
cin >> k;
while(i++)
{
n += 1 / i;
if (n > k)
{
break;
}
}
cout << ceil(n);
Gold coin
#include<iostream>
using namespace std;
int main()
{
int i = 1,m=1,k,sum=0;
cin >> k;
while (1)
{
while (m--) // Every time the m God ,m by 0 when , For the next group of days, press m=i, namely m+1 To calculate .
{
sum += i; //m individual i Add up namely n God *n Gold coin
k--;
if (k == 0)
break;
}
i++; // The number of gold coins , Receive... Every day i Gold coins
m = i;// Days , continuity m God
if (k == 0)
{
break;
}
}
cout << sum;
return 0;
}
Prime pocket
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main()
{
int L,sum=1,num=2;
cin >> L;
if (L == 1)
cout << "0";
else
{
for (int i = 2; num <= L; i++)
{
if (i == 2)
cout << i << endl;
bool prime = true;// Note the location of the initialization
for (int j = 2; j <= i / 2 + 1; j++)
{
if (i % j == 0)// Remainder
{
prime = false;
break;
}
}
if (prime == true)
{
num += i;
if (num > L)
break;
cout << i << endl;
sum++;
}
}
cout << sum;
}
return 0;
}
Prime Palindromes
I. Judge the palindrome number
bool isPalindrome(int x)
{
if (x < 0 || (x % 10 == 0 && x != 0)) // Not 0 outside 10 The multiple of is definitely not palindromes .
{
return false;
}
int re = 0;
while (x > re)
{
re = re * 10 + x % 10;
x /= 10;
}
return x == re || x == re / 10;
}
Figure source force buckle
II. Judge the prime number
bool isprime(int y)
{
if (y != 2 && y % 2 == 0) // except 2 Even numbers other than are definitely not prime numbers
return false;
int n = y,sum=0;
if (isPalindrome(y) && y != 11) // except 11 Palindromes other than even digits are definitely not prime numbers , There are 11 It's a factor
{
while (y)
{
y /= 10;
sum++;
}
if (sum % 2 == 0)
return false;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
bool isPalindrome(int x)
{
if (x < 0 || (x % 10 == 0 && x != 0))
{
return false;
}
int re = 0;
while (x > re)
{
re = re * 10 + x % 10;
x /= 10;
}
return x == re || x == re / 10;
}
bool isprime(int y)
{
if (y != 2 && y % 2 == 0)
return false;
int n = y,sum=0;
if (isPalindrome(y) && y != 11)
{
while (y)
{
y /= 10;
sum++;
}
if (sum % 2 == 0)
return false;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}
int main()
{
int a, b;
cin >> a >> b;
if (b > 10000000) b = 10000000; // except 11 Palindromes other than even digits are definitely not prime numbers . Run to the 10000000 Can .
for (int i = a; i <= b; i++)
{
if (isPalindrome(i))
{
if (isprime(i))
{
cout << i << endl;
}
}
}
return 0;
}
Xiaoyu is swimming
#include<iostream>
using namespace std;
int main()
{
double meter=2,x;
int step=0;
cin >> x;
while (x > 0)
{
x -= meter;
meter*=0.98;
step++;
}
cout << step;
return 0;
}
Number reversal
Pay attention to cut off all at the end 0
#include<iostream>
using namespace std;
int main()
{
int N,a,b,count0=1;
cin >> N;
if (N == 0)
cout << 0;
else {
if (N < 0)
{
cout << "-";
N = -N;
}
a = N;
b = N;
while (b)// Judge how many there are at the end of this number 0; Cut them all off.
{
N = b % 10;
b /= 10;
if (N == 0)
{
count0 *= 10;
}
else
break;
}
a /= count0; // Cut off the end 0
b = a;
while (b)
{
a = b;
a %= 10;
cout << a;//2
b /= 10;
}
}
return 0;
}
When the moon falls and the crow dies, the money is
- Don't be confused by formulas , Don't use formulas directly !!!
- To find a pattern
- The title has already said Fibonacci sequence . The characteristic of this series is f(n-1)+f(n-2)=f(n)
#include <stdio.h> // The header file int main() { double f[50]; int n,i; f[0]=0; f[1]=1; f[2]=1; // Recursive boundary conditions scanf("%d",&n); for (i=3;i<=n;i++) f[i]=f[i-1]+f[i-2]; // Start using Fibonacci series printf("%0.2lf",f[n]); // Output , Keep two decimal places return 0; }
Longest hyphen
#include<iostream>
using namespace std;
int main()
{
int n,serial=1,a,b,max=1;
cin >> n;
cin >> a;// First number
n--;
while (n--)
{
cin >> b;// Other numbers , The next number of the previous number .
if (a == b - 1)
{
serial++;
if (serial > max)
max = serial;
}
if (a != b - 1)
{
serial = 1;
}
a = b;// The last number
}
cout << max;
return 0;
}
Prime factor decomposition
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,p,a;
cin >> n;
// The product of two prime numbers , The factor of this product can only be the sum of these two prime numbers and 1 And itself .
// There are many iterations from big to small , Traverse from small to large, find smaller divisors, and traverse fewer , Reuse n/
for (int i =2; i<=sqrt(n); i++)// Judge the prime number
{
if (n%i == 0)// First, let's see if the product can be divisible
{
p = i;
break;
}
}
cout << n/p;
return 0;
}
Find triangle
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int scale,row=0,a,b,newrow=0;
cin >> scale;
a = scale * scale;
for (int i = 1; i <=a ; i++)
{
printf("%02d", i);
row++;
if (row % scale == 0)
cout << endl;
}
cout << endl;
cout << setw(2 * (scale - 1)) << ""; // The first row of the second square 01 The space in front of it
b = (scale + 1)*scale / 2;
row = 1;
for (int j = 1; j <= b; j++)// A number means how many numbers there are in a row . Another is that each line starts from 0 Start , Output a number ++
{
printf("%02d", j);
newrow++;
if (row == newrow && row<scale)//row<scale Otherwise, one more line will be output
{
cout << endl;
cout << setw(2 * (scale - row-1)) << "";
newrow=0;
row++;//row Is a line of several numbers .
}
}
return 0;
}
Davor
#include<iostream>
using namespace std;
int main()
{
int n, x, k;
cin >> n;
//1092k+364x=n;
for (int i = 1; i < n; i++)
{//k As small as possible , from 1 Start arranging , Line up first to the smaller k
if ((n - 1092 * i) % 364 == 0)
{
x = (n - 1092 * i) / 364;
if (x > 100)
{
continue;
}
k = i;
break;
}
}
cout << x << endl;
cout << k << endl;
return 0;
}
Jinjin's savings plan
#include<iostream>
using namespace std;
int main()
{
int a[12],sum=0,rest=0;//sum For the money saved in mom's place
bool is = true;
for (int i = 0; i < 12; i++)
{
cin >> a[i];//a[i] It means the money spent this month . If you save money for mom , That's a flower .
}
for (int i = 0; i < 12; i++)
{
if (a[i] <= 100)//300-a[i] The rest of the money .
{
sum += 200;
a[i] += 200;//a[i] It means the money spent this month . If you save money for mom , That's a flower .
}
else if (a[i] <= 200)
{
sum += 100;
a[i] += 100;
}
rest += (300 - a[i]);// The money you save ,
if (rest >=100)
{
//cout << "rest is >=100 "<<rest << endl;
sum += 100;
rest -= 100;// Note that it's not just this month , The money left over from last month , She saves until next month .
}
if (rest < 0)
{
//cout << "rest is <0 " << rest << endl;
cout << "-" << i+1;
is = false;
break;
}
}
if (is == true)
{
cout << sum*1.2+rest;
}
return 0;
}
边栏推荐
- Keil compilation code of CY7C68013A
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- unable to execute xxx. SH: operation not permitted
- [C language] basic learning notes
- Landing guide for "prohibit using select * as query field list"
- The core idea of performance optimization, dry goods sharing
- Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
- Pit encountered in win11 pytorch GPU installation
- Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
- idea自动导包和自动删包设置
猜你喜欢
UNET deployment based on deepstream
Play with concurrency: what's the use of interruptedexception?
【c语言】动态规划---入门到起立
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Spring moves are coming. Watch the gods fight
Learn AI safety monitoring project from zero [attach detailed code]
The solution to the complexity brought by lambda expression
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
MySQL advanced SQL statement 2
随机推荐
[understand one article] FD_ Use of set
Play with concurrency: what's the use of interruptedexception?
How much is the tuition fee of SCM training class? How long is the study time?
二叉树解题(一)
The solution to the complexity brought by lambda expression
Introduction to vmware workstation and vSphere
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
I sorted out some basic questions about opencv AI kit.
oracle 存储过程与job任务设置
CY7C68013A之keil编译代码
Typescript practice for SAP ui5
How to solve the problem that objects cannot be deleted in Editor Mode
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
June book news | 9 new books are listed, with a strong lineup and eyes closed!
The confusion I encountered when learning stm32
How to write a client-side technical solution
【毕业季·进击的技术er】年少有梦,何惧彷徨
Thinkphp6 limit interface access frequency
cs架构下抓包的几种方法
Realize the function of data uploading