#include<stdio.h>
#include<stdlib.h>
#define N 5
int main(){
int x,n;
srand(time(0));//time 返回一个值,是格林尼治时间1900年1月1日距现在的秒数(可保证不同的数)
for(n=1;n<=N;n++){
x=1+rand()%30;//rand,取time前的任一个数 ;31即可取0~31的随机数
printf("%3d",x);
}
printf("\n");
return 0;
}
![]()
#include<stdio.h>
#include<stdlib.h>
int rad();
int main(){
int x=3,i,y,ans;
printf("Guess which day is your lucky day!\n");
printf("Begins! You have %d chances,let us guess!(1~31)",x);
y=rad();
for(i=1;i<=x;i++)
{
scanf("%d",&ans);
if(ans==y)
{
printf("Yep!U R right!\n");
exit(0);
}
else if(ans>y)
{
printf("oH~ U R wrong!The right one is in front of your ans!\n");
printf("Guess it again!(1~31)");
}
else
{
printf("oH~ U R wrong!The right one is behind your ans!\n");
printf("Guess it again!(1~31)");
}
}
printf("oh,no~Your chances are used off!Let me tell you: it is %d!\n",y);
return 0;
}
int rad(){
int x;
srand(time(0));
x=1+rand()%30;
return x;
}
![]()
#include<stdio.h>
#include<math.h>
int main(){
long outcome,x,number;
int y,count,m;
printf("Enter a number:");
while(scanf("%ld",&number)){
count=1;
outcome=0;
for(;number!=0;)
{
x=number%10;
if(x%2!=0)
{
m=1;
for(y=1;y<count;y++)
{
m*=10;
}
outcome+=x*m;
count++;
}
number/=10;
}
printf("\nnew number is:%ld\n\n",outcome);
printf("Enter a number:");
}
return 0;
}
![]()
#include <math.h>
#include <stdio.h>
double solve1(double a, double b, double c);
double solve2(double a, double b, double c);
int main()
{
double a, b, c,x1,x2;
printf("Enter a, b, c: ");
while(scanf("%lf%lf%lf", &a, &b, &c) != EOF)
{
x1= solve1(a, b, c);
x2= solve2(a,b,c);
printf("x1=%f,x2=%f\n",x1,x2);
printf("Enter a, b, c: ");
}
return 0;
}
double solve1(double a, double b, double c) {
double x1, x2;
double delta, real, imag;
if(a == 0)
printf("not quadratic equation.\n");
else {
delta = b*b - 4*a*c;
if(delta >= 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
return x1;
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", real, imag, real, imag);
exit(0);
}
}
}
double solve2(double a, double b, double c) {
double x1, x2;
double delta, real, imag;
if(a == 0)
printf("not quadratic equation.\n");
else {
delta = b*b - 4*a*c;
if(delta >= 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
return x2;
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", real, imag, real, imag);
exit(0);
}
}
}
task5
#include <stdio.h>
double fun(int n);
int main() {
int n;
double s;
printf("Enter n(1~10): ");
while(scanf("%d", &n) != EOF) {
s = fun(n);
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
double fun(int n){
int i,j,a,sign;
double part,ans;
sign=1;
ans=0.0;
for(i=1;i<=n;i++)
{
a=1;
for(j=1;j<=i;j++)
{
a*=j;
}
part=sign*((double)1.0/a);
sign=(-1)*sign;
ans+=part;
}
return ans;
}
![]()
#include<stdio.h>
#include<math.h>
int isprime(int n);//insert 可改成插入或者覆盖模式
int main()
{
int i;
int count=0;
for(i=101;i<=200;i++)
{
if(isprime(i))
{
if(count%5==0&&count!=0)
{
printf("\n");
}
printf("%4d",i);//不知道为什么输出前面会有一个空格
count++;
}
}
printf("\n 100~200的素数个数为%d",count);
return 0;
}
int isprime(int n){
int k;
for(k=2;k<=sqrt(n);k++)
if(n%k==0)
return 0;
return 1;
}
![]()