紫皮书第二章5个题

1、输出所有形如aabb的4位完全平方数

1、aabb
2、完全平方数

1、方法一:存在开平方,浮点运算可能存在误差
#include <iostream>
#include <cmath>
using namespace std;
int main() 
{
	int i,j;
	int n;
	for(i = 1;i <= 9; i++)
	for(j = 0;j <= 9; j++)
	{
		n=1100*i+11*j;
		int m = floor(sqrt(n));
		
		if(m*m == n)cout << n<<endl;
	}
	return 0;
}

2、枚举思路,避免开平方
#include <iostream>
#include <cmath>
using namespace std;
int main() 
{

		for(int x = 1;;x++)
		{
			
			int n = x *x;
			if( n< 1000) continue;
			if( n > 9999) break;	
			
			int hi = n/100;
			int lo = n%100;
			if(hi / 10==hi%10 && lo/10==lo%10)cout << n<<endl;
		}
	
	return 0;
}

1、round()函数 四舍五入,返回double
2、ceil() 函数,向上取整,返回double
3、floor()函数,向下取整,返回double
4、判断一个数m是否为整数。 m==(int)m

2、开灯问题

有n盏灯,编号为1~n,第一个人把所有的灯打开,第二个人按下所有编号为2的倍数的灯的开关,第三个人按下所有编号为3的倍数的灯的开关,以此类推,一共K个人,问最后哪些灯开着,输入n和k,输出开着的灯的编号。k<=n<=1000.

1、

#include <iostream>
#include <cmath>
#include <cstring>
const int maxn =1010;
int a[maxn];
using namespace std;
int main() 
{
	memset( a, 0, sizeof(a));
	
	//0为开1为关
	int n,k;
	int first=1;
	cin >>n >> k;
	for(int i= 1;i <=k;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(j%i == 0) a[j]=!a[j];
		}
	}
	for(int i=1;i <=n;i++)
	{
		if(a[i]) {
		if(first) first=0; else cout << " ";cout << i;
	}}
	//输出格式 
	cout <<endl;
	return 0;
}

3、蛇形填数
输入 n 填入 n * n的方格中 ,形如
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4

n <= 8
1、填入二维数组,顺序输出
2、运用循环 已赋值判断

#include <iostream>
#include <cmath>
#include <cstring>
const int maxn =10;
int a[maxn][maxn];
using namespace std;
int main() 
{
	int n;

	while (cin>> n&& n<=8)
	{
		memset (a,0,sizeof(a));
		int sum=0;
		int x,y;
		sum = a[x = 0][y = n -1] =1;
		while(sum < n*n)
		{
			//上左下右
			//判断 下一个点存在且未赋值
			cout << x <<" "<< y<< endl;
			while(x +1 <n &&!a[x+1][y]) a[++x][y]=++sum; 
			while(y -1 >=0&&!a[x][y-1]) a[x][--y]=++sum; 
			while(x -1 >=0&&!a[x-1][y]) a[--x][y]=++sum; 
			while(y +1 <n&& !a[x][y+1]) a[x][++y]=++sum; 
		}
		
		for(int i=0;i < n;i++)
		{
			for(int j=0;j <n;j++)cout << a[i][j]<< "  ";
			cout << endl;
		}
		
	}
	return 0;
}

4、竖式问题
输入数字集合,找到形如abc * de (三位数*二维数)的算式,并输出

1、利用字符进行操作
2、将数字输入字符串 sprintf()函数
3、利用strchr (s,s[i]);判断字符是否存在在字符串中

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main() 
{
	int count = 0;
	char s[20],buf[99];
	scanf("%s",s);
	for(int abc = 111;abc <=999; abc++)
	{
		for(int de =11; de <= 99;de++)
		{
			int x= abc * (de %10),y= abc * (de /10),z = abc *de;
			sprintf(buf ,"%d%d%d%d%d",abc,de ,x, y, z);
			//将整数输入字符数组 
			int ok =1;
			for(int i = 0;i < strlen(buf);i++)
			if(strchr(s,buf[i]) == NULL) ok = 0;
			//在字符串S中查找单个字符 buf[i]。strchr 返回的是指针,如果不存在,则返回NULL 
			if(ok)
			{
				printf("<%d>\n",++count);
				printf("%5d\nX%4d\n-----\n%5d\n%4d\n------\n%5d\n\n",abc,de,x,y,z); 
			}
		
		}
	}
	cout << "The number of solution ="<<count <<endl;
	return 0;
}

5、一段英语,单词的字典序排列,去重。
1、单词的输入,转换成小写字母
2、单词的存储 set集合 set dict; dict.insert();
3、集合的输出 iterator dict.begin(), dict.end().类似于指针。

#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;

set<string> dict; 
int main() 
{
	string s,buf;
	while(cin >> s)
	{
		 for(int i =0;i <s.length(); i++)
		 if(isalpha(s[i])) s[i] = tolower(s[i]);else s[i] = ' ';
		 
		//dict.insert(s);当存在空格时,无法排除 
		stringstream ss(s);
		while (ss >> buf)dict.insert(buf); 
	}
	for(set<string>::iterator it = dict.begin();it !=dict.end();++it)
	cout << *it << "\n";

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值