1.求和
#include <stdio.h>//所写程序均为参考答案
#include <stdlib.h>
int main() {
int a,b;
scanf("%d %d",&a,&b);
int sum= a+b;
printf("%d",sum);
return 0;
}
2.存钱啦
#include <stdio.h>
int main(){
float rate, capital, deposit;
int n;
scanf("%f %d %f", &rate, &n, &capital);
while (n){
deposit = capital * (1 + rate);
capital = deposit;
n = n - 1;
}
printf("%.1f", deposit);
return 0;
}
3.试试math
#include <stdio.h>
#include <math.h>
int main(){
float input, output;
scanf("%f", &input);
if (input < 0){
printf("ERROR");
return 0;
}
output = sqrt(input);
printf("%.1f", output);
return 0;
}
4.【手工劳动】
#include <stdio.h>
int main(){
printf("********* *** * * \n");
printf("* * * * *** * * \n");
printf("* * * * ***** * * \n");
printf("* * * * * * * \n");
printf("* * * * * * *\n");
printf("* * * * * * * \n");
printf("* * * * * * * \n");
printf("* * * * * * * \n");
printf("********* *** * * \n");
return 0;
}
5.Validating triangles(验证三角形)
#include<stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a + b > c && a + c >b && b + c >a && a > 0 && b > 0 && c > 0) {
printf("valid");
} else {
printf("invalid");
}
return 0;
}
6.Using the && and || operator
#include<stdio.h>
#include <string.h>
int main() {
int a;
char result1[10] = "0", result2[10] = "0", result3[10] = "0";
scanf("%d", &a);
if(a % 5 == 0 && a % 6 == 0) {
strcpy(result1, "true");
} else {
strcpy(result1, "false");
}
if(a % 5 == 0 || a % 6 == 0) {
strcpy(result2, "true");
} else {
strcpy(result2, "false");
}
if(!strcmp(result1, "false") && !strcmp(result2, "true")) {
strcpy(result3, "true");
} else {
strcpy(result3, "false");
}
printf("Is %d divisible by 5 and 6? %s\n", a, result1);
printf("Is %d divisible by 5 or 6? %s\n", a, result2);
printf("Is %d divisible by 5 or 6, but not both? %s", a, result3);
return 0;
}
7.[Loop]Buy Chicken
#include<stdio.h>
int main() {
int a, b, c;
int money, number, flag;
scanf("%d %d", &money, &number);
flag = 0;
for (a = money/5; a >= 0; a--)
for (b = money/3; b >= 0; b--)
for (c = money; c >= 0; c--) {
if (a + b + c*3 == number && a*5 + b*3 + c == money) {
flag++;
printf("%d %d %d\n", a, b, c*3);
}
}
if ( flag == 0 )
printf("no answer\n");
return 0;
}
8.One Employee’s Wages
#include <stdio.h>
int main(){
int work_hour;
scanf("%d", &work_hour);
if (work_hour < 40) {
printf("%.2f\n", work_hour * 24.75);
} else {
printf("%.2f\n", 40 * 24.75 + (work_hour - 40) * 1.5 * 24.75);
}
return 0;
}
9. Computing the greatest common divisior
#include <stdio.h>
int main() {
int a, b, remain, temp;
scanf("%d%d", &a, &b);
if (a < b) { /*make sure that a >= b*/
temp = a;
a = b;
b = temp;
}
while (a % b != 0) {
remain = a % b;
a = b;
b = remain;
}
printf("%d\n", b);
return 0;
}
10.Finding the highest score
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
int best_value = 0;
int temp;
for (i = 0; i < n; i++) {
scanf("%d", &temp);
if (temp > best_value) {
best_value = temp;
}
}
printf("%d\n", best_value);
return 0;
}
11.倒序输出数组元素
#include<stdio.h>
int main(){
int N;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
int len;
scanf("%d", &len);
if(len == 0)
printf("empty\n");
else{
int array[len];
for(int j = len; j > 0; j--)
scanf("%d", &array[j-1]);
for(int k = 0; k < len; k++)
{
if(k < len - 1)
printf("%d,",array[k]);
else
printf("%d\n",array[k]);
}
}
}
return 0;
}
12.数组不同元素个数
#include <stdio.h>
int main(){
int t, n, num, flag;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
int array[n];
num = 0;
for(int i = 0; i < n; i++){
scanf("%d", &array[i]);
}
for(int i = 0; i < n; i++){
flag = 1;
for(int j = 0; j < i; j++){
if(array[i] == array[j]){
flag = 0;
break;
}
}
if(flag == 1) num++;
}
printf("%d\n", num);
}
return 0;
}
13.求一个字符串的长度
#include<stdio.h>
#include<stdlib.h>
int main()
{
int len, N, i, j;
char str[100];
scanf("%d", &N);
for(i=0; i<N; i++)
{
scanf("%s",str); // 当接收到空格或回车,字符串接收停止
j = 0;
len = 0;
while(str[j] != '\0')
{
len++;
j++;
}
printf("%d\n", len);
}
}
14.签到
#include <stdio.h>
int main()
{
char name[30];
char id[10];
scanf("%s",id);
scanf("%s",name);
printf("%s_%s love the C programming language!",id,name);
return 0;
}
15.数组的最大值与最小值
#include<stdio.h>
#define ARRAY_LEN 100
int main() {
int n, a[ARRAY_LEN];
int max, min;
scanf("%d", &n);
if (n < 0) return 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (i == 0) {
max = a[i];
min = a[i];
} else {
if (a[i] < min)
min = a[i];
if (a[i] > max)
max = a[i];
}
}
printf("the maximum value is: %d\n", max);
printf("the minimum value is: %d\n", min);
return 0;
}
16.查找第n个素数
#include <stdio.h>
#include <math.h>
long long primes[1000];
int main(void){
long long i = 2, count = 0;
while(1){
long long j, is_prime=1;
for(j = 2; j <= sqrt(i); j++){
if(i % j == 0){
is_prime = 0;
break;
}
}
if(is_prime){
primes[count++] = i;
if(count == 1000){
break;
}
}
i++;
}
int t, n;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
printf("%lld\n", primes[n - 1]);
}
return 0;
}
17.级数求和
#include <stdio.h>
long long fact(int);
int main()
{
long long sum=0;
int i = 1, n = 0;
scanf("%d",&n);
for(i=1;i<=n;i++){
long long a = fact(i);
sum=sum+a/i;
}
printf("The sum of the series is: %lld",sum);
return 0;
}
long long fact(int n)
{
int num=0,f=1;
while(num<=n-1)
{
f =f+f*num;
num++;
}
return f;
}
18.校门外的树
#include<stdio.h>
int main() {
int l_num, m_num, i = 0, j = 0;
scanf("%d %d", &l_num, &m_num);
int l[l_num+1];
for (i = 0; i < l_num+1; i++) {
l[i] = 1;
}
int min = 0, max = 0;
while(m_num--) {
scanf("%d %d", &min, &max);
for (j = min; j <= max; j++) {
l[j] = 0;
}
}
int count = 0;
for(i = 0; i < l_num+1; i++) {
count +=l[i];
}
printf("%d", count);
return 0;
}
19.数组行列互换
#include <stdio.h>
int main() {
int sz[3][3] = {0};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &sz[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sz[j][i]);
}
}
return 0;
}
20.分数加法
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 10500;
int gcd(int a, int b){return b ? gcd(b, a % b) : a;}
int tcs;
int main()
{
for(scanf("%d", &tcs); tcs--;)
{
int a, b, c, d;
if(scanf("%d%d%d%d", &a, &b, &c, &d) == EOF) return 0;
int e = a * d + b * c;
int f = b * d;
int g = gcd(e, f);
e /= g; f /= g;
printf("%d %d\n", e, f);
}
return 0;
}
21.水仙花数
#include <stdio.h>
int main()
{
int i,j,k,n;
for(n=100;n<1000;n++)
{i=n/100;
j=n/10%10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%d\n",n);
}
}
}
22.回文数
#include<stdio.h>
#include<string.h>
int main()
{
int i=0,j=0,count=0;
char num[1001];
scanf("%s",num);
j=strlen(num)-1;
for(i=0;i<(strlen(num)+1)/2,j>(strlen(num)+1)/2-1;i++,j--)
{
if(num[i]!=num[j])
{
count=1;
break;
}
}
if(count==1)printf("No\n");
else printf("Yes\n");
return 0;
}
23.小游戏
//当N>2时,小红拿完第一次后,剩下的石头可以看作是排列在一行,小明从中间拿1或2颗使得剩下的石头成对称的两边,接下来小红怎么拿小明就拿对称的,那么小明必定获得胜利
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n==1||n==2) printf("1");
else printf("2");
return 0;
}
24.数组排序
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
#define NUMS 10
int main()
{
int i = 0, j = 0, num[NUMS] = {0};
for (i = 0; i < NUMS; i++)
scanf("%d", &num[i]);
for (i = 0; i < NUMS; i++) {
for (j = 1; j < NUMS - i; j++) {
if (num[j] < num[j-1])
swap(&num[j], &num[j-1]);
}
}
for (i = 0; i < NUMS-1; i++) {
printf("%d ", num[i]);
}
printf("%d ", num[NUMS-1]);
return 0;
}
25.计算天数
#include <stdio.h>
int is_leap(int year) {
if (year % 100 == 0)
return (year % 400 == 0);
else
return (year % 4 == 0);
}
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int deal(int year, int month, int day) {
int re = day;
if (is_leap(year))
days[1] = 29;
else
days[1] = 28;
int i = 0;
for (i = 0; i < month - 1; i++)
re += days[i];
return re;
}
int main() {
int y, m, d;
scanf("%d%d%d", &y, &m, &d);
printf("%d\n", deal(y, m, d));
return 0;
}
26.移动小球
#include <stdio.h>
#define MAXN 500100
int left[MAXN] = {0};
int right[MAXN] = {0};
void link(int x, int y);
int main() {
int t;
int n, m;
int x, y;
int side;
int i;
int ball;
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
for (i = 0; i <= n; i++) {
link(i, i + 1);
}
while (m--) {
scanf("%d %d %d", &side, &x, &y);
link(left[x], right[x]);
if (side == 1) {
link(left[y], x);
link(x, y);
}
else {
link(x, right[y]);
link(y, x);
}
}
for (ball = right[0]; ball != n + 1; ball = right[ball]) {
printf("%d ", ball);
}
printf("\n");
}
return 0;
}
void link(int x, int y) {
right[x] = y;
left[y] = x;
}
27.最终胜者
#include <stdio.h>
int main(void){
int t;
scanf("%d", &t);
int person[100];
while(t--){
int n, m, i, count, rest;
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++){
person[i] = 1;
}
count = 0;
i = 0;
rest = n;
while(1){
if(person[i] == 0){
i = (i + 1) % n;
continue;
}
count++;
if(rest == 1){
printf("%d\n", i + 1);
break;
}
if(count % m == 0){
person[i] = 0;
rest -= 1;
}
i = (i + 1) % n;
}
}
return 0;
}
28.纸牌游戏
#include<stdio.h>
int main() {
int count;
scanf("%d", &count);
int num = 0;
int i = 0, j = 0;
for(i = 0; i < count; i++) {
scanf("%d", &num);
int s[num];
for (j = 0; j < num; j++) {
s[j] = j+1;
}
int front = 0, rear = 0;
int n = num;
while(n--) {
front %= num;
printf("%d ", s[front++]);
rear %= num;
front %= num;
s[rear] = s[front];
rear++;
front++;
}
printf("\n");
}
return 0;
}
29.弓形矩阵
//fun.c
#include "func.h"
#include <stdio.h>
void printMatrix(int m, int n) {
int start = 0, increase = 1;
for (int i = 1; i <= m; ++i) {
if (i % 2 != 0) {
start = n * (i-1) + 1;
increase = 1;
} else {
start = n * i;
increase = -1;
}
for (int j = 0; j < n; ++j) {
printf("%3d", start + j * increase);
}
printf("\n");
}
}
//fun.h
#ifndef FUNC_H
#define FUNC_H
void printMatrix(int m, int n);
#endif
//main.c
#include <stdio.h>
#include <math.h>
#include "func.h"
int main() {
int m, n;
scanf("%d%d", &m ,&n);
printMatrix(m, n);
return 0;
}
30.平方数之和
#include <stdio.h>
#include <math.h>
int main(void){
int num, i, flag;
scanf("%d", &num);
flag = 0;
for(i = 0; i <= sqrt(num/2); i++){
int t;
t = sqrt(num - i * i);
if(t * t == num - i * i){
flag = 1;
break;
}
}
if(flag){
printf("true\n");
}else{
printf("false\n");
}
}
31.找零
#include <stdio.h>
int main(){
int t, n, bill, num_5, num_10, flag;
scanf("%d", &t);
while(t--){
num_5 = num_10 = 0;
flag = 1;
scanf("%d", &n);
while(n--){
scanf("%d", &bill);
if(bill == 5) num_5++;
else if(bill == 10){
num_5--;
num_10++;
}
else{
if(num_10 > 0){
num_10--;
num_5--;
}
else num_5 -= 3;
}
if(num_5 < 0){
flag = 0;
}
}
if(flag == 1) printf("True\n");
else printf("False\n");
}
return 0;
}
32.简单递归
#include <stdio.h>
void f(int n, int star, int end, int mid);
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
f(n, 1, 3, 2);
}
return 0;
}
void f(int n, int star, int end, int mid) {
if (n == 1) {
printf("move disk %d from peg %d to peg %d\n", n, star, end);
} else {
f(n-1, star, mid, end);
printf("move disk %d from peg %d to peg %d\n", n, star, end);
f(n-1, mid, end, star);
}
}
33.过河
#include<stdio.h>
int jump(int, int);
int main()
{
int s, y;
scanf("%d%d", &s, &y);
printf("%d\n", jump(s, y));
return 0;
}
int jump(int s, int y)
{
if (s == 0)
return y+1;
else
return 2*jump(s-1, y);
}
34.买邮票
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int test, t, n, m, i, j, k, ans;
scanf("%d%d", &m, &n);
n <<= 1;
ans = 0;
for (j = 0; j * j <= n; j++) { }
for (i = j - 1; i > 0; i--)
if (n % i == 0) {
j = n / i;
if (j - i - 1 >= 0 && (j - i - 1) % 2 == 0 && (j - i - 1) / 2 + i <= m)
printf("[%d,%d]\n", (j - i - 1) / 2 + 1, (j - i - 1) / 2 + i);
}
}
35.求星期几
#include <stdio.h>
#include <math.h>
int main(void){
long long year, month, day;
scanf("%lld%lld%lld", &year, &month, &day);
long long days=0;
days += (year - 1) * 365;
days += (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
int _month;
for(_month = 1; _month < month; _month++){
if(_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12){
days += 31;
}else if(_month == 2){
if((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)){
days += 29;
}else{
days += 28;
}
}else{
days += 30;
}
}
days += day;
if(days % 7){
printf("%lld\n", days % 7);
} else{
printf("7\n");
}
}
36.加密
#include<stdio.h>
#include <string.h>
int main ()
{
char s[100];
int t;
scanf("%d", &t);
while(t--){
scanf("%s", s);
int len = strlen(s), i;
for(i = 0; i < len; i++){
int diff;
if(s[i] == '_'){
diff = 95;
}else if(s[i] == ';'){
diff = 59;
}else if(s[i] >= 'A' && s[i] <='Z'){
diff = s[i] - 'A';
}else if(s[i] >= 'a' && s[i] <='z'){
diff = 26 + s[i] - 'a';
}
char base[3] = {'0', '0', '0'};
int p = 0;
while(diff){
if(diff % 16 > 9){
base[p] = 'A' + (diff % 16) - 10;
}else{
base[p] = '0' + diff % 16;
}
p++;
diff /= 16;
}
int j = 2;
while(j--){
printf("%c", base[j]);
}
}
printf("\n");
}
return 0;
}
37.下楼
#include <stdio.h>
void Try(int stairNum, int maxStep, int* stepNum)
{
int step;
if (stairNum == 0)
*stepNum = *stepNum + 1;
else{
for ( step = 1; step <= maxStep; step++ ){
if (stairNum >= step)
Try(stairNum - step, maxStep, stepNum);
else
break;
}
}
}
int main( )
{
int h = 0;
int s = 0;
int num = 0;
scanf("%d%d", &h, &s);
Try(h, s, &num);
printf("%d\n", num);
return 0;
}
38.解密
#include<stdio.h>
#include <string.h>
int main ()
{
char s[100];
int t;
scanf("%d", &t);
while(t--){
scanf("%s", s);
int len = strlen(s), i;
int num = 0;
for(i = 1; i <= len; i++){
if (s[i - 1] >= 'A'){
num = num * 16 + s[i - 1] - 'A' + 10;
}else{
num = num * 16 + s[i - 1] - '0';
}
if( i % 2 == 0){
if(num == 95){
printf("%c", '_');
}else if(num == 59){
printf("%c", ';');
}else if(num < 26){
printf("%c", 'A' + num);
}else{
printf("%c", 'a' + num - 26);
}
num = 0;
}
}
printf("\n");
}
return 0;
}
39.3n+1数链问题
#include <stdio.h>
int get_len(int x) {
int num = 1;
while (x != 1) {
x = x % 2 ? 3 * x + 1 : (x >> 1);
num++;
}
return num;
}
int main() {
int i, start, end, max = 0, tmp;
scanf("%d%d", &start, &end);
for (i = start; i <= end; i++) {
tmp = get_len(i);
max = tmp > max ? tmp : max;
}
printf("%d\n", max);
return 0;
}
40.简单字符串
#include <stdio.h>
#include <string.h>
int main() {
char str[1001] = {0};
while (scanf("%s", str) != EOF) {
int i = 0, k1 = 0, k2 = 0;
char str1[501] = {0}, str2[501] = {0};
for (i = 0; i < strlen(str); i++) {
if (i % 2) str2[k2++] = str[i];
else str1[k1++] = str[i];
}
printf("%s,%s\n", str1, str2);
}
return 0;
}
41.全排列
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 20
int a[MAX_SIZE];
bool select[MAX_SIZE];
int size;
void print(int index) {
if (index == size) {
for (int i = 0; i < size-1; ++i) {
printf("%d ", a[i]);
}
printf("%d\n", a[size-1]);
return ;
}
for (int i = 0; i < size; ++i) {
if (select[i] == false) {
select[i] = true;
a[index] = i+1;
print(index+1);
select[i] = false;
}
}
}
int main() {
scanf("%d", &size);
print(0);
return 0;
}
42.Counting single digits
#include <stdio.h>
int main()
{
int n = 0, m = 0, j = 0, i = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
int mark[10] = {0}, k = 0;
scanf("%d", &m);
for (j = 0; j < m; j++) {
int temp = 0;
scanf("%d", &temp);
mark[temp]++;
}
for (j = 0; j < 10; j++) {
if (mark[j] != 0)
printf("%d %d\n", j, mark[j]);
}
}
return 0;
}
43.Ant
#include<stdio.h>
int main() {
int n, l, x, sum = 0, i;
char s[11];
scanf("%d%d", &n, &l);
for (i = 1; i <= n; i++) {
scanf("%s%d", s, &x);
if (s[0] == 'R') x = l - x;
if (x > sum) sum = x;
}
printf("%d\n", sum);
return 0;
}
44. 01个数相同的子串数
#include <stdio.h>
#include <string.h>
int main() {
char str[1000100];
int dp[2000100] = {0};
int idx=1000010;
scanf("%s", str);
long long sum = 0;
int lens = strlen(str);
int i;
for (i = 0; i < lens; i++) {
dp[idx]++;
if (str[i] == '0') {
idx++;
} else {
idx--;
}
sum += dp[idx];
}
printf("%lld\n", sum);
return 0;
}
45. 复读机
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
char str[1000],st[1000];
int arr[1000],j=0,k=0,sum=0,op=0,op1;
scanf("%s",str);
for(int i=0;i<strlen(str);i++){
if(str[i]>=48&&str[i]<=57&&i!=strlen(str)-1){
arr[j]=str[i]-48;
j++;
}
else if(i==strlen(str)-1&&str[i]>=48&&str[i]<=57){
arr[j]=str[i]-48;
j++;
sum=0;
k=j;
j=0;
if(k>0){
op1=op-1;
for(int x=0;x<k;x++){
sum=sum+arr[x]*pow(10,k-x-1);
}
for(int y=0;y<sum%6;y++){
st[op]=st[op1];
op++;
}
}
if(i!=strlen(str)-1){
st[op]=str[i];
op++;
}
}
else{
sum=0;
k=j;
j=0;
if(k>0){
op1=op-1;
for(int x=0;x<k;x++){
sum=sum+arr[x]*pow(10,k-x-1);
}
for(int y=0;y<sum%6;y++){
st[op]=st[op1];
op++;
}
}
st[op]=str[i];
op++;
}
}
st[op]='\0';
printf("%s",st);
return 0;
}
46.Second Max
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
/*冒泡排序*/
int n = 0, i = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
int m = 0, j = 0, num[10001] = {0};
scanf("%d", &m);
for (j = 0; j < m; j++) {
scanf("%d", &num[j]);
}
int s = 0, t = 0;
for (s = 1; s < m; s++) {
if (num[s] < num[s-1])
swap(&num[s], &num[s-1]);
}
for (s = 1; s < m - 1; s++) {
if (num[s] < num[s-1])
swap(&num[s], &num[s-1]);
}
printf("%d\n", num[m-2]);
}
return 0;
}
47.递归求一个数的阶乘
//bcd.c
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
//bcd.h
long int multiplyNumbers(int n);
//main.c
#include <stdio.h>
#include "bcd.h"
int main()
{
int n;
scanf("%d", &n);
printf("%ld", multiplyNumbers(n));
return 0;
}
48.字符串反向拼接
#include <stdio.h>
#include <string.h>
int main(void){
char first[43], second[43];
int t;
scanf("%d", &t);
while(t--){
scanf("%s", first);
scanf("%s", second);
int len = strlen(second);
while(len--){
printf("%c", second[len]);
}
len = strlen(first);
while(len--){
printf("%c", first[len]);
}
printf("\n");
}
}
49.按字典顺序排列
#include<stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d", &n);
char str[n][101];
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(str[i], str[j]) > 0) {
char a[100];
strcpy(a, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], a);
}
}
}
for (int i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
50.有序链表
//fun.c
#include "fun.h"
void insert(struct Node** head, int num) {
struct Node* pre = NULL, *cur = *head;
struct Node* t = (struct Node*) malloc(sizeof(struct Node));
t->next = NULL;
t->value = num;
while (cur != NULL && num >= cur->value) {
pre = cur;
cur = cur->next;
}
if (pre == NULL) {
t->next = *head;
*head = t;
}
else {
pre->next = t;
t->next = cur;
}
pre = NULL;
cur = NULL;
}
void print_linklist(struct Node* head) {
struct Node* p = head;
printf("%d", p->value);
p = head->next;
while (p != NULL) {
printf(" %d", p->value);
p = p->next;
}
printf("\n");
}
void delete_linklist(struct Node* head) {
struct Node* p = NULL;
while (head != NULL) {
p = head;
head = head->next;
free(p);
}
p = NULL;
}
//fun.h
#include <stdlib.h>
struct Node {
struct Node* next;
int value;
};
void insert(struct Node** head, int num);
void print_linklist(struct Node* head);
void delete_linklist(struct Node* head);
//main.c
#include <stdio.h>
#include "fun.h"
int main(void) {
int n, num;
scanf("%d", &n);
struct Node* head = NULL;
while (n--) {
scanf("%d", &num);
insert(&head, num);
}
print_linklist(head);
delete_linklist(head);
}