当前位置:网站首页>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;
}边栏推荐
- flink sql-client. SH tutorial
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
- 关于miui12.5 红米k20pro用au或者povo2出现问题的解决办法
- Leetcode 61: rotating linked list
- Red envelope activity design in e-commerce system
- Respect others' behavior
- [C language] Pointer written test questions
- How to match chords
- MySQL triggers
- How to build a technical team that will bring down the company?
猜你喜欢

SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!

Leetcode 61: rotating linked list

自动控制原理快速入门+理解

Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions

SqlServer函数,存储过程的创建和使用

《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波

No servers available for service: xxxx

Talk about 10 tips to ensure thread safety

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

5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
随机推荐
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
Summary of common problems in development
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
开发中常见问题总结
leecode学习笔记-约瑟夫问题
C language course design questions
LVGL 8.2 List
Deep learning 7 transformer series instance segmentation mask2former
MySQL stored procedure exercise
Codeforce:c. sum of substrings
Ultrasonic distance meter based on 51 single chip microcomputer
Xcode abnormal pictures cause IPA packet size problems
Talk about 10 tips to ensure thread safety
深度学习 神经网络案例(手写数字识别)
92. (cesium chapter) cesium building layering
Partial modification - progressive development
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
智能客服赛道:网易七鱼、微洱科技打法迥异
LVGL 8.2 Sorting a List using up and down buttons