信息学奥赛一本通第六页(1172——1184)C语言版

/*1172:求10000以内n的阶乘
#include <stdio.h>
#define N 100010
int main() {
    int n, k, i;
    int a[N] = {0};
    int lena = 1;
    scanf("%d", &n);
    a[0] = 1;
    for (k = 2; k <= n; k++) {
        for (i = 0; i < lena; i++) a[i] *= k;

        for (i = 0; i < lena; i++) {
            if (a[i] > 9) {
                a[i + 1] += a[i] / 10;
                a[i] %= 10;
                if (i == lena - 1) lena++;
            }
        }
    }
    for (i = lena - 1; i >= 0; i--) {
        printf("%d", a[i]);
    }
    printf("\n");
    return 0;
}
1173:阶乘和
#include <stdio.h>
#include <string.h>
#define N 210
void jia(int* ans, int* a, int lena, int* lenans) {
    int length = (*lenans > lena) ? *lenans : lena;
    int i;
    for (i = 0; i < length; i++) {
        ans[i] += a[i];
    }
    for (i = 0; i < length; i++) {
        if (ans[i] > 9) {
            ans[i + 1] += ans[i] / 10;
            ans[i] %= 10;
            if (i == length - 1) {
                length++;
            }
        }
    }
    *lenans = length;
}
void cheng(int* a, int k, int* lena) {
	int i;
    for (i = 0; i < *lena; i++) {
        a[i] *= k;
    }
    for (i = 0; i < *lena; i++) {
        if (a[i] > 9) {
            a[i + 1] += a[i] / 10;
            a[i] %= 10;
            if (i == *lena - 1) {
                (*lena)++;
            }
        }
    }
}
int main() {
    int a[N] = {0}, i;
    int ans[N] = {0};
    int n, lena = 1, lenans = 1;
    scanf("%d", &n);
    if (n == 0) {
        printf("0\n");
        return 0;
    }
    a[0] = 1;
    ans[0] = 1;
    for (i = 2; i <= n; i++) {
        cheng(a, i, &lena);
        jia(ans, a, lena, &lenans);
    }
    for (i = lenans - 1; i >= 0; i--) {
        printf("%d", ans[i]);
    }
    printf("\n");
    return 0;
}
1174:大整数乘法
#include <stdio.h>
#include <string.h>
int main() {
    char s[10001], t[10001];
    int a[20002] = {0};
    scanf("%s %s", s, t);
    int len_s = strlen(s), i, j;
    int len_t = strlen(t);
    for (i = 0; i < len_s / 2; i++) {
        char temp = s[i];
        s[i] = s[len_s - i - 1];
        s[len_s - i - 1] = temp;
    }
    for (i = 0; i < len_t / 2; i++) {
        char temp = t[i];
        t[i] = t[len_t - i - 1];
        t[len_t - i - 1] = temp;
    }
    for (i = 0; i < len_s; i++) {
        for (j = 0; j < len_t; j++) {
            a[i + j] += (s[i] - '0') * (t[j] - '0');
        }
    }
    int carry = 0;
    for (i = 0; i < len_s + len_t; i++) {
        a[i] += carry;
        carry = a[i] / 10;
        a[i] %= 10;
    }
    int len_result = len_s + len_t - 1;
    while (a[len_result] == 0 && len_result > 0) {
        len_result--;
    }
    for (i = len_result; i >= 0; i--) {
        printf("%d", a[i]);
    }
    printf("\n");
    return 0;
}
1175:除以13
#include <stdio.h>
#include <string.h>
int main() {
    int a[105];
    char b[105];
    int i, j, k, m = 0;
    scanf("%s", b);
    k = strlen(b);
    for (i = 0; i < k; i++)
        a[i] = b[i] - '0'; 
    for (i = 0; i < k; i++) {
        m = m * 10 + a[i];
        a[i] = m / 13;
        m = m % 13;
        if (!(a[i] == 0 && (i == 1 || i == 0)))
            printf("%d", a[i]);
    }
    if ((m < 13) && i <= 2 && a[i - 1] == 0)
        printf("0");
    printf("\n%d", m);
    return 0;
}
1310:【例2.2】车厢重组
#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int n, a[10005], count = 0, i, j;
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (i = 1; i <= n - 1; ++i)
        for (j = 1; j <= n - i; ++j)
            if (a[j] > a[j + 1]) {
                swap(&a[j], &a[j + 1]);
                count++;
            }
    printf("%d", count);
    return 0;
}
1311:【例2.5】求逆序对
#include <stdio.h>
#define N 500005
int a[N], t[N];
long long ct;
void merge(int l, int mid, int r) {
    int ti = l, li = l, ri = mid + 1, i;
    while (li <= mid && ri <= r) {
        if (a[li] <= a[ri])
            t[ti++] = a[li++];
        else
        {
            ct += mid - li + 1;
            t[ti++] = a[ri++];
        }
    }
    while (li <= mid)
        t[ti++] = a[li++];
    while (ri <= r)
        t[ti++] = a[ri++];
    for (i = l; i <= r; ++i)
        a[i] = t[i];
}
void mergeSort(int l, int r) {
    if (l >= r)
        return;
    int mid = (l + r) / 2;
    mergeSort(l, mid);
    mergeSort(mid + 1, r);
    merge(l, mid, r);
}
int main() {
    int n, i;
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    mergeSort(1, n);
    printf("%lld", ct);
    return 0;
}
1176:谁考了第k名
#include <stdio.h>
#include <string.h>
#define N 110
struct q {
    char as[50];
    float b;
} a[N];
int main() {
    int n, k, i, j;
    scanf("%d%d", &n, &k);
    for (i = 1; i <= n; i++)
        scanf("%s%f", a[i].as, &a[i].b);
    for (i = 1; i <= n - 1; i++) {
        int tmp = i;
        for (j = i + 1; j <= n; j++)
            if (a[j].b > a[tmp].b) tmp = j;
        struct q temp;
        temp = a[i], a[i] = a[tmp], a[tmp] = temp;
    }
    printf("%s %g\n", a[k].as, a[k].b);
    return 0;
}
1177:奇数单增序列
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int n, a[501];
int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}
int main() {
	int i;
    scanf("%d", &n);
    for (i = 1; i <= n; i ++)
        scanf("%d", &a[i]);
    qsort(a + 1, n, sizeof(int), compare);
    bool patched = false;
    for (i = 1; i <= n; i ++) {
        if (a[i] % 2 == 1) {
            if (patched == true) printf(",");
            patched = true;
            printf("%d", a[i]);
        }
    }
    return 0;
}
1178:成绩排序
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct Stu {
    char name[25];
    int score;
};
bool isPrior(struct Stu *a, struct Stu *b) {
    return a->score > b->score || (a->score == b->score && strcmp(a->name, b->name) < 0);
}
int main() {
    struct Stu stu[25];
    int n, i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%s %d", stu[i].name, &stu[i].score);
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (isPrior(&stu[j + 1], &stu[j])) {
                struct Stu temp = stu[j];
                stu[j] = stu[j + 1];
                stu[j + 1] = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
        printf("%s %d\n", stu[i].name, stu[i].score);
    return 0;
}
1179:奖学金
#include <stdio.h>
struct Stu {
    int id, Chinese, score;
};
int isPrior(struct Stu d1, struct Stu d2) {
    if (d1.score > d2.score)
        return 1;
    else if (d1.score < d2.score)
        return 0;
    else {
        if (d1.Chinese > d2.Chinese)
            return 1;
        else if (d1.Chinese < d2.Chinese)
            return 0;
        else {
            if (d1.id < d2.id)
                return 1;
            else
                return 0;
        }
    }
}
void swap(struct Stu* a, struct Stu* b) {
    struct Stu temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int n, a, b, c, i, j;
	scanf("%d", &n);
    struct Stu d[305];
    for (i = 1; i <= n; ++i) {
        scanf("%d %d %d", &a, &b, &c);
        d[i].id = i;
        d[i].Chinese = a;
        d[i].score = a + b + c;
    }
    for (i = 2; i <= n; ++i) {
        for (j = i; j > 1; --j) {
            if (isPrior(d[j], d[j - 1]))
                swap(&d[j], &d[j - 1]);
            else
                break;
        }
    }
    for (i = 1; i <= 5; ++i)
        printf("%d %d\n", d[i].id, d[i].score);
    return 0;
}
1180:分数线划定
#include<stdio.h>
#include<math.h>
int main(){
	int n, m;
	scanf("%d %d", &n, &m);
	int i, j, count = 0;
	int k[n], s[n];
	for (i = 0; i < n; i++){
		scanf("%d %d", &k[i], &s[i]);
	}
	int number = floor(m * 1.5);
	for (i = 0; i < n; i++){
		int temp_s, temp_k;
		for (j = i + 1; j < n; j++){
			if (s[i] < s[j]){
				temp_k = k[i];
				temp_s = s[i];
				s[i] = s[j];
				k[i] = k[j];
				s[j] = temp_s;
				k[j] = temp_k;
			}
			if (s[i] == s[j]){
				if (k[i] > k[j]){
					temp_k = k[i];
					k[i] = k[j];
					k[j] = temp_k;
				}
			}
		}
		if (s[i] >= s[number-1]){
			count++;
		}
	}
	printf("%d %d\n", s[number-1], count);
	for (i = 0; i < count; i++){
		printf("%d %d\n", k[i], s[i]);
	}
	return 0;
}
1181:整数奇偶排序
#include<stdio.h>
int main(){
	int i, j, count_0 = 0, count_1 = 0;
	int num_0[10], num_1[10];
	int num[10];
	for (i = 0; i < 10; i++){
		scanf("%d", &num[i]);
		if (num[i] % 2 == 0){
			num_0[count_0] = num[i];
			count_0++;
		}
		if (num[i] % 2 == 1){
			num_1[count_1] = num[i];
			count_1++;
		}
	}
	for (i = 0; i < count_0; i++){
		int temp_0;
		for (j = i + 1; j < count_0; j++){
			if (num_0[i] > num_0[j]){
				temp_0 = num_0[i];
				num_0[i] = num_0[j];
				num_0[j] = temp_0;
			}
		}
	}
	for (i = 0; i < count_1; i++){
		int temp_1;
		for (j = i + 1; j < count_1; j++){
			if (num_1[i] < num_1[j]){
				temp_1 = num_1[i];
				num_1[i] = num_1[j];
				num_1[j] = temp_1;
			}
		}
	}
	for (i = 0; i < count_1; i++){
		printf("%d ", num_1[i]);
	}
	for (i = 0; i < count_0; i++){
		printf("%d ", num_0[i]);
	}
	return 0;
}
1182:合影效果
#include<stdio.h>
#include<string.h>
int main(){
	int n;
	scanf("%d", &n);
	double heigh_m[n], heigh_f[n];
	int count_m = 0, count_f = 0, i, j;
	for (i = 0; i < n; i++){
		char str[7];
		double heigh;
		scanf("%s %lf", str, &heigh);
		if (str[0] == 'm'){
			heigh_m[count_m] = heigh;
			count_m++;
		}
		if (str[0] == 'f'){
			heigh_f[count_f] = heigh;
			count_f++;
		}
	}
	for (i = 0; i < count_m; i++){
		double temp_m;
		for (j = i + 1; j < count_m; j++){
			if (heigh_m[i] > heigh_m[j]){
				temp_m = heigh_m[i];
				heigh_m[i] = heigh_m[j];
				heigh_m[j] = temp_m;
			}
		}
	}
	for (i = 0; i < count_f; i++){
		double temp_f;
		for (j = i + 1; j < count_f; j++){
			if (heigh_f[i] < heigh_f[j]){
				temp_f = heigh_f[i];
				heigh_f[i] = heigh_f[j];
				heigh_f[j] = temp_f;
			}
		}
	}
	for (i = 0; i < count_m; i++){
		printf("%.2lf ", heigh_m[i]);
	}
	for (i = 0; i < count_f; i++){
		printf("%.2lf ", heigh_f[i]);
	}
	return 0;
}
1183:病人排队
#include <stdio.h>
#include <string.h>
int main() {
    int n;
    scanf("%d", &n);
    char str_o[n][10], str_y[n][10];
    int year_o[n], year_y[n], order_o[n], order_y[n];
    int i, j, count_o = 0, count_y = 0;
    for (i = 0; i < n; i++) {
        char str[10];
        int year;
        scanf("%s %d", str, &year);
        if (year >= 60) {
            year_o[count_o] = year;
            strcpy(str_o[count_o], str);
            order_o[count_o] = count_o;
            count_o++;
        }
        if (year < 60) {
            year_y[count_y] = year;
            strcpy(str_y[count_y], str);
            order_y[count_y] = count_y;
            count_y++;
        }
    }
    for (i = 0; i < count_o; i++) {
        for (j = i + 1; j < count_o; j++) {
            if (year_o[i] < year_o[j] || (year_o[i] == year_o[j] && order_o[i] > order_o[j])) {
                int temp_year = year_o[i];
                year_o[i] = year_o[j];
                year_o[j] = temp_year;

                char temp_str[10];
                strcpy(temp_str, str_o[i]);
                strcpy(str_o[i], str_o[j]);
                strcpy(str_o[j], temp_str);

                int temp_order = order_o[i];
                order_o[i] = order_o[j];
                order_o[j] = temp_order;
            }
        }
    }
    for (i = 0; i < count_o; i++) {
        printf("%s\n", str_o[i]);
    }
    for (i = 0; i < count_y; i++) {
        printf("%s\n", str_y[i]);
    }
    return 0;
}
1184:明明的随机数
#include<stdio.h>
int main(){
	int n;
	scanf("%d", &n);
	int num[n];
	int i, j, count = 0;
	for (i = 0; i < n; i++){
		scanf("%d", &num[i]);
	}
	for (i = 0; i < n; i++){
		for (j = i + 1; j < n; j++){
			if (num[i] > num[j]){
				int temp = num[i];
				num[i] = num[j];
				num[j] = temp;
			}
			if (num[i] == num[j]){
				num[j] = -1;
			}
		}
		if (num[i] != -1){
			count++;
		}
	}
	printf("%d\n", count);
	for (i = 0; i < n; i++){
		if (num[i] != -1){
			printf("%d ", num[i]);
		}
	}
	return 0;
}*/

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值