当前位置:网站首页>[C language] question set of IX
[C language] question set of IX
2022-07-07 03:21:00 【InfoQ】
write in front
Question 41 → Sum of each digit of the number 『 recursive 』
sum += n % 10;// Find a bit sum = sum + n % 10
n = n / 10; // Erase a bit
Question 42 → Bubble sort
Question 43 → Study Group
Question 44 → Multiplication of positive integers
Question 45 → Array element exchange
Question 41 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int print(unsigned int n)
{
// The reason for this is n>9 Because n<10 No matter what number we input, it will be the final sum of the sum
if (n > 9)
{
// Take the mold and get a bit , Divide by ten .
return print(n / 10) + n % 10;
}
else
{
return n;
}
}
int main(void)
{
unsigned int num = 0;
printf(" Please enter a number →");
scanf("%d", &num);
int ret = print(num);
printf("ret = %d\n", ret);
return 0;
}
Question 42 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void Bubble_Sort(int arr[],int sz)
{
int i = 0;
// Determine the number of times to sort
for (i = 0; i < sz - 1; i++)
{
// Number of times per exchange , Number of first exchanges n-1, successively ......
int j = 0;
// Prevent invalid loops , That is, when our array is bubble sorted .
int flag = 1;
// Determine the number of exchanges .
for (j = 0; j < sz - 1 - i; j++)
{
// Compare the first number with the second number , Exchange when the first number is greater than the second number .
if (arr[j] > arr[j + 1])
{
// Create temporary variables , swapping !
int change;
change = arr[j];
arr[j] = arr[j+1];
arr[j+1] = change;
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
int main(void)
{
int i = 0;
// Reverse order of array
int arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
//sz Is the size of the total elements of the array
int sz = sizeof(arr) / sizeof(arr[0]);
// Call function
Bubble_Sort(arr,sz);
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Question 43 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int i = 0;
int j = 0;// Loop initialization subscript
int arr[3][5] = { 0 };// That's ok - subject , Column - Student .
int sum = 0; // The total score of the current subject
int average = 0; // Total average score
int v[3]; // Average number of subjects
printf(" Please enter the grades of students in each subject :\n");
for (i = 0; i < 3; i++)
{
printf("\n A subject counts into 5 Second grade \n");
if (i == 0)
printf(" Mathematics :");
if (i == 1)
printf(" Chinese subject :");
if (i == 2)
printf(" English subjects :");
for (j = 0; j < 5; j++)
{
scanf("%d", &arr[i][j]); // Enter each student's grade in each subject
sum += arr[i][j]; // Calculate the total score of the current subject (sum)
}
v[i] = sum / 5; // Average score of current account , Divide the total score by 5
sum = 0; // Clear the total score of the current subject 0
}
printf("\n Math scores = %d\n Chinese achievement = %d\n English scores = %d\n", v[0], v[1], v[2]);
average = v[0] + v[1] + v[2];
printf(" average : %d\n", average / 3);
return 0;
}
Question 44 の Code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int Multiply(int a, int b)
{
if (b == 0)
{
return 0;
}
return a + Multiply(a, b - 1);// a + a * b
}
int main(void)
{
int i = 0;
int j = 0;
printf(" Please enter two numbers :");
scanf("%d %d", &i, &j);
Multiply(i, j);
printf("%d\n", Multiply(i, j));
return 0;
}
Question 45 の Code
#include<stdio.h>
#define number 5
int main(void)
{
int i = 0;
int arr1[number] = { 1, 3, 5, 7, 9 };
int arr2[number] = { 2, 4, 6, 8, 10 };
for (i = 0; i < number; i++)
{
printf(" No exchange of previous values :arr1[%d] = %d\n",i + 1, arr1[i]);
}
for (i = 0; i < number; i++)
{
printf(" No exchange of previous values :arr2[%d] = %d\n", i + 1, arr2[i]);
}
printf("\n");
int sz = sizeof(arr1) / sizeof(arr1[0]);
for (i = 0; i < sz; i++)
{
int tmp;
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
printf("arr1[%d] = %-2d ", i + 1, arr1[i]);
printf("arr2[%d] = %-2d\n", i + 1, arr2[i]);
}
return 0;
}
边栏推荐
猜你喜欢
Decoration design enterprise website management system source code (including mobile source code)
Analysis of USB network card sending and receiving data
How-PIL-to-Tensor
[cpk-ra6m4 development board environment construction based on RT thread studio]
Starting from 1.5, build a micro Service Framework -- log tracking traceid
函数重入、函数重载、函数重写自己理解
Oauth2协议中如何对accessToken进行校验
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
Construction of knowledge map of mall commodities
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
随机推荐
Appx代码签名指南
Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
房费制——登录优化
Decoration design enterprise website management system source code (including mobile source code)
Starting from 1.5, build a micro Service Framework -- log tracking traceid
opencv环境的搭建,并打开一个本地PC摄像头。
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
制作(转换)ico图标
Jerry's transmitter crashed after the receiver shut down [chapter]
IDEA重启后无法创建Servlet文件的解决方案
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
Shangsilicon Valley JVM Chapter 1 class loading subsystem
input_delay
C language string sorting
源代码保密的意义和措施
How-PIL-to-Tensor
简单冒泡排序
centerX: 用中国特色社会主义的方式打开centernet
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》