1.给定条件的整数
算法
初始化a
输入一个不超过6的整数a(pta系统:不用核查)
// 组成整数,……*100+……*10+…… 系数取值范围{a,a+1,a+2,a+3}
每个位置的系数for循环,要三个数不相等
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
int i,j,k;
int num;
int count =0;
for (i = a; i < a + 4; i++)
{
for (j = a; j < a + 4; j++)
{
if (j != i)
{
for (k = a; k < a + 4; k++)
{
if (k != i)
{
if (k != j)
{
num = i * 100 + j * 10 + k;
printf("%d", num);
count++;
if (count%6 < 6)
{
printf(" ");
}
}
}
}
}
}
printf("\n");
}
return 0;
}
2.水仙花数
/* 153=1*100+5*10+3=1^3+5^3+3^3 */
/* 3-7位数 */
// i = rand() % 10 不行 得遍历
// for
#include <stdio.h>
#include <math.h>
int main()
{
int x;
scanf("%d", &x);
int i, j, k, l, m, n, p;
int ret = 0;
for (i = 0; i <= 9; i++)
{
for (j = 0; j <= 9; j++)
{
for (k = 0; k <= 9; k++)
{
for (l = 0; l <= 9; l++)
{
for (m = 0; m <= 9; m++)
{
for (n = 0; n <= 9; n++)
{
for (p = 0; p <= 9; p++)
{
int left = i * pow(10, 6) + j * pow(10, 5) + k * pow(10, 4) + l * pow(10, 3) + m * pow(10, 2) + n * pow(10, 1) + p * pow(10, 0);
int right = pow(i, 3) + pow(j, 3) + pow(k, 3) + pow(l, 3) + pow(m, 3) + pow(n, 3) + pow(p, 3);
int r = pow(10, x - 1);
int s = pow(10, x);
if (left / s <= 0 && left / r > 0 && left == right )
{
printf("%d\n", left);
ret = left;
}
}
}
}
}
}
}
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int n = 3;
int l = pow(10, n - 1);
int r= pow(10, n);
int i;
for (i = l; i < r; i++) {
int j = n - 1;
int deno;
int num = 1;
int u = i;
int sum = 0;
while (j >= 0) {
deno = pow(10, j);
num = u / deno;
u %= deno;
sum += pow(num, n);
j--;
}
if (i == sum) {
printf("%d\n", i);
}
}
return 0;
}
3.九九乘法表
/* 打印N*N的乘法表,格式按照一行1 2 3 递增排列
*/
#include <stdio.h>
int main()
{
int N=5;
//scanf("%d", &N);
int i, j;
for (j = 1; j <= N; j++)
{
int i = 1;
for(i=1;i<=j;i++)
{
printf("%d*%d=%d", i, j, i * j);
if (i < j)
{
if (i * j <= 9) {
printf(" ");
}
else {
printf(" ");
}
}
if (i == j)
{
printf("\n");
break;
}
}
}
return 0;
}
4.统计素数求和
#include <stdio.h>
int main()
{
int M = 10;
int N = 31;
//先判断大小,确定左右边界
int min, max;
if (M < N) {
min = M;
max = N;
}
else {
min = N;
max = M;
}
int num;
int count = 0;
int sum = 0;
//生成数num,挨个判断
for (num = min; num <= max; num++)
{
//判断素数
int isPrime = 1;
int i = 2;
for (i = 2; i < num; i++)
{
if (num % i == 0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{
// printf("%d是素数", num);
count++;
sum += num;
}
}
printf("%d %d", count, sum);
return 0;
}
5.猜数游戏
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x; //输入猜的数
int N;//猜的次数
scanf("%d %d", &x, &N);
//printf("%d %d\n", x, N);
//int randn = rand() % 100;
int randn = 40;
int count = 1;
while (count < N)
{
if (x < 0) {
printf("Game Over!\n");
break;
}
else if (x < randn)
{
printf("Too small!\n");
}
else if (x > randn)
{
printf("Too big!\n");
}
else
{
if (count == 1)
{
printf("Bingo!\n");
}
else if (count <= 3)
{
printf("Lucky you!\n");
}
else if (count > 3)
{
if (count <= N) {
printf("Good Guess!\n");
}
else {
printf("Game Over!\n");
}
}
break;
}
scanf("%d", &x);
//printf("%d\n", x);
count++;
}
return 0;
}
6.n项求和
#include <stdio.h>
int main()
{
//计算分母a
//计算分子b
//组合数字finger=b/a
//总和 sum+=finger
//格式:小数点后两位
double deno = 1;//分母初始化
double nume = 2;//分子初始化
double retd, retn;
double finger;
double sum = 0.0;
int count = 0;
int N;
scanf("%d", &N);
do {
finger = nume / deno;
sum += finger;
count++;
retd = deno;
retn = nume;
deno = retn;
nume = retd + retn;
} while (count < N);
printf("%.2f", sum);
return 0;
}
重要在数据类型定义那里,数据溢出,double的范围更大一些🤔
7.约分最简分式
#include <stdio.h>
int main()
{
//输入
int a, b;//分子a,分母b,前提都是正整数
scanf("%d/%d", &a, &b);
//找到min
int min;
if (a < b) {
min = a;
}
else {
min = b;
}
//求最大公约数
int i, ret;
for (i = 1; i <= min; i++)
{
if (min % i == 0)
{
ret = i;
}
}
//分别约分
//输出……/……
printf("%d/%d", a / ret, b / ret);
return 0;
}
8.念数字
#include <stdio.h>
#include <math.h>
int main()
{
int num;
scanf("%d", &num);
//解决负数先fu问题
if (num < 0) {
printf("fu ");
num = -num;
}
//确定数的位数
int j = 0;
int num2 = num;
do {
num2 /= 10;
j++;
} while (num2 != 0);
//循环提取单个数,并输出对应内容
int finger, b;
while (j > 0)
{
b = pow(10, j - 1);
finger = num / b;
num %= b;
//单个数的几种输出情况
switch (finger)
{
case 0: printf("ling"); break;
case 1: printf("yi"); break;
case 2: printf("er"); break;
case 3: printf("san"); break;
case 4: printf("si"); break;
case 5: printf("wu"); break;
case 6: printf("liu"); break;
case 7: printf("qi"); break;
case 8: printf("ba"); break;
case 9: printf("jiu"); break;
}
//空格,但行末没有
if (j!=1)
{
printf(" ");
}
j--;
}
return 0;
}
9.求a的连续和
#include <stdio.h>
#include <math.h>
int main()
{
int a, n;
scanf("%d %d", &a, &n);
int num = 0;
int sum = 0;
int count = 0;
while (count < n)
{
num += a*pow(10,count);
sum += num;
count++;
printf("num=%d,sum=%d,count=%d\n", num, sum, count);
}
printf("%d", sum);
return 0;
}