冒泡排序
#include <stdio.h>
int main()
{
int arr[10] = { 10,9,13,17,29,30,11,7,8,3 };
//外层控制行(外层执行次数为元素个数-1)
for (int i = 0; i < 9; i++)
{
//内层控制列(每次比较次数=元素个数-执行次数-1;i是从0开始)
for (int j = 0; j < 9- i; j++)
{
//比较两个元素
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
printf(" %d", arr[i]);
}
return 0;
}
//将给定的n个整数从大到小排列
#define N 10
int main()
{
int n, a[N], temp;
scanf("%d", &n);
//找出n个数中最大数 置入a[0],第二大数 置入a[1],以此类推
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);//输入的值是数组元素个数
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])//数组第一个元素与第二个元素比较,以此类推
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
for (int i = 0; i < n - 1; i++)
{
printf("%d ", a[i]);//输出数组对应的数,此时i是下标,所以-1(存放到新数组中)
}
printf("%d", a[n - 1]);//输出新的排序好的数组元素
return 0;
}
数组中插入一个数
int main(void)
{
int i, j, x;
int a[11] = { 1,2,4,6,8,9,12,15,149,156 };
scanf("%d", &x);
for (i = 0; i < 10; i++)
if (x < a[i])//找到数组中比x大的数就进行下一步
break;
for (j = 10; j > i; j--)//从数组最后一个元素下手
a[j] = a[j - 1];//???
a[j] = x;
for (i = 0; i < 11; i++)
printf("%5d", a[i]);
printf("\n");
return 0;
}//不理解
删除数字字符
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string str;
getline(cin, str);
vector<char> v;
for (int i = 0; i < str.size(); i++)
{
if (!isdigit(str[i]))
v.push_back(str[i]);
}
for (int i = 0; i < str.size(); i++)
{
cout << v[i];
}
return 0;
}//(看不懂)
删除非英文字符
#include<stdio.h>
int main()
{
char arr[80];
int i;
scanf("%s", &arr); //输入字符串是%s
for (i = 0; arr[i] != '\0'; i++) //遍历arr每个数据
{
if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))
{
printf("%c", arr[i]);//将是英文的打印出来,也就是变相的删除了非英文
}
}
printf("\n");
}
寻找最高分
#include<stdio.h>
#include<string.h>
struct Student
{
char num[11]; //学号,最多10个字符
char name[11]; //姓名, 最多10个字符
int s1, s2, s3; //三门课的考试成绩
int total; //总成绩
};
typedef struct Student Student; //声明了一个结构类型Student类型
int main() {
int n, maxx = 0;
struct Student s[20];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s %s %d %d %d", s[i].num, s[i].name, &s[i].s1, &s[i].s2, &s[i].s3);
s[i].total = s[i].s1 + s[i].s2 + s[i].s3;
if (maxx < s[i].total)maxx = s[i].total;
}
for (int i = 0; i < n; i++)
if (s[i].total == maxx)
printf("%s %s %d\n", s[i].num, s[i].name, s[i].total);
return 0;
}
杨辉三角
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int n = 0;
scanf("%d", &n);
int a[10][10] = { 0 };
//存放元素
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j == 0 || i == j) //第一列和i==j时直接赋值为1
{
a[i][j] = 1;
}
if (i >= 2 && j >= 1)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
//打印
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
输出三角字符阵列
#include<stdio.h>
int main()
{
int n, i, j;
char x = 64; //从A开始,A的ASCLL是65,进j循环时+1
scanf("%d", &n);
for (i = 0; i < n; i++) //控制行数
{
for (j = 0; j < n - i; j++) //控制打印
{
x += 1; //每一次都要+1,A→65,B→66
printf("%c ", x);
}
printf("\n");
}
return 0;
}
计算阶乘和
#include <stdio.h>
int main()
{
int i = 1, n;
long s = 1, sum = 0;
scanf("%d", &n);
int d = 0;
sum++;
for (; s <= n; s++)
{
for (; i <= s; i++)
{
sum *= i;
}
d += sum;
}
printf("%d", d);
return 0;
}
求交错序列前N相和
#include<stdio.h>
int main()
{
int N, i, flag = 1;
double sum = 0;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
sum += flag * 1.0 * i / (2 * i - 1);
flag = -flag;
}
printf("%.3f", sum);
return 0;
}