//求闭区间 [m , n] 的所有素数和并计数
//(1)
#include<stdio.h>
int main(){
int m,n;
int sum = 0;
int cnt = 0;
int i;
//scanf("%d %d",&m,&n);
m=2,n=4;
if(m==1) m==2;
for(i=m;i<=n;i++){
int isPrime = 1;
int k;
for(k=2;k<i-1;k++){
if(i%k == 0){
isPrime = 0;
break;
}
}
if(isPrime){
sum += i;
cnt++;
}
}
printf("%d %d\n",cnt,sum);
return 0;
}
//(2)
#include<stdio.h>
int isPrime(int i){
int ret=1;
int k;
for(k=2;k<i-1;k++){
if(i%k == 0){
ret = 0;
break;
}
}
return ret;
}
int main(){
int m,n;
int sum = 0;
int cnt = 0;
int i;
//scanf("%d %d",&m,&n);
m=2,n=4;
if(m==1) m==2;
for(i=m;i<=n;i++){
if(isPrime(i)==1){
sum += i;
cnt++;
}
}
printf("%d %d\n",cnt,sum);
return 0;
}
//求和:求出1-10;20-30;35-45的三个和
//(1)
#include<stdio.h>
int main()
{
int i;
int sum;
for(i=1,sum=0;i<=10;i++){
sum+=i;
}
printf("%d到%d的和是%d\n",1,10,sum);
for(i=20,sum=0;i<=30;i++){
sum+=i;
}
printf("%d到%d的和是%d\n",20,30,sum);
for(i=35,sum=0;i<=45;i++){
sum+=i;
}
printf("%d到%d的和是%d\n",35,45,sum);
}
//代码复制是程序质量不良的表现,下面用函数进行重新编程
//(2)
#include<stdio.h>
void sum(int begin,int end){
int i;
int sum=0;
for(i=begin;i<=end;i++){
sum+=i;
}
printf("%d和%d的和是%d\n",begin,end,sum);
}
int main()
{
sum(1,10);
sum(20,30);
sum(35,45);
return 0;
}
#include<stdio.h>
int max(int a,int b)
{
int ret;
if(a>b){
ret = a;
}else{
ret = b
}
return 0;
}
int main(){
int a,b,c;
a = 5;
b = 6;
c = max(10,12);
c = max(a,b);
c = max(c,23);
c = max(max(c,a),5);
printf("%d\n",max(a,b));
max(1,13);
return 0;
}