C语言程序设计_Cprog0103奇数之和

描述

输入一个正整数n,求解并输出1~n之间所有奇数之和。

输入

在一行中给出一个整数n(1≤n≤1000)。

输出

在一行中输出一个整数,即1~n之间所有奇数之和。

输入样例 1 

51

输出样例 1

676

输入样例 2 

800

输出样例 2

160000
#include<stdio.h>
int main()
{
	int i=1,s=0,n;
	scanf("%d",&n);
	while(i<=n)
	{
		if(i%2!=0)
		{
		s=s+i;
	    }
		i=i+1;
	}
	printf("%d",s);
	return 0;
}

PS:使用while循环语句判断i是否为奇数,如果是,累加到s上,注意s初始化要为0,因为定义s后系统会随机分配数值,不一定初始值为0.

                                                                                                                               解答完毕

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个比较大的项目,需要涉及到读写文件、结构体、数组、排序、查找等多个知识点,下面是一个简单的实现: ``` #include <iostream> #include <fstream> #include <string> #include <algorithm> using namespace std; struct Student { string num; string name; string sex; int year; int month; int day; int math; int cprog; int phy; int total; float avg; }; int count = 0; Student students[100]; void readData() { ifstream fin("student.txt"); while (!fin.eof()) { fin >> students[count].num; fin >> students[count].name; fin >> students[count].sex; fin >> students[count].year; fin >> students[count].month; fin >> students[count].day; fin >> students[count].math; fin >> students[count].cprog; fin >> students[count].phy; students[count].total = students[count].math + students[count].cprog + students[count].phy; students[count].avg = students[count].total / 3.0; count++; } fin.close(); } void display() { cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl; for (int i = 0; i < count; i++) { cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t"; cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl; } } void add() { cout << "请输入要添加的学生个数:"; int num; cin >> num; for (int i = 0; i < num; i++) { cout << "请输入第" << i + 1 << "个学生的信息:" << endl; cout << "学号:"; cin >> students[count].num; cout << "姓名:"; cin >> students[count].name; cout << "性别:"; cin >> students[count].sex; cout << "出生日期(格式为YYYY MM DD):"; cin >> students[count].year >> students[count].month >> students[count].day; cout << "高数成绩:"; cin >> students[count].math; cout << "C语言成绩:"; cin >> students[count].cprog; cout << "大学物理成绩:"; cin >> students[count].phy; students[count].total = students[count].math + students[count].cprog + students[count].phy; students[count].avg = students[count].total / 3.0; count++; } } bool cmpNum(Student a, Student b) { return a.num < b.num; } bool cmpTotal(Student a, Student b) { return a.total > b.total; } void sort() { int choice; cout << "请选择排序方式(1.按学号递增排序 2.按总分递减排序):"; cin >> choice; if (choice == 1) { std::sort(students, students + count, cmpNum); } else if (choice == 2) { std::sort(students, students + count, cmpTotal); } else { cout << "输入有误,请重新选择!" << endl; sort(); } } void searchNum() { string num; cout << "请输入要查找的学生学号:"; cin >> num; for (int i = 0; i < count; i++) { if (students[i].num == num) { cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl; cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t"; cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl; return; } } cout << "未找到该学生!" << endl; } void searchName() { string name; cout << "请输入要查找的学生姓名:"; cin >> name; for (int i = 0; i < count; i++) { if (students[i].name == name) { cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl; cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t"; cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl; return; } } cout << "未找到该学生!" << endl; } void searchScore() { int score; cout << "请输入要查找的成绩:"; cin >> score; bool found = false; cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl; for (int i = 0; i < count; i++) { if (students[i].math == score || students[i].cprog == score || students[i].phy == score) { cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t"; cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl; found = true; } } if (!found) { cout << "未找到该成绩!" << endl; } } void calcAvg() { int mathSum = 0; int cprogSum = 0; int phySum = 0; for (int i = 0; i < count; i++) { mathSum += students[i].math; cprogSum += students[i].cprog; phySum += students[i].phy; } float mathAvg = mathSum / (float)count; float cprogAvg = cprogSum / (float)count; float phyAvg = phySum / (float)count; cout << "高数平均分:" << mathAvg << endl; cout << "C语言平均分:" << cprogAvg << endl; cout << "大学物理平均分:" << phyAvg << endl; } void saveData() { ofstream fout("student.txt"); for (int i = 0; i < count; i++) { fout << students[i].num << " "; fout << students[i].name << " "; fout << students[i].sex << " "; fout << students[i].year << " "; fout << students[i].month << " "; fout << students[i].day << " "; fout << students[i].math << " "; fout << students[i].cprog << " "; fout << students[i].phy << endl; } fout.close(); } void showMenu() { cout << "1. 显示成绩" << endl; cout << "2. 添加学生成绩" << endl; cout << "3. 排序" << endl; cout << "4. 查找" << endl; cout << "5. 计算平均分" << endl; cout << "6. 保存数据" << endl; cout << "0. 退出程序" << endl; } int main() { readData(); while (true) { showMenu(); int choice; cout << "请输入要执行的操作:"; cin >> choice; if (choice == 0) { break; } else if (choice == 1) { display(); } else if (choice == 2) { add(); } else if (choice == 3) { sort(); } else if (choice == 4) { int searchChoice; cout << "请选择查找方式(1.按学号查找 2.按姓名查找 3.按分数查找):"; cin >> searchChoice; if (searchChoice == 1) { searchNum(); } else if (searchChoice == 2) { searchName(); } else if (searchChoice == 3) { searchScore(); } else { cout << "输入有误,请重新选择!" << endl; } } else if (choice == 5) { calcAvg(); } else if (choice == 6) { saveData(); } else { cout << "输入有误,请重新选择!" << endl; } } return 0; } ``` 这个程序中,我们首先定义了一个结构体 `Student`,表示学生的基本信息和成绩,然后用一个数组 `students` 存储所有学生的信息。`readData()` 函数从文件中读取数据,`display()` 函数显示所有成绩,`add()` 函数添加一条或多条记录,`sort()` 函数按照学号或总分进行排序,`searchNum()`、`searchName()` 和 `searchScore()` 函数分别按照学号、姓名和分数进行查找,`calcAvg()` 函数计算各门课程的平均分数,`saveData()` 函数将结果保存到文件中。 在主函数中,我们通过一个循环来不断读取用户的输入,根据用户输入的操作来调用相应的函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值