首页 >> 大全

P5731 【深基5.习6】蛇形方阵 - 输出格式

2023-06-26 大全 51 作者:考证青年

目录

P5731 【深基5.习6】蛇形方阵 - 输出格式

P5732 【深基5.习7】杨辉三角 - 经典数学

P1957 口算练习题

小知识1:函数的用法

P1308 [ 普及组] 统计单词

小知识2:C++输入带空格字符串;

P5731 【深基5.习6】蛇形方阵 - 输出格式

  1  2  3  412 13 14  511 16 15  610  9  8  7

可以说是水题了,但是全WA,debug了半天,原来是输出格式有毒

AC:

#include
using namespace std;int a[10][10];
int main()
{int n;cin>>n;int k=0,i,j;int x=1,y=0;    //从a[1][1]开始,方便操作while(k1&&a[x][y-1]==0) //从右到左a[x][--y]=++k;while(x>1&&a[x-1][y]==0) //从下到上a[--x][y]=++k;}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");//cout<

如果是n*m的蛇形方阵呢?

#include 
using namespace std;const int N = 110;
int a[N][N];int main()
{int r,c;cin >> r >> c;int left = 0, right = c - 1;int top = 0, bottom = r - 1;int k = 1;while(left <= right || top <= bottom){for(int i = left; i <= right && top <= bottom; i++)//构造最上面一行{a[top][i] = k++;}top++;for(int i = top; i <= bottom && left <= right; i++)//构造最右侧一列{a[i][right] = k++;}right--;for(int i = right; i >= left && top <= bottom; i--)//构造最下面一行{a[bottom][i] = k++;}bottom--;for(int i = bottom; i >= top && left <= right; i--)//构造最左侧一列{a[i][left] = k++;}left++;}for(int i = 0; i < r; i++){for(int j = 0; j < c; j++) printf("%3d",a[i][j]);cout << endl;}return 0;
}

P5732 【深基5.习7】杨辉三角 - 经典数学

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

思路:先定义一个二维数组:a[N][N],略大于要打印的行数。再令两边的数为 1,即当每行的第一个数和最后一个数为 1。a[i][0]=a[i][i-1]=1,n 为行数。除两边的数外,任何一个数为上两顶数之和,即 a[i][j] = a[i-1][j-1] + a[i-1][j]。

#include
#include
#include
#include
#include
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++)    //从1开始好操作a[i][1]=a[i][i]=1;    //易看出,第一列和对角线为1for(int i=1;i<=n;i++){for(int j=2;j

P1957 口算练习题

4
a 64 46
275 125
c 11 99
b 46 64

小知识:

函数的用法

函数的格式:

int ( char *, const char * [, ,…] );

1、可以控制精度,还能“四舍五入”

#include
#include
#include
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<

2、可以将多个数值数据连接起来

char str[20];
int a=20984,b=48090;
sprintf(str,”%3d%6d”,a,b);
str[]=”20984 48090”

3、 可以将多个字符串连接成字符串

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在字符串的输出中,m表示宽度,字符串共占的列数;n表示实际的字符数。%m.n在浮点数中,m也表示宽度;n表示小数的位数。

4、

之后的操作基本跟一样,只不过是函数打印到字符串中(要注意字符串的长度要足够容纳打印的内容,否则会出现内存溢出),而函数打印输出到屏幕上。

明白用法后,这题直接秒杀:

#include
#include
#include
#include
#include
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>x;if(x[0]>='a'&&x[0]<='c')//看第一个字符是数字还是字母{ch=x[0];cin>>a>>b;//是字母直接输出数字}else{sscanf(x,"%d",&a);//是字符数字转化成数字cin>>b;}if(ch=='a')sprintf(s,"%d+%d=%d",a,b,a+b);//直接输出,非常好用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<

P1308 [ 普及组] 统计单词数 题目描述

一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。

现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同(参见样例1 ),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2 )。

输入格式

共2行。

第1行为一个字符串,其中只含字母,表示给定单词;

第2行为一个字符串,其中只可能包含字母和空格,表示给定的文章。

输出格式

一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从00开始);如果单词在文章中没有出现,则直接输出一个整数-1。

输入输出样例

输入 #1

To
to be or not to be is a question

输出 #1

2 0

输入 #2

to
Did the Ottoman Empire lose its power at that time

输出 #2

-1

说明/提示

数据范围

1≤1≤第一行单词长度≤10≤10。

1≤1≤文章长度≤1,000,000≤1,000,000。

普及组第2题

前期提要:

C++输入带空格字符串;

方法1、

    char word[100];char sentence[1000010];cin.getline(word,100);cin.getline(sentence,1000010);

计算字符串长度时只能用():int len =(s);

方法2、

	string word;string sentence;getline(cin,word);getline(cin,sentence);

计算字符串长度时只不能用(),只能用size()or (),

如int len =s.size();

AC思路:

由于是不区分大小写的,在执行的过程中难以直接进行转换,我们就得转换思路,遇到这种变化的,我们一开始就全部转成固定的,也就是将输入的单词和句子全部转换为小写,这样就后面就好执行了。既然要转换,那么就必定要两个字符串的长度,然后进行for循环来转换

大小写转换完成后,就要开始执行了,思路是从句子中,每次记录下单词的开始坐标和结束坐标,然后与单词进行比较,比较成功就sum++,然后记录下第一次比对成功的坐标即可

#include
using namespace std;
int main()
{char word[100];char jz[1000010];cin.getline(word,100);cin.getline(jz,1000010);int len1=strlen(word);int len2=strlen(jz);for(int i=0;i

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了