1、枚举n到m之间所有的质数
#define _crt_secure_no_warnings 1
#include<iostream>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
if (n > m)
{
swap(n, m);
}
for (int j = n; j <= m; j++)
{
if (j == 1)
{
continue;
}
bool is_prime = true;
for (int i = 2; i * i < j; i++)
{
if (j % i == 0)
{
is_prime = false;
cout << j << "not prime" << endl;
break;
}
}
if (is_prime)
{
cout << j << "prime" << endl;
}
}
return 0;
}
2、枚举字符
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(NULL));
char s[10];
for (int i = 0; i < 10; i++)
{
s[i] = (char)(65 + rand() % 26);
printf("%c", s[i]);
}
printf("\n");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 26; j++)
{
if (s[i] == (char)(65 + j))
{
cout << (char)(65 + j);
break;
}
}
}
return 0;
}
3、回文数字
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int n;
int digit[6];
bool judge(int x)
{
int m = 0, sum = 0;
while (x)
{
digit[m++] = x % 10;
sum += x % 10;
x /= 10;
}
if (sum != n)
{
return false;
}
for (int i = 0; i < m / 2; i++)
{
if (digit[i] != digit[m - 1 - i])
{
return false;
}
}
return true;
}
int main()
{
bool f = false;
cin >> n;
for (int i = 10000; i < 1000000; i++)
{
if (judge(i))
{
cout << i << endl;
f = true;
}
}
if (!f)
{
cout << -1 << endl;
}
return 0;
}
4、四叶玫瑰
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cmath>
using namespace std;
bool rose(int i)
{
int a = i / 1000, b = i / 100 % 10, c = i / 10 % 10, d = i % 10;
int ans = a * a * a * a + b * b * b * b + c * c * c * c + d * d * d * d;
if (ans == i)
{
return true;
}
else
{
return false;
}
}
int main()
{
int n;
cin >> n;
if (n < 1000 || n>9999)
{
cout << "error!";
}
else
{
for (int i = 1000; i <= n; i++)
{
if (rose(i))
{
cout << i << endl;
}
}
}
return 0;
}
5、生日蜡烛
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 200; i++)
{
int can = 0, j = i;
while (can < 236 && j <= 200)
{
can += j;
j++;
}
if (can == 236)
{
cout << i << " " << j - 1 << endl;
}
}
return 0;
}
6、奖券数目
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
bool judeg(int x)
{
while (x)
{
if (x % 10 == 4)
{
return true;
}
x /= 10;
}
return false;
}
int main()
{
int n, m, cnt = 0;
cin >> n >> m;
for (int i = n; i <= m; i++)
{
if (!judeg(i))
{
cnt++;
}
}
cout << cnt << endl;
return 0;
}
7、方程的解
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int a = 1; a * a <= n; a++)
{
for (int b = a + 1; a * a + b * b <= n; b++)
{
for (int c = b + 1; a * a + b * b + c * c <= n; c++)
{
if (a * a + b * b + c * c == n)
{
cout << a << " " << b << " " << c << endl;
}
}
}
}
return 0;
}
8、四平方和
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int N, d;
scanf("%d", &N);
for (int a = 0; a * a <= N; a++)
{
for (int b = a; a * a + b * b <= N; b++)
{
for (int c = b; a * a + b * b + c * c <= N; c++)
{
d = sqrt(N - a * a - b * b - c * c);
if (a * a + b * b + c * c + d * d == N)
{
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
}
}
}
return 0;
}
9、装饰效果
#define _CRT_SECURE_NO_WARNINGS 1
#include<cstdio>
int a[1005];
int main()
{
int n, sum, ans;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sum = 0;
ans = 0;
for (int i = 0; i < n; i++)
{
sum = 0;
for (int j = i; j < n; j++)
{
sum += a[j];
if (sum > ans)
{
ans = sum;
}
}
}
printf("%d\n", ans);
return 0;
}
10、双节棍
#define _CRT_SECURE_NO_WARNINGS 1
#include<cstdio>
int L[105];
int main()
{
int n, ans;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &L[i]);
}
ans = 10000;//求最小值可以先设定一个极大值
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)//j=i+1 是为了防止重复计算
{
if (L[i] >= L[j])
{
if (L[i] - L[j] < ans)
{
ans = L[i] - L[j];
}
}
else
{
if (L[j] - L[i] < ans)
{
ans = L[j] - L[i];
}
}
}
}
printf("%d\n", ans);
return 0;
}