2020年3月26日 星期四

複習程設GOGOGO

了解除餘數在C/C++裡的概念
#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    printf("%d   %d   %d   %d   %d",n/10000,n/1000%10,n/100%10,n%1000/10%10,n%10);
    }

了解質數的概念
#include <iostream>
using namespace std;
int prime(int n){
    for(int i=2;i<n;i++){
      if(n%i==0){
       return 0;
       }
    }
   return 1;
}

int main(){
  int n;cin>>n;
  cout<<"["<<prime(n)<<"]";
  return 0;
}
先繞過陣列講解字串的概念
#include<stdio.h>
int main(){
   int a=0,b=0,c=0,d=0,e=0,f=0;
   char n;
   while((n=getchar())!='\n'){
       if(n=='1')a++;
       if(n=='2')b++;
       if(n=='3')c++;
       if(n=='4')d++;
       if(n=='5')e++;
       if(n=='6')f++;
       }
       printf("1:%d\n",a);
       printf("2:%d\n",b);
       printf("3:%d\n",c);
       printf("4:%d\n",d);
       printf("5:%d\n",e);
       printf("6:%d\n",f);
      }
做一個球體往下
int []x=new int[1000];
int []y=new int[1000];
color [] c = new color[1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
     background(255);
     for(int i=0;i<n;i++){
         fill(c[i]);
         circle(x[i],y[i],30);
         y[i]-=2;
     }
}
void mousePressed(){
     x[n]=mouseX;
     y[n]=mouseY;
     c[n]=color(random(255),random(255),random(255));
     n++;
}

點一個球體往上



int []x=new int[1000];
int []y=new int[1000];
int []r=new int[1000];
color [] c = new color[1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
     background(255);
     for(int i=0;i<n;i++){
         fill(c[i]);
         circle(x[i],y[i],r[i]);
         y[i]-=2;
     }
}
void mousePressed(){
     x[n]=mouseX;
     y[n]=mouseY;
     r[n]=10;
     c[n]=color(random(255),random(255),random(255));
     n++;
}
改變球體的大小
int []x=new int[1000];
int []y=new int[1000];
int []r=new int[1000];
color [] c = new color[1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
     background(255);
     for(int i=0;i<n;i++){
         fill(c[i]);
         circle(x[i],y[i],r[i]);
         y[i]-=2;
     }
     if(mousePressed)r[n-1]++;
}
void mousePressed(){
     x[n]=mouseX;
     y[n]=mouseY;
     r[n]=10;
     c[n]=color(random(255),random(255),random(255));
     n++;
}

學會用類別class
提取物件
Bubble [] b=new Bubble[1000];
int n=0;
void setup() {
  size(400, 400);
}
void draw() {
  background(255);
  for (int i=0; i<n; i++) {      
    b[i].drawAndUpdate();
  }
  if (mousePressed)b[n-1].r++;
}
void mousePressed() {
  b[n]=new Bubble(mouseX, mouseY, 10);
  n++;
}

class Bubble {
  int x;
  int y;
  int r;
  color c;
  Bubble(int _x,int _y,int _r){
    x=_x; y=_y; r=_r;
    c=color(random(255),random(255),random(255));
  }
  void drawAndUpdate(){
    fill(c);
    circle(x,y,r);
    y--;
  }
}
了解矩陣的概念
按按看
int [][]table=new int[6][6];
void setup() {
  size(300, 300);
}
void draw(){
  background(128);
  for (int i=0; i<6; i++) {
    for (int j=0; j<6; j++) {
      if (table[i][j]==1)fill(255, 0, 0);
      else fill(128);
      rect( j*50, i*50, 50, 50);
    }
  }
}
void mousePressed(){
     int x = (mouseX)/50;
     int y = (mouseY)/50;
     table[y][x]=1;
}



沒有留言:

張貼留言