周次 |
学习时间 |
新编写代码行数 |
博客量(篇) |
学到知识点 |
第15周 |
0.5h |
95 |
1 |
#include<stdio.h> #include "stdafx.h" #include"math.h" int search();//查找数组下标 void calc();//计算 int nums[5]; void sort(int nums); main() { int i; int nums[5]; // int index; // calc(); // index=search(); // printf("The you need number's index is :%d ",index); printf("Please enter the array's element : "); for (i = 0; i < 5; i++) scanf_s("%d",nums+i); printf("The array is : "); for (i = 0; i < 5; i++) printf(" %d ",*(nums+i)); sort(nums); getch(); return 0; } void sort(int *(nums)) { int i, j; int temp; for (i = 0; i <= 5; i++) { for (j = 0; j <= i-2; j++) { if (*(nums + j) > *(nums + j + 1)) { temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } printf(" The after sort array:"); for (i = 0; i < 5; i++) { printf(" %d ",*(nums+i)); } } int search() { int i; int searchnums; int searchindex=-1; int nums[5] = {0,5,12,6,32}; printf("Please enter the element you want to find "); scanf_s("%d", &searchnums); printf(" "); for (i = 0; i <= 5; i++) { if (searchnums == *(nums + i)) { searchindex = i; break; } } return searchindex; } void calc()//计算最小公倍数,最大公约数 { int i, j, num1, num2; printf("Please put two numbers for Find the least common multiple :"); scanf_s("%d%d", &num1, &num2); for (i = 1; i < num1*num2; i++) { if (i%num1 == 0 && i%num2 == 0) break; } for (j =num1+num2; j >1; j--) { if (num1 % j == 0 && num2%j == 0) break; } printf("The %d and %d 's least common multiple is %d ", num1, num2, i); printf("The %d and %d 's greatest common divisor gcd is %d ", num1, num2, j); }