2023年10月第四周题解------输入与输出
问题 A: ly喜欢玩石头
解题思路
- 题目告诉我们(1<=a,b <=1e9),那么int类型就够了。因为这两个数相加最大为20亿
- 定义两个变量a和b
- 输入a和b的值
- 打印a加b的值
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { int a;//第一堆石头 int b;//第二堆石头 scanf("%d%d", &a, &b);//输入第一堆和第二堆石头的个数 printf("%d\n", a + b);//打印合并之后石头的个数 return 0; }
问题 B: Hello World!
解题思路
- 调用printf函数打印字符串
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { printf("Hello,World!"); return 0; }
问题 D: 找最小
解题思路
- 假设下标0是最小的,在输入的同时比较新进来的数和最小的谁更小,如果新进来的数小,就更新最小的下标,不是就继续输入下一个数
- 将最小的数与最后一个数进行交换
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> #define N 10 void swap(int *a, int *b);//交换a和b中的值 int main(int argc,char *argv[]) { int a[N];//存放10个数 int min = 0;//假设0下标是最小的,存放最小的下标 for (int i = 0; i < N; i++) {//输入10个数 scanf("%d", a + i);//输入 if (abs(a[min]) > abs(a[i])) {//如果a[i]比最小的数还要小 min = i;//更新最小的下标 } } if (min != N - 1) {//只要最小的不是最后一个,就把最小的数和最后一个数交换 swap(&a[min], &a[N - 1]); } for (int i = 0; i < N; i++) {//打印交换后的数组 printf("%d ", a[i]); } return 0; } void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
问题 E: 统计个数
解题思路
- 定义一个count数组用来统计不同分数段的学生
- 输入分数
- 如果是大于0的数,判断在那个分数段,然后让对应的计数数组加1
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { int score;//存放成绩 int count[3] = {0}; //初始化计数数组 //count[0]代表>=85分段的 //count[1]代表>=60 && <= 84分段的 //count[2]代表>=1 && <=59分段的 scanf("%d", &score);//输入成绩 while(score > 0) {//成绩合法 if ( score >= 85) {//成绩大于等于85 count[0]++; } else if (score >= 60) {//成绩大于等于60且小于等于84 count[1]++; } else {//成绩大于等于1且小于等于59 count[2]++; } scanf("%d", &score);//再次输入成绩 } printf(">=85:%d\n", count[0]);//打印>=85分段的人数 printf("60-84:%d\n", count[1]);//打印60分到84分的人数 printf("<60:%d", count[2]);//打印1分到59分的人数 return 0; }
问题 F: 最大与最小
解题思路
- 要开辟一个有n个float元素的数组
- 假设下标0是最小和最大的,因为数组只有一个数时,下标0位置的数就是最大的也是最小的
- 给float数组赋值,每赋一次值之后和最大数比较,如果新的数比最大数还要大,就把最大的下标更新
- 如果新的数比最小的数还要小,更新最小的下标
- 重复3.4步知道输入完毕
- 输入完之后min指向最小的数,max指向最大的数
- 打印保留两位小数的最大数和最下数
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { int n;//浮点数的个数 scanf("%d", &n);//输入浮点数的个数 float *p = (float*)malloc(sizeof(float) * n);//开辟n个float的空间,相当于float p[n]; int min = 0;//假设下标0是最小的,用来存放最小的下标 int max = 0;//假设下标0是最大的 ,用来存放最大的下标 for (int i = 0; i < n; i++)//读入n个float的数 { scanf("%f", p + i);//输入 if ( p[min] > p[i] )//如果p[i]比p[min]还小 { min = i;//那么最小的就是i } else if ( p[max] < p[i] ) {//如果p[i]比p[max]还大 max = i;//那么最大的就是i } } printf("%.2f %.2f", p[max], p[min]);//打印保留2位小数的最大和最下值 return 0; }
问题 G: 偶数判断
解题思路
- 输入一个数
- 判断它是不是偶数
- 是偶数打印true
- 不是偶数打印false
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { int n; scanf("%d", &n);//输入一个数 if (n % 2 == 0)//如果是偶数 { printf("true\n");//打印true } else //奇数 { printf("false\n");//打印false } return 0; }
问题 H: C语言实验题——鸡兔同笼(JSU-ZJJ)
解题思路
- 假设鸡的数量为x只,兔子的数量为y。那么兔子的数量就为总头的数量减去鸡的数量
- x从0开始到总的数量开始遍历,y = 总的数量 - x
- 判断鸡的脚和兔子的脚相加等不等于总的脚数
- 等于就输出鸡的数量和兔子的数量
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { int headCount;//存放头的数量 int feetCount;//存放腿的数量 scanf("%d%d", &headCount, &feetCount);//输入头的数量,输入腿的数量 for (int i = 0; i <= headCount; i++)//i表示有几只鸡,那么headCount - i就是兔子的数量 { if (i * 2 + (headCount - i) * 4 == feetCount) {//如果鸡的脚和兔子的脚加起来满足腿的总数代表找到了 printf("%d %d\n", i, headCount - i);//打印鸡的数量和兔子的数量 } } return 0; }
问题 I: 憨憨发牌员
解题思路
- 设置一个结构体,因为一张牌有花色和大小
- 读入庄家位置
- 设置第一个拿到牌的人
- 按顺序拿牌,直到没有牌了
- 按照大小进行排序
- 输出结果
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> // 扑克牌结构体 typedef struct { char suit; // 花色:C、D、S、H char rank; // 牌面等级:2~9、T、J、Q、K、A }Card; char s[13] = { '2','3','4','5','6','7','8','9','T','J','Q','K','A' };//用来比较牌面大小的辅助数组 void sort(Card* person);//给一个人手中的牌排序 void p(void);//打印分隔符+---+---+---+---+---+---+---+---+---+---+---+---+---+ void printCard(Card* person);//打印一个人手中的牌 void printRank(Card* person);//打印一个人手中牌的牌面 void printSuit(Card* person);//打印一个手中牌的花色 void swap(Card* person, int i, int j);//交换一个人手中第i张牌和第j张牌交换位置 int find_index(char target);//在辅助数组s中找到牌面所对应的下标 int main(int argc, char* argv[]) { char c = 0;//庄家的位置 Card person[4][13] = { 0 };//0代表北,1代表东,2代表南,3代表西 Card cur = { 0 };//从牌中拿的牌 int m = 0;//表示拿牌的人 int count[4] = { 0 };//表示每个人手上有几张牌 scanf("%c", &c);//读入庄家的位置 getchar();//处理掉在缓存区的换行,避免读到换行 if (c == 'N')//如果庄家位置是北 { m = 1;//第一个拿牌位置是东 } else if (c == 'E')//如果庄家位置是东 { m = 2;//第一个拿牌位置是南 } else if (c == 'S')//如果庄家位置是南 { m = 3;//第一个拿牌位置是西 } else {//如果庄家位置是西 m = 0;//第一个拿牌位置是北 } while (count[0] + count[1] + count[2] + count[3] <= 51)//如果牌没有被拿完 { scanf("%c%c", &cur.suit, &cur.rank);//从牌堆中读入 person[m][count[m]].suit = cur.suit;//赋给拿牌的人 person[m][count[m]].rank = cur.rank; count[m]++;//手中牌数加1 m++;//下一个拿牌的人 m %= 4;//如果这个人的位置是西了,那么他的下一个就是北,因此要模4[0, 3]; } for (int i = 0; i < 4; i++) {//给每个人手中的牌排序 sort(person[i]); } printf("South player:\n"); printCard(person[2]);//打印在南方位置人的牌 printf("West player:\n"); printCard(person[3]);//打印在西方位置人的牌 printf("North player:\n"); printCard(person[0]);//打印在北方位置人的牌 printf("East player:\n"); p();//打印分隔符 printRank(person[1]);//打印在东方位置人手中牌的牌面 printSuit(person[1]);//打印在东方位置人手中牌的花色 printRank(person[1]);//打印在东方位置人手中牌的牌面 printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+");//打印分隔符 return 0; } void p(void) { printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n"); } void printCard(Card* person) { p(); printRank(person); printSuit(person); printRank(person); p(); } void printRank(Card* person) { putchar('|'); for (int i = 0; i < 13; i++) { printf("%c %c|", person[i].rank, person[i].rank); } putchar('\n'); } void printSuit(Card* person) { putchar('|'); for (int i = 0; i < 13; i++) { printf(" %c |", person[i].suit); } putchar('\n'); } void sort(Card* person) { //按花色排序,用的是冒泡 for (int i = 0; i < 12; i++) { for (int j = 0; j < 12 - i; j++) { if (person[j].suit == 'H') {//如果前一张的牌花色是最大的花色就交换 swap(person, j, j + 1); } else if (person[j].suit == 'S' && (person[j + 1].suit == 'D' || person[j + 1].suit == 'C')) {//如果前一张的牌花色是第二大的花色, //且后面的花色没有前一张牌花色大,那么交换 swap(person, j, j + 1); } else if (person[j].suit == 'D' && person[j + 1].suit == 'C') {//如果前一张牌的花色是第三大的 //且后面的花色是最小的花色,那么交换 swap(person, j, j + 1); } } } //按牌面排序,用的是冒泡 for (int i = 0; i < 12; i++) { for (int j = 0; j < 12 - i; j++) { if (person[j].suit == person[j + 1].suit) {//通过辅助数组的下标来比较大小,下标越大,那么牌面就越大 if (find_index(person[j].rank) > find_index(person[j + 1].rank)) { swap(person, j, j + 1); } } } } } void swap(Card* person, int i, int j) { char t = person[j].suit; person[j].suit = person[i].suit; person[i].suit = t; t = person[j].rank; person[j].rank = person[i].rank; person[i].rank = t; } int find_index(char target) { for (int i = 0; i < 13; i++) { if (s[i] == target) { return i; } } }
问题 J: acm社团欢迎你
解题思路
- 调用printf函数打印字符串
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(int argc,char *argv[]) { printf("acm社团欢迎你"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人