2
#include <stdio.h> #define VALUE(X,Y) (1/((1/(X)+1/(Y))/2)) int main() { double x,y; puts("请输入 2 个数: "); while (scanf_s("%lf%lf", &x, &y) == 2 && (x != 0 || y != 0)) { printf("%g 和 %g 的调和平均数为 %g\n", x, y, VALUE(x, y)); puts("请输入下一组数[0 0退出]:"); } system("pause"); return 0; }
3
读书少,完全不懂这道数学题目,看的别人的计算方式
#include <stdio.h> #include <math.h> #define PI 3.141592654 struct value1 { double x; double y; }; struct value2 { double r; double a; }; void compute(struct value2 *ps1, struct value1 *ps2); int main() { struct value2 value; struct value1 values; puts("请输入向量的模和角度:"); while (scanf_s("%lf%lf", &value.r, &value.a) == 2 && (value.r != 0 || value.a != 0)) { compute(&value, &values); printf("x 和 y 的坐标为: %g %g\n", values.x, values.y); puts("请输入向量的模和角度[0 0退出]:"); } puts("完成"); system("pause"); return 0; } void compute(struct value2 *ps1, struct value1 *ps2) { static double z = PI / 180.0; double ang = z * ps1->a; ps2->x = ps1->r*cos(ang); ps2->y = ps1->r*sin(ang); }
4
#include <stdio.h> #include <time.h> void count(double n); int main() { double time; puts("请输入时间[s]:"); while (scanf_s("%lf", &time) == 1) { count(time); puts("时间完成退出."); puts("请输入时间[s][q退出]:"); } puts("完成"); system("pause"); return 0; } void count(double n) { double time; time = (double)clock() / (double)CLOCKS_PER_SEC; while ((double)clock() / (double)CLOCKS_PER_SEC < time + n); }
5
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 33 //抽签 void roll(int *st, int n, int m); int main() { int n[SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33 }; int num; srand((unsigned int)time(0)); //获取系统时间 生成随机种子 puts("请输入抽签的次数:"); while (scanf_s("%d", &num) == 1) { if (num <= 0 || num > SIZE) { puts("错误: 请输入1-33之间."); continue; } roll(n, SIZE, num); puts("请输入抽签的次数[q退出]:"); } puts("完成退出"); system("pause"); return 0; } void roll(int *st, int n, int m) { int i, j; int s_temp; int temp[SIZE]; int count; for (i = 0; i < m; i++) { s_temp = st[rand() % n]; for (j = 0, count = 0; j < i; j++) { if (temp[j] == s_temp) count++; } if (count == 0) { temp[i] = s_temp; printf("%d ", temp[i]); } else i--; } putchar('\n'); }
顺便做了一个双色球随机选号系统
#include <stdio.h> #include <stdlib.h> #include <time.h> #define RSIZE 33 #define BSIZE 16 //抽签 void roll(int *st, int n, int m); //清除输入行 inline static void d_stdin(); //延迟开关 char ch = 'n'; int main() { int red[RSIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33 }; int blue[BSIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }; int num; srand((unsigned int)time(0)); //获取系统时间 生成随机种子 puts("*******双色球开奖系统*******"); puts("请输入随机选号的次数:"); while (scanf_s("%d", &num) == 1) { d_stdin(); puts("询问: 是否打开延迟功能[Y/N]"); scanf_s("%c", &ch, 1); if (ch == 'Y' || ch == 'y') puts("已经打开模拟抽奖时间功能."); else if (ch == 'N' || ch == 'n') puts("已经关闭模拟抽奖时间功能."); else puts("输入错误: 默认关闭模拟抽奖时间功能."); putchar('\n'); for (int i = 0; i < num; i++) { printf("第 %d 次选号结果为:\n", i + 1); printf("红球:"); roll(red, RSIZE, 6); printf("蓝球:"); roll(blue, BSIZE, 1); putchar('\n'); } puts("*******双色球开奖系统*******"); puts("请输入随机选号的次数[q退出]:"); } puts("********选号完成退出********"); system("pause"); return 0; } inline static void d_stdin() { while (getchar() != '\n') continue; } void roll(int *st, int n, int m) { int i, j; int s_temp; int temp[RSIZE]; int count; double time; for (i = 0; i < m; i++) { s_temp = st[rand() % n]; for (j = 0, count = 0; j < i; j++) { if (temp[j] == s_temp) count++; } if (count == 0) temp[i] = s_temp; else i--; } for (i = 0; i < m; i++) { for (j = i; j < m; j++) { if (temp[i] > temp[j]) { s_temp = temp[i]; temp[i] = temp[j]; temp[j] = s_temp; } } } for (i = 0; i < m; i++) { //获取时间值 time = (double)clock() / (double)CLOCKS_PER_SEC; if (ch == 'Y' || ch == 'y') while ((double)clock() / (double)CLOCKS_PER_SEC < time + 0.8); //获取时间值 大于 原先获取时间值加0.8 时跳出循环 printf("%02d ", temp[i]); } putchar('\n'); }
6
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM 40 struct names { char first[NUM]; char last[NUM]; }; void showarray(const struct names *pst, int n); int mycomp(const void *p1, const void *p2); int main() { struct names list[4] = { { "xiaojun","lei" }, {"kang","lei"}, { "dan","zhu" }, {"xiangwei","liu"} }; puts("随机列表:"); showarray(list, 4); qsort(list, 4, sizeof(struct names), mycomp); puts("\n排序列表:"); showarray(list, 4); system("pause"); return 0; } void showarray(const struct names *pst, int n) { int index; for (index = 0; index < n; index++) { printf("%s, %s ", pst[index].first, pst[index].last); putchar('\n'); } } int mycomp(const void *p1, const void *p2) { const struct names *a1 = (const struct names *)p1; const struct names *a2 = (const struct names *)p2; int res; res = strcmp(a1->last, a2->last); if (res != 0) return res; else return strcmp(a1->first, a2->first); }
7
#include <stdio.h> #include <stdlib.h> #include <stdarg.h> //打印 void show_array(const double ar[], int n); //变参函数 double *new_d_array(int n, ...); int main() { double *p1; double *p2; p1 = new_d_array(5, 1.2, 2.3, 3.4, 4.5, 5.6); p2 = new_d_array(4, 100.0, 20.00, 8.08, -1890.0); show_array(p1, 5); show_array(p2, 4); free(p1); free(p2); system("pause"); return 0; } double *new_d_array(int n, ...) { va_list ap; //声明一个对象储存参数 double *pf; int i; pf = (double *)malloc(n * sizeof(double)); va_start(ap, n); //把ap初始化为参数列表 for (i = 0; i < n; i++) pf[i] = va_arg(ap, double); va_end(ap); return pf; } void show_array(const double ar[], int n) { int i; for (i = 0; i < n; i++) printf("%-8.2f ", ar[i]); putchar('\n'); }