当前位置:网站首页>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;
}
边栏推荐
- Practical puzzle solving | how to extract irregular ROI regions in opencv
- 深度学习 网络正则化
- Red envelope activity design in e-commerce system
- UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
- Graduation season - personal summary
- 为什么国产手机用户换下一部手机时,都选择了iPhone?
- 局部修改-渐进型开发
- 如何配和弦
- (1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
- LVGL 8.2 Draw label with gradient color
猜你喜欢
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
如何配和弦
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
Detailed explanation of visual studio debugging methods
Query optimizer for SQL optimization
Expose Ali's salary and position level
电商系统中红包活动设计
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
5G电视难成竞争优势,视频资源成中国广电最后武器
随机推荐
局部修改-渐进型开发
MySQL stored procedure exercise
C language personal address book management system
Gin integrated Alipay payment
LVGL 8.2 LED
Node mongodb installation
No servers available for service: xxxx
Test evaluation of software testing
Nowcoder rearrange linked list
软件测试之测试评估
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
Leetcode t49: grouping of alphabetic words
各大主流编程语言性能PK,结果出乎意料
C language book rental management system
函数计算异步任务能力介绍 - 任务触发去重
openresty 限流
LVGL 8.2 keyboard
MySQL triggers
如何搭建一支搞垮公司的技术团队?
Digi restarts XBee Pro S2C production. Some differences need to be noted