1.
#include <stdio.h>
#include <stdbool.h>
#define ROW 3
#define COLUMN 4
bool search(int [ROW][COLUMN],int);
int main(){
int temperatures[ROW][COLUMN];
int *s = &temperatures[0][0];
printf("enter %d numbers:",ROW*COLUMN);
for(s;s<&temperatures[ROW-1][COLUMN-1];s++){
scanf("%d",s);
}
int key;
printf("enter a number:");
scanf("%d",&key);
printf("the result is %d",search(temperatures,key));
return 0;
}
bool search(int temperatures[ROW][COLUMN],int key){
int *p = &temperatures[0][0];
for(p;p<&temperatures[ROW-1][COLUMN-1];p++){
//printf("%d\n",*p);
if(*p == key){
return true;
}
}
return false;
}
2.
1.
#include <stdio.h>
#define N 100
int main(){
printf("Enter a message:");
char ch;
char str[N];
int i = 0;
while((ch = getchar())!='\n'){
if(i<N){
str[i++] = ch;
}else{
break;
}
}
printf("Reversal is:");
for(;i>=0;i--){
printf("%c",str[i]);
}
return 0;
}
2.
#include <stdio.h>
#define N 100
int main(){
printf("Enter a message:");
char ch;
char str[N];
char *p = str;
char *s = p;
while((ch = getchar())!='\n'){
if(p-s<N){
*p++ = ch;
*p = '\0';
}else{
break;
}
}
//puts(p);
//puts(str); //正序输出
printf("Reversal is:");
while(p--!=s)
printf("%c",*p);
return 0;
}
3.
#include <stdio.h>
void reverse_name(char *);
int main(){
char str[100];
printf("Enter a first and last name:");
char *name = gets(str);
reverse_name(name);
return 0;
}
void reverse_name(char *name){
char *s;
while(*name){
if(*name!= ' '){
s = name;
break;
}
name++;
}
while(*name++!=' ');
while(*name){
if(*name!=' ')
putchar(*name);
name++;
}
printf(", %c.",*s);
}
4.
#include <stdio.h>
int main(){
printf("Enter a date(mm/dd/yyyy):");
int day,month,year;
scanf("%d/%d/%d",&month,&day,&year);
char *mon[]={"January","February","March","April","May","June",
"July","August","September","October","November","December"};
printf("You entered the date %s %d, %d",mon[month-1],day,year);
return 0;
}
5.
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main(){
char *fly[8]={"8:00 a.m.","9:43 a.m.","11:19 a.m.","12:47 p.m.",
"2:00 p.m.","3:45 p.m.","7:00 p.m.","9:45 p.m."};
char *launch[8]={"10:16 a.m.","11:52 a.m.","1:31 p.m.","3:00 p.m.",
"4:08 p.m.","5:55 p.m.","9:20 p.m.","11:58 p.m."};
printf("Enter a 24-hour time:");
int hour,minute,i;
scanf("%d:%d",&hour,&minute);
int min = 60*hour + minute;
int time,str[8];
for(i=0;i<8;i++){
char *p;
strcpy(p,fly[i]);
//puts(p);
int left,right,j=0;
while(*p){
if(isdigit(*p)){
time = 10*j + (*p-48);
j = time;
}
p++;
}
//printf("%d\n",time);
right = time % 100;
left = time / 100;
//printf("%d %d\n",left,right);
if(i<4){
time = left*60 + right;
}else{
time = (left+12)*60 + right;
}
str[i] = abs(min - time);
}
int k,flag,des = 10000;
for(k=0;k<8;k++){
if(str[k]<des){
des = str[k];
flag = k;
}
}
printf("Closest departure time is %s, arriving at %s",fly[flag],launch[flag]);
return 0;
}
6.
#include <stdio.h>
#include<unistd.h>
//include<Windows.h>//for windows
#define MAX_DIGITS 10
#define CLEAR "cls"//"cls" for windows
#define Vel 7
#define Long 0
const int segments[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // code for 0
{0, 1, 1, 0, 0, 0, 0}, // code for 1
{1, 1, 0, 1, 1, 0, 1}, // code for 2
{1, 1, 1, 1, 0, 0, 1}, // code for 3
{0, 1, 1, 0, 0, 1, 1}, // code for 4
{1, 0, 1, 1, 0, 1, 1}, // code for 5
{1, 0, 1, 1, 1, 1, 1}, // code for 6
{1, 1, 1, 0, 0, 0, 0}, // code for 7
{1, 1, 1, 1, 1, 1, 1}, // code for 8
{1, 1, 1, 1, 0, 1, 1} // code for 9
};
char digits[3][MAX_DIGITS * 4+Long];
void clear_digits_array(void);//flush the canvas
void process_digits_array(int dight, int position,int time);//paint
void print_digits_array(void);//print
int display(int,int);//print an int
void countdown_display(int);//countdown based on display
void calculator_add(int,int);//adder based on display
int main(void)
{
int *time;
printf("Enter a positive integer: ");
int n;
scanf("%d",&n);
if(n<=0)
{
printf("ERROR INPUT!\n");
return -1;
}
printf("%d can be displayed as:\n",n);
//return display(n);
countdown_display(n);
int m;
printf("Enter another postive integer:");
scanf("%d",&m);
if(m<=0)
{
printf("ERROR INPUT!\n");
return -1;
}
calculator_add(n,m);
}
void clear_digits_array(void)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<MAX_DIGITS*4+Long;j++)
digits[i][j]=' ';
}
void process_digits_array(int digit, int position,int time)
{
int n = position * 4+time;
if (n+2>=MAX_DIGITS*4+Long)
{
n=n%(MAX_DIGITS*4+Long);
}
if (segments[digit][0])
digits[0][n + 1] = '_';
if (segments[digit][1])
digits[1][n + 2] = '|';
if (segments[digit][2])
digits[2][n + 2] = '|';
if (segments[digit][3])
digits[2][n + 1] = '_';
if (segments[digit][4])
digits[2][n] = '|';
if (segments[digit][5])
digits[1][n] = '|';
if (segments[digit][6])
digits[1][n + 1] = '_';
}
void print_digits_array(void)
{
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < MAX_DIGITS * 4+Long; j++)
{
printf("\033[40;31m%c\033[0m",digits[i][j]);
}
printf("\n");
}
}
int display(int n,int time)
{
clear_digits_array();
int nums[MAX_DIGITS];
int k;
for(k=0;k<MAX_DIGITS;k++)
nums[k]=-1;
int i=0;
while(n>0)
{
int r=n%10;
n/=10;
nums[i++]=r;
if(i>=MAX_DIGITS)
{
printf("TOO HUGE NUMBER!\n");
return -1;
}
}
int j=0;
for(i=MAX_DIGITS-1;i>=0;i--)
{
if(nums[i]>=0)
{
process_digits_array(nums[i], j++,time);
}
}
print_digits_array();
return 1;
}
void countdown_display(int n)
{
int i,time_cc=0;
system(CLEAR);
for(i=n;i>0;i--)
{
time_cc+=Vel;
display(i,time_cc);
sleep(1);
system(CLEAR);
}
}
void calculator_add(int n,int m)
{
int time=0;
system(CLEAR);
display(n,time);
printf("\n+\n");
display(m,time);
printf("\n=\n");
display(n+m,time);
}
7.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//#include<Windows.h>
#include<time.h>
#define N 30
#define DIRECTION 8
#define STATUS 3
#define EMPTY 0
#define POOR 1
#define RICH 2
int city[N][N];
const char statuses[STATUS]={'.','#','$'};
void randomly_set_city(void);
void print_city(void);
double same_neighbors_ratio(int,int);
void move(int,int);
void evolve(double);
int main(void)
{
srand(time(NULL));
printf("plz input a threshold:");
double th;
scanf("%lf",&th);
if(th>1.0 || th<0.0)
{
printf("ERROR INPUT\n");
return -1;
}
randomly_set_city();
int i;
for(i=0;i<10000;i++)
{
evolve(th);
if(i==0 || i%100==0)
{
system("cls");
//windows平台可能是System("cls");
print_city();
if(i==0)
sleep(10);//windows平台有变化,包括时间的单位。
else
sleep(1);
}
}
return 0;
}
void randomly_set_city()
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
city[i][j]=rand()%STATUS;
}
}
void print_city()
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(city[i][j]==RICH)
printf("\033[41;31m \033[0m");
if(city[i][j]==EMPTY)
printf("\033[47;37m \033[0m");
if(city[i][j]==POOR)
printf("\033[44;34m \033[0m");
}
printf("\n");
}
}
double same_neighbors_ratio(int i,int j)
{
int sn=0;
int nei=0;
//check all the directions
if(i-1>=0)
{
if(city[i-1][j]!=EMPTY)
{
nei++;
if(city[i][j]==city[i-1][j])
sn++;
}
}
if(i+1<N)
{
if(city[i+1][j]!=EMPTY)
{
nei++;
if(city[i][j]==city[i+1][j])
sn++;
}
}
if(j-1>=0)
{
if(city[i][j-1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i][j-1])
sn++;
}
}
if(j+1<N)
{
if(city[i][j+1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i][j+1])
sn++;
}
}
if(i+1<N && j+1<N)
{
if(city[i+1][j+1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i+1][j+1])
sn++;
}
}
if(i-1>=0 && j-1>=0)
{
if(city[i-1][j-1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i-1][j-1])
sn++;
}
}
if(i-1>0 && j+1<N)
{
if(city[i-1][j+1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i-1][j+1])
sn++;
}
}
if(i+1<N && j-1>=0)
{
if(city[i+1][j-1]!=EMPTY)
{
nei++;
if(city[i][j]==city[i+1][j-1])
sn++;
}
}
//printf("nei=%d,sn=%d\n",nei,sn);
return (double)sn/nei;
}
void move(int i,int j)
{
while(1)
{
int m=rand()%N;
int n=rand()%N;
if(city[m][n]==EMPTY)
{
//find an empty place and move to
city[m][n]=city[i][j];
//set the origin place empty
city[i][j]=EMPTY;
//end
break;
}
}
}
void evolve(double th)
{
while(1)
{
int i=rand()%N;
int j=rand()%N;
if(city[i][j]!=EMPTY)
{
//might move
double ratio=same_neighbors_ratio(i,j);
if(ratio<th)
{
//will move
move(i,j);
}
//end this time of evolution
break;
}
}
}
输入数字是0.3时,初始分布
最终分布
当参数调整为0.8时
起始分布
最终分布
迁移阈值比较小的时候(0.3),每次分布的改变比较小,最终会得到一个比较稳定的结果,穷人和富人的分布比较明显。迁移阈值比较大的时候,反而每次分布的改变都比较剧烈,最终却并没有显示出富人和穷人明显的分布。
北京房价的空间分布
可以看到实际的房价分布是十分复杂的,简单的模型是无法概括出完整的面貌的。首先房价最开始就不是随机分布的,有它的历史根源,以及真正的房价影响要素不仅仅取决于周围人的贫富,还与是不是学区房,交通是否便利,工作单位等等因素相关,所以scheeling可以做一些简单的分析,要更进一步的话还需要做许多更细致的分析。
网友评论