#255. No.5 | 程序题_嵌套循环

No.5 | 程序题_嵌套循环

当前没有测试数据。

No.5 | 程序题_嵌套循环

P24三位回文数

#include<iostream>
using namespace std;
int main() {
	int m,n, cnt=0; cin>>m>>n;
	for(int i=m; i<=n; i++){
		// i = 123 = abc
		int a = i/100;
		int c = i%10;
		if(a==c) {
			cout<<i<<endl;
			cnt ++;
		}
	} 
	cout<<cnt;
	return 0;
}

多位回文数的情况

#include<iostream>
using namespace std;
int main() {
	int m,n, cnt=0; cin>>m>>n;
	for(int i=m; i<=n; i++){
		int t = i, b=0;
		while(t){
			b =b*10 +t%10;
			t /= 10;
		}
		if(i==b){
			cout<<i<<endl;
			cnt++;
		}
	} 
	cout<<cnt;
	return 0;
}

P25求素数

#include<iostream>
using namespace std;
int main() {
	int n, cnt=0; cin>>n;
	for(int i=2; i<n; i++){
		// 判断 i是不是素数
//		[2, i-1]  --> [2, sqrt(i)] == [2, i/j] 
		bool f=1; // 假定 i 是素数 
		for(int j=2; j<=i/j; j++)
			if(i%j==0){ // i 不是素数 
				f = 0; break;// 退出当前一层循环 
			}
		if(f){
			cout<<i; cnt++;
			if(cnt%5==0) cout<<endl;
			else cout<<" ";	
		}
	}
	return 0;
}

P26 九九乘法表

#include<cstdio>
#include<iostream>
using namespace std;
int main() {
	int k; scanf("%d", &k);
	int t=1, ans=0;
	while(k){
		for(int i=1; i<=t && k; i++) 
			ans += t, k--;
		t ++;
	}
	printf("%d", ans);
	return 0;
}

P27 骑士的金币/coin

#include<iostream>
using namespace std;
int main() {
	int k; scanf("%d", &k);
	int t=1, ans=0;
	while(k){
		for(int i=1; i<=t && k; i++) 
			ans += t, k--;
		t ++;
	}
	printf("%d", ans);
	return 0;
}

P30 百钱买百鸡

#include<bits/stdc++.h>
using namespace std;
int main() {
	for(int a=0; a<=20; a++){
		for(int b=0; b<=33; b++){ 
			for(int c=0; c<=100; c+=3){ // 小鸡的数量一定是 3的倍数 
				if(a+b+c==100 && 5*a+3*b+c/3==100){
					cout<<a<<" "<<b<<" "<<c<<endl;
				}
			}
		}
	}
	return 0;
}

P296 桐桐去购物

P34 质因子