OJ循环——求a!+b!+c!的和

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:李晓凯
 * 完成日期:2014年 12 月 31 日
 * 版 本 号:v1.0
 *
 * 问题描述:求a!+b!+c!的和

 * 输入描述:输入a、b、c的值
 * 程序输出:输出所求的和

 */

#include <iostream>
using namespace std;
int fac(int );
int main()
{
  int a,b,c,sum;
  cin>>a>>b>>c;
  sum=fac(a)+fac(b)+fac(c);
  cout<<sum<<endl;
  return 0;
}
int fac(int n)
{
    int i,f=1,sum=0;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    sum+=f;
    return sum;
}



由于当前未提供关于东方博宜OJ平台题号2437的具体题目描述,无法直接给出完整的C语言解题代码。然而,可以根据常见OJ平台上的问题类型推测可能涉及的内容,基于此构建通用解决方案。 假设东方博宜OJ 2437是一道基础算法或逻辑判断类题目,以下是针对几种典型场景的解决方法: --- ### 假设1:计算两个整数的商余数 如果该题目类似于引用[1]中的内容,即要输入两个整数输出它们的商余数,则可以使用如下代码实现: ```c #include <stdio.h> int main() { int a, b; scanf("%d %d", &a, &b); printf("%d\n%d\n", a / b, a % b); // 输出余数 return 0; } ``` 上述代码实现了基本的除法运算[^1]。 --- ### 假设2:素数判定 如果该题目类似于引用[2]中的内容,即要判断某个整数是否为素数,则可参考以下代码: ```c #include <stdio.h> #include <stdbool.h> int main() { int n; scanf("%d", &n); bool isPrime = true; if (n <= 1) { isPrime = false; } for (int i = 2; i * i <= n; ++i) { // 判断是否存在因子 if (n % i == 0) { isPrime = false; break; } } if (isPrime) { printf("T\n"); // 是素数 } else { printf("F\n"); // 不是素数 } return 0; } ``` 这段代码通过循环检测给定数字是否有因数来完成素数验证[^2]。 --- ### 假设3:字符串合法性检查 假如题目与引用[3]有关联,比如检验某一标识符是否合法(仅由字母、数字或下划线组成),则可以用下面的方法处理: ```c #include <stdio.h> #include <ctype.h> #define MAX_LEN 100 int main() { char str[MAX_LEN]; scanf("%s", str); int len = strlen(str); bool isValid = true; for (int i = 0; i < len; ++i) { if (!((str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_' || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9'))) { isValid = false; break; } } if (isValid) { printf("Valid Identifier\n"); } else { printf("Invalid Identifier\n"); } return 0; } ``` 这里利用ASCII码范围筛选有效字符[^3]。 --- ### 假设4:竞赛评分机制模拟 考虑到引用[4]提到的比赛计分规则,若本题与此相似——统计选手得分情况及其最终排名,那么程序结构大致如下所示: ```c #include <stdio.h> #include <string.h> typedef struct Contestant { char name[11]; int solvedProblemsCount; long totalTimePenalty; } Contestant; void swap(Contestant* a, Contestant* b) { Contestant temp = *a; *a = *b; *b = temp; } // 排序函数定义 void sortParticipantsByScoreAndTime(Contestant participants[], int size) { for (int i = 0; i < size - 1; ++i) { for (int j = 0; j < size - i - 1; ++j) { if (participants[j].solvedProblemsCount < participants[j + 1].solvedProblemsCount || (participants[j].solvedProblemsCount == participants[j + 1].solvedProblemsCount && participants[j].totalTimePenalty > participants[j + 1].totalTimePenalty)) { swap(&participants[j], &participants[j + 1]); } } } } int main() { int problemNum, penaltyPerWrongSubmission; scanf("%d %d", &problemNum, &penaltyPerWrongSubmission); const int maxParticipants = 100; Contestant contestants[maxParticipants]; int participantCount = 0; while (scanf("%s", contestants[participantCount].name) != EOF) { contestants[participantCount].solvedProblemsCount = 0; contestants[participantCount].totalTimePenalty = 0; for (int pIdx = 0; pIdx < problemNum; ++pIdx) { int timeTakenOrNegativeAttempts; scanf("%d", &timeTakenOrNegativeAttempts); if (timeTakenOrNegativeAttempts > 0) { contestants[participantCount].solvedProblemsCount++; contestants[participantCount].totalTimePenalty += abs(timeTakenOrNegativeAttempts); int wrongAttemptCount; scanf("%d", &wrongAttemptCount); contestants[participantCount].totalTimePenalty += wrongAttemptCount * penaltyPerWrongSubmission; } } participantCount++; } sortParticipantsByScoreAndTime(contestants, participantCount); for (int idx = 0; idx < participantCount; ++idx) { printf("%s %d %ld\n", contestants[idx].name, contestants[idx].solvedProblemsCount, contestants[idx].totalTimePenalty); } return 0; } ``` 以上代码片段展示了如何依据特定条件对参赛者进行排序[^4]。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值