当前位置:网站首页>Luo Gu - some interesting questions
Luo Gu - some interesting questions
2022-07-04 14:51:00 【NO.-LL】
P5731 【 Deep base 5. xi 6】 Serpentine matrix - Output format

1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
It can be said to be a water problem , But all WA,debug For a long time , It turns out that the output format is toxic
AC:
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
int main()
{
int n;
cin>>n;
int k=0,i,j;
int x=1,y=0; // from a[1][1] Start , Convenient operation
while(k<n*n)
{
while(y<n&&a[x][y+1]==0) // From left to right
a[x][++y]=++k;
while(x<n&&a[x+1][y]==0) // From top to bottom
a[++x][y]=++k;
while(y>1&&a[x][y-1]==0) // From right to left
a[x][--y]=++k;
while(x>1&&a[x-1][y]==0) // From bottom to top
a[--x][y]=++k;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",a[i][j]);
printf("\n");
//cout<<a[i][j]<<' ';
//cout<<endl;
}
return 0;
}P5732 【 Deep base 5. xi 7】 Yang hui triangle - Classical mathematics

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Ideas : So let's define one Two dimensional array :a[N][N], Slightly larger than the number of lines to print . Let the number on both sides be 1, That is, when the first and last number of each row are 1.a[i][0]=a[i][i-1]=1,n Is the number of rows . Except for the numbers on both sides , Any number is the sum of the upper two vertices , namely a[i][j] = a[i-1][j-1] + a[i-1][j].
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
typedef long long LL;
const int N=1e5+10;
using namespace std;
int a[21][21];
int n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) // from 1 Start good operation
a[i][1]=a[i][i]=1; // Easy to see , The first column and diagonal are 1
for(int i=1;i<=n;i++)
{
for(int j=2;j<i;j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1]; // The core formula
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
return 0;
}P1957 Oral arithmetic exercises

4 a 64 46 275 125 c 11 99 b 46 64
Little knowledge :
sprintf Function usage
sprintf Format of function :
int sprintf( char *buffer, const char *format [, argument,…] );
1、 Can control the accuracy , Can also “ rounding ”
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char str1[200];
char str2[200];
double f1=14.305100;
double f2=14.304900;
//double f2=14.305000;
sprintf(str1,"%20.2f\n",f1);
sprintf(str2,"%20.2f\n",f2);
cout<<str1<<str2;
return 0;
}
2、 You can connect multiple numerical data
char str[20];
int a=20984,b=48090;
sprintf(str,”%3d%6d”,a,b);
str[]=”20984 48090”3、 You can connect multiple strings into a string
char str[20];
char s1[5]={'A','B','C'};
char s2[5]={'x','y','z'};
sprintf(str,"%.3s%.3s",s1,s2);
str="ABCxyz";%m.n In the output of the string ,m Width , The number of columns shared by strings ;n Represents the actual number of characters .%m.n In floating point numbers ,m Also means width ;n Represents the number of decimal places .
4、
The subsequent operations are basically the same as printf equally , nothing but sprintf Function to print to a string ( Note that the length of the string should be enough to accommodate the printed content , Otherwise, there will be a memory overflow ), and printf Function prints out to the screen .
understand sprintf After usage , This is a direct second kill :
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
typedef long long LL;
const int N=1e5+10;
using namespace std;
int main()
{
char ch;
int n,a,b;
char s[10010],x[2];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
if(x[0]>='a'&&x[0]<='c')// See whether the first character is a number or a letter
{
ch=x[0];
cin>>a>>b;// It is letters that directly output numbers
}
else
{
sscanf(x,"%d",&a);// It is the conversion of characters and numbers into numbers
cin>>b;
}
if(ch=='a')
sprintf(s,"%d+%d=%d",a,b,a+b);// Direct output , Very easy to use
else if(ch=='b')
sprintf(s,"%d-%d=%d",a,b,a-b);
else if(ch=='c')
sprintf(s,"%d*%d=%d",a,b,a*b);
cout<<s<<endl<<strlen(s)<<endl;
}
return 0;
}边栏推荐
- 实战解惑 | OpenCV中如何提取不规则ROI区域
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
- Exploration and practice of eventbridge in the field of SaaS enterprise integration
- Docker compose public network deployment redis sentinel mode
- LVGL 8.2 List
- C language small commodity management system
- [algorithm leetcode] interview question 04.03 Specific depth node linked list (Multilingual Implementation)
- Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- LVGL 8.2 keyboard
猜你喜欢

如何配和弦

scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月

Memory management summary

Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000

No servers available for service: xxxx

Data Lake (13): spark and iceberg integrate DDL operations

10. (map data) offline terrain data processing (for cesium)

Xcode abnormal pictures cause IPA packet size problems

实战解惑 | OpenCV中如何提取不规则ROI区域

Why do domestic mobile phone users choose iPhone when changing a mobile phone?
随机推荐
Xcode abnormal pictures cause IPA packet size problems
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
92. (cesium chapter) cesium building layering
LVGL 8.2 text shadow
Transplant tinyplay for imx6q development board QT system
[information retrieval] link analysis
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
Popular framework: the use of glide
Expose Ali's salary and position level
Compile oglpg-9th-edition source code with clion
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
LVGL 8.2 Sorting a List using up and down buttons
为什么国产手机用户换下一部手机时,都选择了iPhone?
Count the running time of PHP program and set the maximum running time of PHP
Node mongodb installation
Exploration and practice of eventbridge in the field of SaaS enterprise integration
深度学习 网络正则化
PLC模拟量输入 模拟量转换FC S_ITR (CODESYS平台)
Practical puzzle solving | how to extract irregular ROI regions in opencv
Respect others' behavior