c语言练习
计算数字有多少段
连续相同的最长整数序列为一段,2 2 3 4 5 ,2 2 第一段;3第二段;4 第三段;5 第四段。
`#define _CRT_SECURE_NO_WARNINGS 1`
`#include<stdio.h>`
`int main() {``int a[100];//接收数字``int sum = 0;//计算段``int n;//接收会输入多少数字``scanf("%d", &n);``for (int i = 1; i <= n; i++) {``scanf("%d", &a[i]);``}``a[0] = 1001;``for (int j = 1; j <= n; j++) {``if (a[j] != a[j - 1]) {``sum++;``}``}``printf("%d", sum);`
`}`
数字排序
第一行输入包含多少数字;第二行输入数字,中间用空格分开。
要求:计算每个数各个位数之和,输出从大到小。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdbool.h>
int shu(int x) {int sum = 0;while (x != 0) {sum += x % 10;x = x / 10;//只取整数部分}return sum;
}int main() {int a[100], b[100];//接收数字int n;//接收会输入多少数字scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (int j = 1; j <= n; j++) {b[j] = shu(a[j]);}//冒泡算法实现排序for (int i = 1; i <= n; i++) {bool flag =false;for (int j = 1; j <= n - i; j++) {if (b[j + 1] > b[j] || (b[j + 1] > b[j] && a[j + 1] < a[j])) {flag = true;int h = b[j + 1];int l = a[j + 1];b[j + 1] = b[j];a[j + 1] = a[j];b[j] = h;a[j] = l;}}if (flag = false)break;}for (int i = 1; i <= n; i++) {printf("%d\t%d\n", a[i], b[i]);}return 0;
}
棋子消消消
给一个棋盘,不同数字代表不同颜色棋子,行或者列有三个以上相同则消除,消除后为0。给出消除一次后的结果。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdbool.h>int main() {int a[100][100];//接收数字int n,m;//n行 m列scanf("%d%d", &n,&m);for (int i = 1; i <= n; i++) {for(int j=1;j<=m;j++)scanf("%d", &a[i][j]);}for (int i = 1; i <=n; i++) {for (int j = 1; j <=m ; j++) {//判断是否有行三个相等if (a[i][j] == a[i][j + 1] && a[i][j + 1] == a[i][j + 2]) {a[i][j] =0;a[i][j + 1] = 0;a[i][j + 2] = 0;}//判断是否有列三个相等if (a[i][j] == a[i + 1][j] && a[i+1][j] == a[i + 2][j]) {a[i][j] = 0;a[i + 1][j] = 0;a[i + 2][j] = 0;}}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {printf("%d\t", a[i][j]);}printf("\n");}return 0;
}
表达式求值
写出这个,你的栈算是学好了!!!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>#define MAX_SIZE 100
//定义栈结构
typedef struct {//数字int data[MAX_SIZE];int top;
}NumStack;
typedef struct {//字符int data[MAX_SIZE];int top;
}OpStack;//初始化
void initNumStack(NumStack *s) {s->top = -1;
}
void initOpStack(OpStack *s) {s->top = -1;
}//判空
int isNumStackEmpty(NumStack* s) {return s->top == -1;
}
int isOpStackEmpty(OpStack* s) {return s->top == -1;
}//判满
int isNumStackFull(NumStack* s) {return s->top == MAX_SIZE -1;
}
int isOpStackFull(OpStack* s) {return s->top == MAX_SIZE -1;
}//入栈
void pushNum(NumStack *s, int value) {if (isNumStackFull(s)) {printf("栈满,无法入栈\n");}s->data[++(s->top)] = value;
}
void pushOp(OpStack* s, char value) {if (isOpStackFull(s)) {printf("栈满,无法入栈\n");}s->data[++(s->top)] = value;
}//出栈
int popNum(NumStack* s) {if (isNumStackEmpty(s)) {printf("栈空\n");return -1;}return s->data[(s->top)--];
}
char popOp(OpStack* s) {if (isOpStackEmpty(s)) {printf("栈空\n");return -1;}return s->data[(s->top)--];
}//操作符优先级
int getPriority(char op) {switch (op) {case '+':case '-':return 1;case '*':case '/':return 2;default:return 0;}
}//计算表达式
int calculate(int a, int b, char op) {switch (op) {case '-':return a - b;case '+':return a + b;case '*':return a * b;case '/':return a / b;default:return 0;}
}int main() {char expression[MAX_SIZE];NumStack numStack;OpStack opStack;initNumStack(&numStack);initOpStack(&opStack);printf("输入表达式:\n");scanf("%s", &expression);int i = 0;while (expression[i] != '\0') {if (isdigit(expression[i])) {int num = 0;while (isdigit(expression[i])) {num = num * 10 + (expression[i] - '0');i++;}pushNum(&numStack, num);i--;}else if (expression[i] == '(') {pushOp(&opStack, expression[i]);}else if (expression[i] == ')') {while (opStack.data[opStack.top] != '(') {int b = popNum(&numStack);int a = popNum(&numStack);char op = popOp(&opStack);int result = calculate(a, b, op);pushNum(&numStack, result);}popOp(&opStack);}else {while (!isOpStackEmpty(&opStack) && getPriority(opStack.data[opStack.top]) >= getPriority(expression[i])) {int b = popNum(&numStack);int a = popNum(&numStack);char op = popOp(&opStack);int result = calculate(a, b, op);pushNum(&numStack, result);}pushOp(&opStack,expression[i]);}i++;}while (!isOpStackEmpty(&opStack)) {int b = popNum(&numStack);int a = popNum(&numStack);char op = popOp(&opStack);int result = calculate(a, b, op);pushNum(&numStack, result);}int fianlResult = popNum(&numStack);printf("最终结果为:%d\n", fianlResult);return 0;}