洛谷 - 一些好玩的问题 3

目录

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

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

P1957 口算练习题

小知识1:sprintf函数的用法

P1308 [NOIP2011 普及组] 统计单词数

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


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

  1  2  3  4
 12 13 14  5
 11 16 15  6
 10  9  8  7

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

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;    //从a[1][1]开始,方便操作
	
	while(k<n*n)
	{
		while(y<n&&a[x][y+1]==0) //从左到右
		a[x][++y]=++k;
		
		while(x<n&&a[x+1][y]==0) //从上到下
		a[++x][y]=++k;
		
		while(y>1&&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<<a[i][j]<<' ';
		//cout<<endl;
	}
	return 0;
}

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

#include <iostream>
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<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++)    //从1开始好操作
 	a[i][1]=a[i][i]=1;    //易看出,第一列和对角线为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]; //核心公式
		}
	}
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		cout<<a[i][j]<<' ';
		cout<<endl;
	}
		
	return 0;
}

P1957 口算练习题

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

小知识:

sprintf函数的用法

sprintf函数的格式:
int sprintf( char *buffer, const char *format [, argument,…] );

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

#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、可以将多个数值数据连接起来

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、

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

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

#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')//看第一个字符是数字还是字母
		{
			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<<s<<endl<<strlen(s)<<endl;
	}
	return 0;
}

P1308 [NOIP2011 普及组] 统计单词数

题目描述

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

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

noip2011普及组第2题

前期提要:

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

方法1、

    char word[100];
	char sentence[1000010];

	cin.getline(word,100);
	cin.getline(sentence,1000010);

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

方法2、

	string word;
	string sentence;

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

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

如int len = s.size();

AC思路:

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

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

#include<bits/stdc++.h>
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<len1;i++)
		if(isupper(word[i]))
			word[i]=tolower(word[i]);//全部转换成小写
	for(int i=0;i<len2;i++)
		if(isupper(jz[i]))
			jz[i]=tolower(jz[i]);
			
	int sum=0,wz=-1,x,y;
	for(int i=0;i<len2;i++)
	{
		if(i==0||jz[i-1]==' ')//找单词位置
		{
			x=i;
			while(i<len2&&jz[i]!=' ') i++;
			y=i;//找单词末尾的空格
			
			if(y-x==len1)//相减等于单词长度才继续,避免浪费
			{
				int flag=1;
				for(int j=x;j<y;j++)
				{
					if(jz[j]!=word[j-x])//判断是否相同
					{
						flag=0;
						break;
					}
				}	
				if(flag)
				{
					sum++;
					if(wz==-1)
					wz=x;
				}
			}
		}
	}
	if(sum)
	cout<<sum<<" "<<wz;
	else cout<<wz<<endl;
	return 0;
	
}

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NO.-LL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值