正负相和
//正负项和-------
void code2_8() {
int denominator = 1, flag = 1, i, n;
double item = 1, sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += item;
flag = -flag;
denominator += 2;
item = flag * 1.0 / denominator;
}
printf("sum = %f\n", sum);
}
统计空格or换行,数字,英文个数(switch)
void code3_10_1(){
int blank= 0,digit = 0,i,n,other = 0;
char ch;
scanf("%d",&n);
for(i = 0;i < n;i++){
ch = getchar();
switch(ch){
case ' ':
blank++;
break;
case '\n':
blank++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digit++;
break;
default:
other++;
}
}
printf("blank = %d, digit = %d, other = %d",blank,digit,other);
}
斐波那契数列
void code4_9() {
int n;
int fib[46] = {1, 1};
scanf("%d", &n);
if (n >= 1 && n <= 46) {
for (int i = 2; i < n; i++) {//从第三位开始, 滚动前进
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < n; i++) {
printf("%6d", fib[i]);
if ((i + 1) % 5 == 0) {
printf("\n");
}
}
if (n % 5 != 0) {
printf("\n");
}
}else{
printf("Invalid.\n");
}
}
阶乘合
double my_fact(int n){
int i;
double result = 1;
if(n<0){
return 0;
}
for(i = 1;i<=n;i++){
result*=i;
}
return result;
}
//阶层和------
void code4_7(){
int i,n;
double sum = 0;
scanf("%d",&n);
for(i = 1;i<=n;i++){
sum += my_fact(i);
}
printf("%.0f",sum);
}
统计区间内素数和
void code4_10(){
int m, n;
scanf("%d %d", &m, &n);
int sum = 0, count = 0;
for (int i = m; i <= n; ++i) {
int flag = 1;
if(i == 1) flag = 0;//1不是素数
for (int j = 2; j <= sqrt(i) + 1; ++j) {
if (i % j == 0) {//不是素数
flag = 0;
break;
}
}
if(i == 2) flag = 1;
if (flag) {
sum += i;
count++;
}
}
printf("%d %d", count, sum);
}
数字金字塔
void pyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
}
//n的数字金字塔
void code5_6() {
int n;
scanf("%d", &n);
pyramid(n);
}
调用静态变量的函数计算n!
double fact_s(int n){
static double f = 1;//静态变量在程序执行前进行一次初始化
f*=n;
return f;
}
//调用静态变量的函数计算n!
void code5_9(){
int n;
scanf("%d",&n);
for(int i = 1;i<=n;i++){
printf("%3d!=%.0f\n",i,fact_s(i));
}
}
统计单词个数
//统计单词个数
void code6_5(){
int cnt=0,word=0;
char ch;
while((ch = getchar())!= '\n'){
if(ch == ' '){
word = 0;
} else if(word == 0){
word = 1;
cnt++;
}
}
printf("%d\n",cnt);
}
选择排序
//选择排序
void code7_5() {
int arr[] = {21, 4, 1, 6, 7, 1, 45};
int n = sizeof(arr) / 4;
int min_index;
for (int i = 0; i < n - 1; i++) {
min_index = i;
for (int j = i + 1; j < n; j++) {
if(arr[min_index]>arr[j]){
min_index = j;
}
}
if(arr[i]!=arr[min_index]){
arr[i]^=arr[min_index]^=arr[i]^=arr[min_index];
}
}
for(int i : arr){
printf("%d,",i);
}
}
二分查找
//二分查找
void code7_7() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / 4, x;
scanf("%d", &x);
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (x == arr[mid]) {
break;
} else if (x < arr[mid]) {
high = mid - 1;
} else { // x> arr[mid]
low = mid + 1;
}
}
if(low <= high){
printf("Index is %d\n",mid);
} else{
printf("Not Found\n");
}
}
方阵转置
//方阵转置
void code7_9() {
int n;
scanf("%d", &n);
int arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; ++j) {
arr[i][j] = i * n + j + 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i <= j) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%4d", arr[i][j]);
}
printf("\n");
}
}
回文
//回文
void code7_11() {
//字符串输入-----------
char line[80];
int k = 0;
while ((line[k] = getchar()) != '\n') {
k++;
}
line[k] = '\0';
//判断回文------------
int i = 0;
k -= 1;
while(i<k){
if(line[i]!=line[k]){
break;
}
i++;
k--;
}
if(i>=k){
printf("It is a palindrome\n");
}else{
printf("It is not a palindrome\n");
}
}
凯撒密码
//凯撒密码
void code7_12() {
char str[80];
int i = 0;
while ((str[i] = getchar()) != '\n') {
i++;
}
str[i] = '\0';
//输入offset
int offset;
scanf("%d", &offset);
if (offset >= 26) {
offset %= 26;
}
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
if ((str[i] - 'A' + offset) < 26) {
str[i] += offset;
} else {
str[i] -= 26 - offset;
}
} else if (str[i] >= 'a' && str[i] <= 'z') {
if ((str[i] - 'a' + offset) < 26) {
str[i] += offset;
} else {
str[i] -= 26 - offset;
}
}
}
for (i = 0; str[i] != '\0'; i++) {
putchar(str[i]);
}
printf("\n");
}
输入年份和天数, 输入年月日
void month_day(int year, int year_day, int *pmonth, int *pday) {
int tab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};
int is_leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
int k = 1;
for (; year_day > tab[is_leap][k]; k++) {
year_day -= tab[is_leap][k];
}
*pmonth = k;
*pday = year_day;
}
冒泡排序
//冒泡法
void code8_5() {
int arr[] = {7, 4, 7, 3, 22, 72, 2};
int n = sizeof(arr) / 4;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
arr[j] ^= arr[j + 1] ^= arr[j] ^= arr[j + 1];
}
}
}
for (int i = 0; i < n; i++) {
printf("%d, ",arr[i]);
}
}
指针计算元素个数
//指针计算元素个数及存储元素
void code8_7() {
double a[2], *p, *q;
p = &a[0]; //指针p指向数组a的首地址
q = p + 1; //指针q指向数组元素a[1]
printf("%d\n", q - p); //计算指针p和q之间的元素的个数
printf("%d\n", (int)q - (int)p);//计算指针p和q之间的字节数\
}
字符压缩
void zip(char *p) {
char *q = p;
int n;
while (*p != '\0') {
n = 1;
while (*p == *(p + n)) {
n++;
}
if (n >= 10) {
*q++ = (n / 10) + '0';
*q++ = (n % 10) + '0';
} else if (n >= 2) {
*q++ = n + '0';
}
*q++ = *(p + n - 1);
p += n;
}
*q = '\0';
}
最小串
//最小串
void code8_9() {
int n;
char sx[80], smin[80];
scanf("%d", &n);
scanf("%s", sx);
strcpy(smin, sx);
for (int i = 1; i < n; i++) {
scanf("%s",sx);
if(strcmp(sx,smin)<0){//找到最小串
strcpy(smin,sx);
}
}
printf("min is %s \n",smin);
}
递归求最大公约数
int gcd(int m, int n) {
if (m % n == 0) {
return n;
} else {
return gcd(n, m % n);
}
}
//递归求最大公约数
void code10_3() {
int n, m;
scanf("%d %d", &m, &n);
printf("%d", gcd(m, n));
}
递归求n!
int fact(int n){
if(n==1||n ==0){
return 1;
}
return n* fact(n-1);
}
//递归求n!
void code10_2(){
int n;
scanf("%d",&n);
printf("%d",fact(n));
}
带参宏定义
#define MAX(a, b) a>b?a:b
#define SQR(x) x*x
//带参数的宏定义
void code10_8() {
int x, y;
scanf("%d %d", &x, &y);
x = MAX(x,y);
y = SQR(x);
printf("%d %d\n",x,y);
}
#undef MAX
#undef SQR
指针数组开头字母找单词
//指针数组开头字母找单词
void code11_1() {
char ch;
const char *color[5] = {"red", "blue", "yellow", "green", "black"};
ch = getchar();
int flag = 0;
for (int i = 0; i < 5; ++i) {
if (*color[i] == ch) {
flag = 1;
puts(color[i]);
}
}
if(flag == 0){
printf("Not Found\n");
}
}
串从小到大用指针排序
void fsort(const char *color[], int n) {
const char *temp;
for (int k = 0; k < n-1; k++) {//冒泡排序
for (int j = 0; j < n - k -1; j++) {
if (strcmp(color[j], color[j + 1]) > 0) {
temp = color[j];
color[j] = color[j+1];
color[j+1] = temp;
}
}
}
}
//5个串从小到大用指针排序
void code11_4() {
const char *pcolor[5] = {"red", "blue", "yellow", "green", "black"};
fsort(pcolor, 5);
for (int i = 0; i < 5; i++)printf("%s ", pcolor[i]);
}
藏头诗
//藏头诗
void code11_5() {
int i, n = 0;
char *poem[20], str[80], mean[20];
gets(str);
while (str[0] != '#') {
poem[n] = (char *) malloc(sizeof(char) * (strlen(str) + 1));
strcpy(poem[n], str);
n++;
gets(str);
}
for (i = 0; i < n; i++) {
mean[i] = *poem[i];
free(poem[i]);
}
mean[i] = '\0';
puts(mean);
}