The code example is as follows:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p=0;
int i, j, k;
for (i = 1; i < 5; i++)
{
for (j = 1; j < 5; j++)
{
for (k = 1; k < 5; k++)
{
if (i != j && i != k && j != k)
{
printf("%d%d%d\n", i, j, k);
p++;
}
}
}
}
printf("一共有:%d组\n", p);
return 0;
}
The result of the operation is as follows:
(Code upgrade) Expand the input of four one-digit integers, and output the three-digit numbers formed
The code example is as follows:
#include <stdio.h>
int main()
{
int a[4];
int i, j, k, p = 0; //这里p要进行初始化
printf("请输入四个一位数,用空格隔开:");
printf("\n");
for (i = 0; i < 4; i++)
scanf("%d", &a[i]);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
{
if (i != j && i != k && j != k && a[i] != 0)
{
printf("%d%d%d\n", a[i], a[j], a[k]);
p++;
}
}
}
}
printf("\n");
printf("三位数总数为%d", p);
return 0;
}
The result of the operation is as follows:
(Advanced version) Think about how many three-digit numbers can be formed by 5 integers, and output
n integer output three digits
The code example is as follows (5 integers):
#include<stdio.h>
int main()
{
int i, j, k, q;
for (i = 1; i <= 5; i++)
{ //使用for四重循环
for (j = 1; j <= 5; j++)
{
for (k = 1; k <= 5; k++)
{
for (q = 1; q <= 5; q++)
{
if (i != j & i != k & i != q & j != k & j != q & k != q)
{ //进行数字不重复的判断
printf("%d%d%d\t", i, j, k, q); //输出,使用制表符,输出整齐
}
}
}
printf("\n"); //在第二个for循环回车,输出整齐
}
}
}
The result of the operation is as follows:
0-n (n<=9) integers, output three digits (implemented by pointer method)
The code example is as follows:
#include<stdio.h>
int n, m;
int count = 0;
int arr[3];
int num = 0;
void free(void* ptr);
void* malloc(size_t size);//C语言malloc()函数:动态分配内存空间
void fun(int arr1[], int n)
{
if (num == 2)
{
for (int i = 0; i < n; i++)
{
printf("%-4d", arr[0] * 100 + arr[1] * 10 + arr1[i]);
count++;
if (count % 10 == 0)
printf("\n");
}
}
else
{
for (int i = 0; i < n; i++)
{
arr[num] = arr1[i];
int* p = (int*)malloc((n - 1) * sizeof(int));
int k = 0;
for (int j = 0; j < n; j++)
{
if (j != i)
p[k++] = arr1[j];
}
num++;
fun(p, n - 1);
free(p);
num--;
}
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
if (n < 0 || m <= 0 || m + n>10)
continue;
count = 0;
num = 0;
int* q = (int*)malloc(n * sizeof(int));
for (int i = m; i <= n + m - 1; i++)
{
q[i - m] = i;
}
fun(q, n);
free(q);
if (count % 10 != 0)
printf("\n");
printf("Total of %d numbers.\n", count);
}
return 0;
}
The result of the operation is as follows:
Editor's note: The above-mentioned various methods of writing code for this topic are welcome to collect, learn from and forward;
The above code is for reference only, if you have any questions, you are welcome to criticize and correct in the message area;
All rights reserved, reprints must be investigated, any similarity is purely coincidental, please indicate the source for reprinting.
By CRH380AJ2808 2022.06.08
—————————————————
Copyright statement: This article is an original blogger article, following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting .
Link to this article: https://blog.csdn.net/JH13thpig/article/details/125183968
—————————————————
(37 messages) The c language realizes the accumulation from 1 to n, The cumulative calculation of 1+2+3+.....+n is realized by different methods, and the output format is 1+2+3+....+(n-1)+n_CRH380AJ2808's Blog-CSDN Blog_c Language 1~n cumulative summation