Example 4.1

利用for循环,计算输出1+2+...+100的和。

Example 4.2

输出1~100之间所有偶数。

Example 4.3

利用for循环,分别计算1~100中的奇数的和、偶数的和。

Example 4.4

利用for循环计算n!的值。

Exercise 1

  1. 求平均年龄http://noi.openjudge.cn/ch0105/01/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n,a;
    	double sum = 0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		sum = sum + a;	
    	}
    	
    	printf("%.2f",sum/n);	
    }
    
  2. 财务管理http://noi.openjudge.cn/ch0105/02/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	double a;
    	double sum = 0;
    	for (int i=0;i<12;i++){
    		cin >> a;
    		sum = sum + a;	
    	}
    	
    	printf("$%.2f",sum/12);	
    }
    
  3. 均值http://noi.openjudge.cn/ch0105/03/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n;
    	double a;
    	double sum = 0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		sum = sum + a;	
    	}
    	
    	printf("%.4f",sum/n);	
    }
    
  4. 求整数的和与均值http://noi.openjudge.cn/ch0105/04/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n,a,sum = 0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		sum = sum + a;	
    	}
    	printf("%d %.5f",sum,(double)sum/n);	
    	return 0;
    }
    
  5. 最高的分数http://noi.openjudge.cn/ch0105/05/
    #include <iostream>
    using namespace std;
    //05:最高的分数
    int main(){
    	
    	int n,a,max = 0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		if (a>max){
    			max = a;
    		}	
    	}
    	printf("%d",max);	
    	return 0;
    }
    
  6. 整数序列的元素最大跨度值http://noi.openjudge.cn/ch0105/06/
    #include <iostream>
    using namespace std;
    //06:整数序列的元素最大跨度值
    int main(){
    	
    	int n,a,max = 0,min=10000;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		if (a>max){
    			max = a;
    		}
    		if(a<min){
    			min = a;
    		}
    	}
    	printf("%d",max-min);	
    	return 0;
    }
    
  7. 奥运奖牌计数http://noi.openjudge.cn/ch0105/07/
    #include <iostream>
    using namespace std;
    //07:奥运奖牌计数
    int main(){
    	
    	int n,g,s,c,sg=0,ss=0,sc=0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> g >> s >> c;
    		sg = sg + g;
    		ss = ss + s;
    		sc = sc + c; 
    	}
    	printf("%d %d %d %d",sg,ss,sc,sg+ss+sc);	
    	return 0;
    }
    
  8. 多边形内角和http://noi.openjudge.cn/ch0105/08/
    #include <iostream>
    using namespace std;
    //08:多边形内角和
    int main(){
    	
    	int n,a,sum=0;
    	cin >> n;
    	for (int i=1;i<n;i++){
    		cin >> a;
    		sum = sum + a;
    	}
    	printf("%d",(n-2)*180-sum);	
    	return 0;
    }
    
  9. 奇数求和http://noi.openjudge.cn/ch0105/09/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int m,n;
    	int sum = 0;
    
    	cin >> m >> n;
    	for(int i=m;i<=n;i++){
    		if(i%2==1){
    			sum = sum + i;
    		}
    	}	
    	cout << sum << endl;
    	
    	return 0;
    }
    
  10. 满足条件的数累加http://noi.openjudge.cn/ch0105/10/
    #include <iostream>
    using namespace std;
    //10:满足条件的数累加
    int main(){
    	
    	int m,n,sum=0;
    	cin >> m >> n;
    	for (int i=m;i<=n;i++){
    		if(i%17==0){
    			sum = sum + i;
    		}
    	}
    	printf("%d",sum);	
    	return 0;
    }
    
  11. 整数的个数http://noi.openjudge.cn/ch0105/11/
    #include <iostream>
    using namespace std;
    //11:整数的个数
    int main(){
    	
    	int n,a,s1=0,s5=0,s10=0;
    	cin >> n;
    	for (int i=0;i<n;i++){
    		cin >> a;
    		if(a==1){
    			s1 = s1 + 1;
    		}
    		if(a==5){
    			s5 = s5 + 1;
    		}
    		if(a==10){
    			s10 = s10 + 1;
    		}
    	}
    	cout << s1 << endl;	
    	cout << s5 << endl;	
    	cout << s10 << endl;	
    	return 0;
    }
    
  12. 与指定数字相同的数的个数http://noi.openjudge.cn/ch0105/12/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int N,m,a,sum=0;
    
    	cin >> N >> m;
    	for (int i=0;i<N;i++){
    		cin >> a;
    		if(a==m){
    			sum = sum + 1;
    		}
    	}
    	cout << sum << endl;	
    
    	return 0;
    }
    
  13. 乘方计算http://noi.openjudge.cn/ch0105/13/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a,n,mul=1;
    
    	cin >> a >> n;
    	for(int i=0;i<n;i++)
    	{
    		mul = mul * a;
    	}
    	cout << mul << endl;
    			
    	return 0;
    }
    
  14. 人口增长问题http://noi.openjudge.cn/ch0105/14/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	double x,n;
    
    	cin >> x >> n;
    	printf("%.4f",x*pow((1+0.001),n));
    			
    	return 0;
    }
    
  15. 银行利息http://noi.openjudge.cn/ch0105/15/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double R,M,Y;
    	
    	cin >> R >> M >> Y;
    	for (int i=0;i<Y;i++)
    	{
    		M = M * (1 + R/100);
    	}
    	printf("%d",(int)M);
    	
    	return 0;
    } 
    
  16. 买房子http://noi.openjudge.cn/ch0105/16/
    #include <iostream>
    using namespace std;
    
    int main(){
    	double sumn,hp=200,n,k;
    	int m=1;
    	cin >> n >> k;
    	sumn = n;
    	while(sumn<hp && m <40){
    		sumn = sumn + n;
    		hp = hp * (1+k/100);
    		m = m + 1;
    		
    	}
    	
    	if(m<=20)
    		cout << m << endl;
    	else
    		cout << "Impossible" << endl;
    	
    	return 0;
    }
    
  17. 菲波那契数列http://noi.openjudge.cn/ch0105/17/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int k,first=1,second=1,fb;
    	
    	cin >> k;
    	if (k > 2)
    		for (int i=3;i<=k;i++)
    		{
    			fb = first + second;
    			first = second;
    			second = fb;
    		}
    	else
    		fb = 1;
    	cout << fb << endl;
    			
    	return 0;
    } 
    
  18. 鸡尾酒疗法http://noi.openjudge.cn/ch0105/18/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	double a[20][2];
    	
    	cin >> n;
    	for (int i=0;i<n;i++)
    	{
    		cin >> a[i][1] >> a[i][2];
    	}
    	
    	double x = a[0][2] / a[0][1] * 100;
    	for(int i=1;i<n;i++){
    		double y = a[i][2] / a[i][1] * 100;
    		if (y-x>5) {
    			cout << "better" << endl;	
    		}else if (x-y>5){
    			cout << "worse" << endl;	
    		}else{
    			cout << "same" << endl;	
    		}
    	}
    			
    	return 0;
    } 
    
  19. 救援http://noi.openjudge.cn/ch0105/19/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int n;
    	double a,b,c,t=0;
    	
    	cin >> n;
    	for(int i=0;i<n;i++)
    	{
    		cin >> a >> b >> c;
    		t = t + sqrt(a*a+b*b) * 2 / 50 + 1.5 * c;
    	}
    	printf("%.0f",ceil(t));
    	
    	return 0;	
    } 
    
  20. 津津的储蓄计划http://noi.openjudge.cn/ch0105/22/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long p,a,r =0,sum=0;
    
    	for(int i=0;i<12;i++)
    	{
    		r = r + 300;
    		cin >> p;
    		r = r - p;
    		a = r/100;
    		sum = sum + a * 100;
    		r = r - a * 100;
    		if (r<0){
    			cout << "-" << i+1 << endl;
    			break;
    		}	 
    	}
    	if (r>0)
    		cout << sum * 1.2 + r << endl;
    	
    	return 0;	
    } 
    
  21. 药房管理http://noi.openjudge.cn/ch0105/23/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long m,n,a,sum=0;
    	
    	cin >> m >>n;
    	for(int i=0;i<n;i++)
    	{
    		cin >> a;
    		if (m -a >= 0)
    			m = m - a;
    		else
    			sum = sum + 1;
    	}
    	cout << sum  << endl;
    	
    	return 0;	
    } 
    
  22. 正常血压http://noi.openjudge.cn/ch0105/24/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long n,a,b,sum=0,max=0;
    	
    	cin >> n;
    	for(int i=0;i<n;i++)
    	{
    		cin >> a >> b;
    		if (a>=90 && a<=140 && b >=60 && b <=90)
    		{
    			sum = sum + 1;		
    		}else{
    			if (sum > max)
    				max = sum;
    			sum = 0;
    		}
    	}
    	if (sum > max) max = sum;
    	cout << max  << endl;
    	
    	return 0;	
    } 
    
  23. 求特殊自然数http://noi.openjudge.cn/ch0105/25/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int i,j,k;
    	
    	bool flag = false;
    	for(i=0;i<7;i++)
    	{
    		for(j=0;j<7;j++)
    		{
    			for(k=0;k<7;k++){
    				if (i*9*9+j*9+k==i+j*7+k*7*7 && (i!=0 || j!=0 || k!=0))
    				{
    					cout << i*9*9+j*9+k  << endl;
    					cout << k << j << i << endl;
    					cout << i << j << k << endl; 
    				}
    			}
    		}
    	}
    	
    	return 0;	
    } 
    
  24. 统计满足条件的4位数个数http://noi.openjudge.cn/ch0105/26/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int n,a,sum=0;
    	int first,second,third,forth;
    	
    	cin >> n;
    	for(int i=0;i<n;i++)
    	{
    		cin >> a;
    		forth = a % 10;
    		third = (a - forth)/10 % 10;
    		second = (a-forth - third * 10)/100 % 10;
    		first = (a - forth - third*10 - second * 100)/1000;
    		if (forth - first - second - third > 0)
    		{
    			sum = sum + 1;
    		}
    	}
    	cout << sum << endl;
    	
    	return 0;
    } 
    
  25. 开关灯http://noi.openjudge.cn/ch0105/31/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long N,M;
    	bool light = true;
    
    	cin >> N >> M;
    	cout << 1 ;
    	for(int i=2;i<=N;i++)
    	{
    		light = true;	
    		for(int j=1;j<=M;j++)
    		{
    			if(i%j==0)
    			{
    				light = ! light;
    			}
    		}
    		if(!light)
    			cout << "," << i;
    		
    	}
    	
    	return 0;
    } 
    
  26. 求分数序列和http://noi.openjudge.cn/ch0105/32/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	double p=1,q=2,sum = 0,temp;
    
    	cin >> n;
    	for (int i=0;i<n;i++)
    	{
    		sum = sum + q / p;
    		temp = q;
    		q = q + p;
    		p = temp;
    	}
    	printf("%.4f",sum);
    		
    	return 0;
    } 
    
  27. 计算分数加减表达式的值http://noi.openjudge.cn/ch0105/33/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	double sum = 0;
    
    	cin >> n;
    	for (int i=1;i<=n;i++)
    	{
    		if(i%2==1)
    			sum = sum + 1.0d/i;
    		else
    			sum = sum - 1.0d/i;
    	}
    	printf("%.4f",sum);
    		
    	return 0;
    } 
    
  28. 余数相同问题http://noi.openjudge.cn/math/7647/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int a,b,c;
    	cin >> a >> b >> c;
    	
    	for(int i=2;i<1000000;i++){
    		int t = a%i;
    		if(b%i==t && c%i==t){
    			cout << i << endl;
    			break;
    		}
    	}
    	
    	return 0;
    } 
    
  29. 分苹果http://noi.openjudge.cn/math/7826/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int n,sum=0;
    	
    	cin >> n;
    	for(int i=1;i<=n;i++){
    		sum = sum + i;
    	}
    	cout << sum << endl;
    	
    	return 0;
    }
    
  30. 求小数的某一位http://noi.openjudge.cn/math/7830/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int a,b,n;
    	
    	cin >> a >> b >> n;
    	
    	for(int i=1;i<=n;i++){
    		a = a%b;
    		a = a*10;
    	}
    	
    	cout << a/b << endl;
    	
    	return 0;
    }
    
  31. 计算星期几http://noi.openjudge.cn/math/7831/
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    string wk[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    
    int main(){
    	int a,b,t=1;
    	
    	cin >> a >> b;
    	
    	for(int i=1;i<=b;i++){
    		t = t * a ;
    		t = t % 7;
    	} 
    	
    	cout << wk[t] << endl;
    	
    	return 0;
    }
    
  32. 幂的末尾http://noi.openjudge.cn/math/7833/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int a,b,t=1;
    	
    	cin >> a >> b;
    	
    	for(int i=1;i<=b;i++){
    		t = t * a;
    		t = t % 1000;
    	}
    	
    	if(t<10)
    		cout << "00" << t << endl;
    	else if(t<100)
    		cout << "0" << t << endl;
    	else
    		cout << t << endl;
    	
    	return 0;
    }
    

Example 4.5

求s=1+2+3+...+n,当加到第几项时,s的值会超过1000?

Example 4.6

求两个正整数m,n的最大公约数。

Example 4.7

编一程序求满足不等式1+1/2+1/3+...+1/n>=5的最小n值。

Example 4.8

输入一个整数,求出他们的最小值,最大值和平均值(保留3位小数)。输入保证这些数都是不超过1000的整数。

输入样例:2 8 3 5 1 7 3 6

输出样例:1 8 4.375

Example 4.9

对于求两个正整数m,n的最大公约数可以用do-while实现。

Example 4.10

求1992个1992的乘积的末两位数是多少?

Example 4.11

校体操队到操场集合,排成每行2人,最后多出1人;排成每行3人,也多出1人,分别按每行排4,5,6人,都多出1人;当排成每行7人时,正好不多。求校体操队至少多少人?

Exercise 2

  1. 球弹跳高度的计算http://noi.openjudge.cn/ch0105/20/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double h,sum=0;
    	
    	cin >> h;
    	
    	sum = h;
    	for(int i=1;i<10;i++)
    	{
    		sum = sum + h;
    		h = h / 2;
    	}
    	cout << sum << endl;
    	cout << h/2 << endl;
    	
    	return 0;	
    } 
    
  2. 角谷猜想http://noi.openjudge.cn/ch0105/21/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long long int N;
    	cin >> N;
    	
    	while(N>1)
    	{
    		if(N%2==1)
    		{
    			cout << N << "*3+1=" << N * 3 + 1 << endl;
    			N = N * 3 + 1;
    		}else{
    			cout << N << "/2=" << N / 2 << endl;
    			N = N / 2; 
    		} 
    	}
    	cout << "End";
    	
    	return 0;	
    } 
    
  3. 级数求和http://noi.openjudge.cn/ch0105/27/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long K,n=0;
    	double sn = 0;
    	
    	cin >> K;
    	while(sn<=K)
    	{
    		n = n + 1;
    		sn = sn + 1.0d/n;
    	}
    	cout << n << endl;
    			
    	return 0;
    } 
    
  4. 分离整数的各个数位http://noi.openjudge.cn/ch0105/28/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long long int n,r;
    	
    	cin >> n;
    	while(n>0)
    	{
    		r = n % 10;
    		cout << r;
    		n = (n - r)/10;
    		if (n>0)
    		{
    			cout << " ";
    		} 
    	}
    	cout << endl;
    				
    	return 0;
    } 
    
  5. 数字反转http://noi.openjudge.cn/ch0105/29/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long long int n,i=0;
    	
    	cin >> n;
    	while(n!=0)
    	{
    		i = i*10 + n % 10;
    		n = n / 10;
    	}
    	printf("%d",i);
    	
    	return 0;
    } 
    
  6. 含k个3的数http://noi.openjudge.cn/ch0105/30/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	long m,k,sum=0;
    	
    	cin >> m >> k;
    	if (m % 19 ==0)
    	{
    		while(m!=0)
    		{
    			if (m % 10==3)
    			{
    				sum = sum + 1;
    			}
    			m = m / 10;
    		}
    		if (sum==k)
    			cout << "YES" << endl;
    		else
    			cout << "NO" << endl;
    		
    	}
    	else
    		cout << "NO" << endl;
    	
    	return 0;
    } 
    

Example 4.12

求s=1!+2!+3!+...+10!

Example 4.13

对于给定的自然数n(n<20),在屏幕上输出仅由"*"构成的n行的直角三角形。

Example 4.14

百钱买百鸡问题。鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?

Example 4.15

求100~999中的水仙花数。若三位数ABC,ABC=A3+B3+C3,则称ABC为水仙花数。
例如153,13+23+33=1+125+27=153,则153是水仙花数。

Example 4.16

输出100~200中所有的素数

Example 4.17

输出所有形如aabb的四位完全平方数(即前两位数字相等,后两位数字也相等)。

Example 4.18

把一个合数分解成若干个质因数乘积的形式(即求质因数的过程)叫做分解质因数。
分解质因数(也称分解素因素)只针对合数。

Example 4.19

输入n,计算s=1!+2!+3!+...+n!的末6位(不含前导0)。n<=106,n!表示前n个正整数之积。

Exercise 4

  1. 求阶乘的和http://noi.openjudge.cn/ch0105/34/
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	long long int sum = 0,product=1;
    
    	cin >> n;
    	for (int i=1;i<=n;i++)
    	{
    		product = 1;
    		for(int j=1;j<=i;j++)
    		{
    			product = product * j;
    		}
    		sum = sum + product;
    	}
    	cout << sum << endl; 
    		
    	return 0;
    } 
    
  2. 求出e的值http://noi.openjudge.cn/ch0105/35/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n;
    	double e = 1,p;
    	cin >> n;
    	for (int i=1;i<=n;i++){
    		p = 1;
    		for(int j=1;j<=i;j++)
    		{
    			p = p * j;
    		}
    		e = e + 1/p;
    	}
    	
    	printf("%.10f",e);	
    }
    
  3. 计算多项式的值http://noi.openjudge.cn/ch0105/36/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
    	
    	long int n;
    	double x,f = 1,p;
    	cin >> x >> n ;
    	f = f + x;
    	for (int i=2;i<=n;i++){
    		f = f + pow(x,i);
    	}
    	
    	printf("%.2f",f);	
    }
    
  4. 雇佣兵http://noi.openjudge.cn/ch0105/37/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int M,N,X;
    	
    	cin >> M >> N >> X ;
    	
    	while(N <=M && X * N >=M)
    	{
    		if (M%N==0)
    		{
    			X = X - M / N;
    		}
    		else
    		{
    			X = X - M / N -1;
    		}
    		N = N + M/N;
    
    	}
    	printf("%d",N);
    	
    	return 0;	
    }
    
  5. 计算多项式的导函数http://noi.openjudge.cn/ch0105/38/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n,c[100];
    	
    	cin >> n ;
    
    	for(int i=n;i>=0;i--){
    		cin >> c[i];
    		c[i] = c[i] * i;
    	}
    	
    	cout << c[n];
    	for(int i=n-1;i>0;i--)
    	{
    		cout << " " << c[i];
    	}
    	cout << endl;
    	
    	return 0;	
    }
    
  6. 与7无关的数http://noi.openjudge.cn/ch0105/39/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n,sum=0,a;
    	
    	cin >> n ;
    	for(int i=1;i<=n;i++){
    		if(i%7!=0){
    			a = i;
    			while(a>0)
    			{
    				if (a % 10 == 7)
    				{
    					break;					
    				}
    				a = a / 10;	
    			}
    			if (a ==0){
    				sum = sum + i * i;
    			}
    		}
    	}
    	cout << sum << endl;
    	
    	return 0;	
    }
    
  7. 数1的个数http://noi.openjudge.cn/ch0105/40/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int n,sum=0,a;
    	
    	cin >> n ;
    	for(int i=1;i<=n;i++){
    		a = i;
    		while(a>0)
    		{
    			if (a % 10 == 1)
    			{
    				sum = sum + 1;
    			}
    			a = a / 10;	
    		}
    	}
    	cout << sum << endl;
    	
    	return 0;	
    }
    
  8. 数字统计http://noi.openjudge.cn/ch0105/41/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int L,R,sum=0,a;
    	
    	cin >> L >> R ;
    	for(int i=L;i<=R;i++){
    		a = i;
    		while(a>0)
    		{
    			if (a % 10 == 2)
    			{
    				sum = sum + 1;
    			}
    			a = a / 10;	
    		}
    	}
    	cout << sum << endl;
    	
    	return 0;	
    }
    
  9. 画矩形http://noi.openjudge.cn/ch0105/42/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	int h,w,fill;
    	char c;
    	
    	cin >> h >> w >> c >> fill ;
    	
    	for (int i=0;i<w;i++) cout << c;
    	cout << endl;
    	
    	for (int i=0;i<h-2;i++){
    		cout << c;
    		for(int j=0;j<w-2;j++)
    			if (fill==0) 
    				cout << " ";
    			else
    				cout << c;
    		cout << c << endl;
    	}
    	for (int i=0;i<w;i++) cout << c;
    	cout << endl;
    	
    	
    	return 0;	
    }
    
  10. 质因数分解http://noi.openjudge.cn/ch0105/43/
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	long long int n,j,t;
    	
    	cin >> n ;
    	for (int i=2;i*i<n;i++){
    		if(n % i ==0 ){
    			cout << n / i << endl;
    			break;
    		}
    	}
    	
    	
    	return 0;	
    }
    
  11. 第n小的质数http://noi.openjudge.cn/ch0105/44/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
    	long n,c=2,i=3,j;
    	bool flag = false;
    	cin >> n ;
    	if (n==1)
    		i = 2;
    	else
    	while(c<n)
    	{
    		i = i + 2;
    		flag = true;
    		for(j=2;j<=sqrt(i);j++)
    			if (i % j==0)
    			{
    				flag = false;
    				break;
    			}
    		if(flag)
    		{
    			c = c + 1;
    		}
    	}
    	cout << i << endl;
    	
    	return 0;	
    }
    
  12. 金币http://noi.openjudge.cn/ch0105/45/
    #include <iostream>
    using namespace std;
    
    int main(){
    	long n,coin=1,sum=0;
    
    	cin >> n ;
    	
    	int count = 1;
    	while(count <= n)
    	{
    		for(int j=1;j<=coin&&count <=n;j++){
    			sum = sum + coin;
    			count = count + 1;
    		}
    		coin = coin + 1;
    	}
    	cout << sum << endl;
    	
    	return 0;	
    }
    
  13. 不定方程求解http://noi.openjudge.cn/math/7650/
    #include <iostream>
    using namespace std;
    
    int main(){
    	int a ,b ,c ,ans=0;
    	cin >> a >> b >> c;
    	
    	for(int i=0;i<=c/a;i++)
    		for(int j=0;j<=c/b;j++)
    			if(a*i+b*j==c)
    				ans = ans + 1;
    	
    	cout << ans << endl;
    	
    	return 0;
    }
    

参考文献

  1. 董永建,信息学奥数一本通(C++)第五版。
  2. http://noi.openjudge.cn

辽师张大为@https://daweizh.github.io/csp/