int []a=new int[10];//Java's Array
//int []a={6,3,5,9,1,0,4,2,7,8};
//int []a={1,2,3,5,0,4,6,7,8,9};
//int []a={9,8,7,6,5,4,3,2,1,0};
void setup(){
size(500,800);
for(int i=0;i<10;i++){//random choose number
a[i]=int(random(10));//亂數決定數字
}
textSize(36);
textAlign(LEFT,TOP);
showArray(y);
}
int y=0;
void showArray(int y){
for(int i=0;i<10;i++){
fill(255); rect(i*50, y, 50,50);
fill(0); text(a[i], i*50, y);
}
}
void draw(){
}
void mousePressed(){
for(int i=0; i<10-1;i++){
if(a[i] > a[i+1]){
int temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
fill(255,0,0,128); rect(i*50, y, 100,50);
}
}
y+=50;
showArray(y);
}
改良過的泡泡排序法
int []a=new int[10];//Java's Array
//int []a={6,3,5,9,1,0,4,2,7,8};
//int []a={1,2,3,5,0,4,6,7,8,9};
//int []a={9,8,7,6,5,4,3,2,1,0};
void setup(){
size(500,800);
for(int i=0;i<10;i++){//random choose number
a[i]=int(random(10));//亂數決定數字
}/// Java是用 int(3.14)會轉成3, C/C++ int b=3.14變3
textSize(32);
textAlign(LEFT,TOP);//文字改用左上角
showArray(y);
}
int y=0;
void showArray(int y){ //左上角
for(int i=0;i<10;i++){//rect(x,y, w,h)四邊形
fill(255); rect(i*50, y, 50,50);
fill(0); text(a[i], i*50, y);
} ///秀文字(字, x, y) 放哪裡 (左下角)
}
void draw(){
}
void mousePressed(){
for(int i=0; i<10-1;i++){
if(a[i] > a[i+1]){
int temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
fill(255,0,0,128); rect(i*50, y, 100,50);
}
}
y+=50;
showArray(y);
}
瘋狂程設進階題再複習
反序列印
#include<stdio.h> int main(){ int a[11],co=0,n; while(scanf("%d",&n)==1){ a[co++]=n; } for(int i=co-2;i>=0;i--){ printf("%d ",a[i]); } printf("\n"); }
大小寫轉換
#include<stdio.h> int main(){ char x; while(scanf("%c",&x)!=EOF){ if(x>='A'&&x<='Z') x+=32; else if(x>='a'&&x<='z') x-=32; printf("%c",x); } }
做出次方函數
#include<stdio.h> int MYPOWER(int x,int y){ int s=x; for(int i=1;i<y;i++){ s*=x; } return s; } int main(void) { int a,b; scanf("%d%d",&a,&b); printf("[%d]",MYPOWER(a,b)); return 0; }
做出Lyto color遊戲了
void setup() {
size(300, 500);
colorMode(HSB);
}
int N=5;
int H=255,S=255,B=255;
void draw() {
background(0);
int R=300/N,w=R/2;
for (int x=0; x<N; x++) {
for (int y=0; y<N; y++) {
fill(H,S,B);
ellipse(0+w+x*R,200+w+y+R,R,R);
}
}
}
void mousePressed(){
H+=16;
if(H>255)H=0;
}
沒有留言:
張貼留言