当前位置:网站首页>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;
}边栏推荐
- remount of the / superblock failed: Permission denied
- 各大主流编程语言性能PK,结果出乎意料
- 深度学习 神经网络的优化方法
- 《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
- LVGL 8.2 Sorting a List using up and down buttons
- [C language] Pointer written test questions
- 如何配和弦
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
- Partial modification - progressive development
- 03-存储系统
猜你喜欢

Digi XBee 3 RF: 4个协议,3种封装,10个大功能

Docker compose public network deployment redis sentinel mode

Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

阿里被裁员工,找工作第N天,猎头又传来噩耗...

Practical puzzle solving | how to extract irregular ROI regions in opencv

Detailed explanation of visual studio debugging methods

LVGL 8.2 LED

LVGL 8.2 LED

How to match chords

IO flow: node flow and processing flow are summarized in detail.
随机推荐
IO流:节点流和处理流详细归纳。
Halo effect - who says that those with light on their heads are heroes
Ali was laid off employees, looking for a job n day, headhunters came bad news
First experience of ViewModel
局部修改-渐进型开发
一文概览2D人体姿态估计
深度学习 网络正则化
Ranking list of databases in July: mongodb and Oracle scores fell the most
电商系统中红包活动设计
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
LVLG 8.2 circular scrolling animation of a label
Digi重启XBee-Pro S2C生产,有些差别需要注意
【C语言】指针笔试题
阿里被裁员工,找工作第N天,猎头又传来噩耗...
如何搭建一支搞垮公司的技术团队?
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
Openresty redirection
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
LVGL 8.2 Draw label with gradient color
Programmer turns direction