求编写源程序
发布网友
发布时间:2023-06-27 07:20
我来回答
共2个回答
热心网友
时间:2024-11-29 11:38
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int is_prime(int n){
int m;
for(m=2 ; m <= (int)sqrt(n); m++){
if(n%m == 0){
return 0;
}
}
return 1;
}
void timu_1(){
int min = 500;
int max = 1000;
int sum = 0;
int i;
for(i = min ; i <= max ; i++){
if(is_prime(i)){
sum = sum + i;
}
}
printf("the sum of prime numbers between %d - %d is %d \n" , min , max , sum);
}
void timu_2(){
int count = 20;
int i ;
float sum = 0.0;
float a = 2.0;
float b = 1.0;
float c = 0.0;
for(i = 0 ; i < count ; i++){
sum += a / b;
//printf("%d %f/%f\n" , i , a , b);
c = a;
a = a + b;
b = c;
}
printf("the sum is %f \n" , sum);
}
void timu_3(){
float sum = 0.0;
int min = 1000;
int max = 2000;
int count = 0;
int i ;
for(i = min ; i <= max ; i++){
if(is_prime(i)){
sum += i;
count++;
}
}
printf("prime numbers average between %d - %d is %.2f\n" , min , max , sum/count);
}
void timu_4(){
int ary[] = {56,78,98,45,58,34,12,90,26,88};
int i , j , len;
int count = 3;
float sum = 0.0;
for(i = 0 , len = sizeof(ary)/sizeof(int); i < len ; i++){
for(j = i+1 ; j < len ; j++){
if(ary[j]>ary[i]){
ary[j] = ary[i] ^ ary[j];
ary[i] = ary[i] ^ ary[j];
ary[j] = ary[i] ^ ary[j];
}
}
}
for(i = 0 ; i < count; i++){
sum += sqrt(ary[i]);
}
printf(" %f \n" , sum);
}
void timu_5(){
int sum = 0;
int i = 1;
do{
sum += (2*i-1)*(2*i+1);
if(((sum%7)==0) && (sum > 1000)){
printf("sum : %d n=%d\n" , sum , i);
break;
}
i++;
}while(1);
}
void timu_6(){
static int ary[20] = { 1 };
int i, j;
int width; /*---表示结果的"宽度"---*/
int current_num; /*--- 当前数字 ---*/
/*--- 从大到小进行阶乘计算 ---*/
for( width = 0 , current_num = 20 ; current_num > 1; current_num-- ){
/*--- 对每一个‘分段’进行运算 ---*/
for( i = j = 0; i <= width; i++ ){
/*--- 当前运算的‘有效数值’ ---*/
ary[i] = ( (j += ary[i] * current_num) ) % 10000;
/*--- 当前运算的‘进位数值’ ---*/
j /= 10000;
}
if ( ary[i] = j ){ /*如果有进位,则索引向前推进*/
width++;
}
}
printf ( "%d", ary[width] );
/*--- 将求的数值输出 ---*/
for( j = width - 1; j >= 0; j-- ){
printf( "%04d", ary[j] );/*--- 这里的6跟10000的位数有关 ---*/
}
printf("\n");
}
void timu_7(){
srand(time(NULL));
int len = 10;
int* ary = (int* )malloc(sizeof(int)*len);
if(ary == NULL){
printf("malloc error !\n");
exit(1);
}
int sum = 0;
int num , i;
float ave = 0.0;
int count = 0;
for(i = 0 ; i < len ; i++){
num = (rand()%90)+10;
*(ary+i) = num;
sum += num;
}
ave = 1.0*(sum/len);
for(i = 0 ; i < len ; i++){
if(*(ary+i) < ave){
count++;
}
printf("ary[%d]=%d " , i , *(ary+i));
}
printf("average=%.1f\n" , ave);
int* out = (int*)malloc(sizeof(int)*count);
if(out == NULL){
printf("malloc error !\n");
exit(1);
}
int j;
for(i = 0 , j = 0 ; i < len ; i++){
if(*(ary+i) < ave){
*(out+j)=*(ary+i);
printf("ary[%d]=%d " , j , *(out+j));
j++;
}
}
printf("\n");
for(i = 0 ; i < count ; i++){
printf("out[%d]=%d " , i , *(out+i));
}
free(ary);
free(out);
printf("\n");
}
void timu_8(){
int n;
int max;
for(n = 1 , max = (2*n-1)*(2*n) ; max < 10000 ; n++){
max = (2*n-1)*(2*n)*max;
}
printf("%d\n" , n);
}
void timu_9(){
int min = 300;
int max = 500;
int sum = 0;
int i;
for(i = min ; i <= max ; i++){
if((i%5)==0 && (i%7)==0){
printf("[%d]" , i);
sum += i;
}
}
printf(" sum = %d\n" , sum);
}
int gy(int a , int b){
int r;
while(r = a%b){
a = b;
b = r;
}
return b;
}
void timu_10(){
printf("%d | " , gy(2352 , 3696));
}
void timu_11(){
int count = 15;
srand(time(NULL));
int* ary = (int*)malloc(sizeof(int)*count);
if(ary == NULL){
printf("malloc error!\n");
exit(1);
}
int i;
for(i = 0 ; i < count ; i++){
*(ary+i) = (rand() % 89) + 10;
}
for(i = 0 ; i < count ; i++){
printf("ary[%d]=%d " , i , *(ary+i));
}
int j;
for(i = 0 ; i < count ; i++){
for(j=i+1 ; j < count ; j++){
if(*(ary+i) < *(ary+j)){
*(ary+i) = *(ary+i) ^ *(ary+j);
*(ary+j) = *(ary+i) ^ *(ary+j);
*(ary+i) = *(ary+i) ^ *(ary+j);
}
}
}
printf("\nsort:\n");
for(i = 0 ; i < count ; i++){
printf("ary[%d]=%d " , i , *(ary+i));
}
free(ary);
}
void echo_t(int i){
printf("\n%-4d:----------------------------:^^^:\n" , i);
}
int main(int argc , char ** argv){
echo_t(1);timu_1();
echo_t(2);timu_2();
echo_t(3);timu_3();
echo_t(4);timu_4();
echo_t(5);timu_5();
echo_t(6);timu_6();
echo_t(7);timu_7();
echo_t(8);timu_8();
echo_t(9);timu_9();
echo_t(10);timu_10();
echo_t(11);timu_11();
return 0;
}
热心网友
时间:2024-11-29 11:39
是用什么语言来编写的,要是用C#的倒还会追问c语言就可以谢谢