算法入门经典-2

#include<iostream>
using namespace std;
int main()
{
	int a,count = 0;
	cin >> a;
	for(int i = 1;i < 1000000000;i *= 10)
	{
		if(a/i != 0)
			count++;
	}
	cout << count << endl;
	return 0;
}
/*算法入门第二章课后习题2-1位数*/


#include<iostream>
using namespace std;
int main()
{
	int a,b,c;
	for(int i = 100; i < 1000; i++)
	{
		a = i/100;
		b = (i/10)%10;
		c = i%10;
		if(i == a*a*a + b*b*b + c*c*c)
			cout << i << endl;
	}
	return 0;
}
/*算法设计入门第二章2-2水仙花数*/

#include <iostream>
using namespace std;
int main()
{
	int a,b,c;
	cin >> a >> b >> c;
	bool mark = false;
	for(int i=10;i<=100;i++)
	{
		if(i == (70*a + 21*b + 15*c) % 105)
		{
			cout << i << endl;
			mark = true;
			break;
		}
	}
	if(false == mark)
			cout << "No answer" << endl;
	return 0;
}
/*韩信点兵(2-3)------中国剩余定理
  ①使用前提:其一是除数都为素数,其二所求数需小于最小公倍数
  x % 3 = a;
  x % 5 = b;
  x % 7 = c;
  中国剩余定理有公式:x = (70*a + 21*b +15*c) % n;
  70是5和7的倍数,用3除余1;
  21是3和7的倍数,用5除余1;
  15是3和5的倍数,用7除余1;
  n为3,5,7的最小公倍数,为105;
*/

#include<iostream>
using namespace std;
int main()
{
	int n,p;
	cin >> n;
	p = n;   // 记录倒序第几行
	for(int i = 0; i<n; i++,p--)
	{
		int j = i;
		while(j--)
		{
		  cout << " ";
		}    // 填充左边空白处  
		int q = 1 + (p-1)*2;
		while(q--)
		{
			cout << "#";
		}    // 输出#三角一行 
		cout << endl; //换行输出
	}
	return 0;
}
/*
  输出类似倒三角图形(2-4)

   #########
    #######
     #####  
      ###   
       #
*/

#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
	vector <int> remember;
	int n;
	cin >> n;
	for(int i = 0;i < n;i++)
	{
		int x;
		cin >> x;
		remember.push_back(x);
	}
	int m;
	cin >> m;
	int count = 0;
	for(vector<int>::iterator beg = remember.begin(); beg != remember.end();beg++)
	{
		if(*beg <= m)
			count++;
	}
	cout << count << endl;
	return 0;
}
/*算法设计第二章2-5,输入数据比较大小再输出统计结果
  对vector的使用还不是很熟,看完书再回头看看这题!!!
*/

#include <iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	double sum = 0;
	double count = 1.0;
	for(int i = 1;i <= n;i++)
	{
		sum += 1/count;
		count++;
	}
	cout.setf(ios_base::fixed,ios_base.floatfield);
	cout.precision(3);
	cout << sum <<endl;
	return 0;
}
/*注意浮点的计算的表示输出问题(2-6)
 
   记住这两句作用
   cout.setf(ios_base::fixed,ios_base.floatfield);
   cout.precision(3);
*/

#include <iostream>
using namespace std;
int main()
{
	double sum = 0;
	double p = 1.0;
	double i = 1.0;
	int j = 1;
	for(; p >=0.0000001;i +=2,j++)
	{
		p = 1.0/i;
		if(j % 2 != 0)
		{
			sum +=p;
		}
		else
			sum -=p;
	}
	cout.setf(ios_base::fixed,ios_base::floatfield);
	cout.precision(6);
	cout << sum*4 << endl;
	return 0;
}
/*近似计算PI(2-7)*/

#include <iostream>
using namespace std;
int main()
{
	double a,b,m,n;
	cin >> a >> b;
	if(a > b)
	{
		m = a;
		n = b;
	}
	else
	{
		m = b;
		n = a;
	}
	double sum = 0;
	for(double i = n; i<=m; i++)
	{
		sum += 1.0/(i*i);
	}
	cout.setf(ios_base::fixed,ios_base::floatfield);
	cout.precision(5);
	cout << sum << endl;
	return 0;
}
/*子序列求和(2-8)*/

#include<iostream>
using namespace std;
int main()
{
	double a,b;
	int c;
	cin >> a >> b >> c;
	cout.setf(ios_base::fixed,ios_base::floatfield);
	cout.precision(c);
	cout << a/b << endl;
	return 0;
}
/*分数化小数,输出指定位数的结果(2-9)*/

#include <iostream>
using namespace std;

inline bool haveSame(const int t,const int (&c)[10])
{
	for(int i = 0; i < 10; i++)
	{
		if(t == c[i])
		{
			return true;
		}
	}
	return false;
}

int main()
{
	int a,b,c,d,e,f,g,h,i;
	int compare [10];
	for(int j = 123; j<333; j++)
	{
		for(int p = 0; p<10; p++)
		{
		  compare[p] = 0;
		}

		a = j/100;
		compare[1] = a;

		b = j%100/10;
		if(haveSame(b,compare))
		{
			continue;
		}
		compare[2] = b;

		c = j%10;
		if(haveSame(c,compare))
		{
			continue;
		}
		compare[3] = c;

		int q = j*2;

		d = q/100;
		if(haveSame(d,compare))
		{
			continue;
		}
		compare[4] = d;

		e = q%100/10;
		if(haveSame(e,compare))
		{
			continue;
		}
		compare[5] = e;

		f = q%10;
		if(haveSame(f,compare))
		{
			continue;
		}
		compare[6] = f;

		int s = j*3;

		g = s/100;
		if(haveSame(g,compare))
		{
			continue;
		}
		compare[7] = g;

		h = s%100/10;
		if(haveSame(h,compare))
		{
			continue;
		}
		compare[8] = h;

		i = s%10;
		if(haveSame(i,compare))
		{
			continue;
		}
		compare[9] = i;

		cout << j << ":" << q << ":" << s << endl;
	}
	return 0;
}


/*排列输出所有解(2-10)*/


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页