2009年8月4日 星期二

C++學習檔案

1.產生6位學生的成績 (宣告一整數陣列)
2.依序輸入學生成績
3.如果學生成績>=60,該變數存該整數
4.如果學生成績未滿60 該變數存0
5.第3跟4點必須使用 ? : 寫出
6.產生下表

例如 int stu[6];
依序輸入60,72,100,42,6
stu[6]內容為{60,72,100,0,0}

100:*
90~99:
80~89:
70~79:*
60~69:*
60-:**
average:(六位學生總和平均)


1.普通寫法

2.第3跟4點必須使用 ? : 寫出

進階版
3.可輸入要幾位學生(動態配置)


4.輸入一整數畫出n*n的矩形中間空 o
5.摩方陣
6.gcd

========================

#include <iostream>

using namespace std;


int main(){

int score[6],sc,sun;

string output[6];



for(int i=0;i<6;i++){

cin >> sc;

sun += sc;

if(sc >= 60){

score[i] = sc;

}else{

score[i] = 0;

}

if(sc == 100){

output[0] += "*" ;

}else if(sc >=90){

output[1] += "*";

}else if(sc >=80){

output[2] += "*";

}else if(sc >=70){

output[3] += "*";

}else if(sc >=60){

output[4] += "*";

}else{

output[5] += "*";

}

}

cout << "100 : " << output[0] << endl;

cout << "90~99 : " << output[1] << endl;

cout << "80~89 : " << output[2] << endl;

cout << "70~79 : " << output[3] << endl;

cout << "60~69 : " << output[4] << endl;

cout << "<60 : " << output[5] << endl;

cout << "average : " << sun/6 << endl;



system("pause");

return 0;

}


===========用 ? : 寫出========

#include <iostream>

using namespace std;

int main(){

int score[6],sc,sun = 0;

string output[6];

for(int i=0;i<6;i++){

cin >> sc;

sun += sc;

sc >= 60 ? score[i] = sc : score[i] = 0;

sc == 100 ? (output[0] += "*"): sc >=90 ? output[1] += "*" : sc >=80 ? output[2] += "*" : sc >=70 ? output[3] += "*":sc >=60 ? output[4] += "*" : output[5] += "*" ;

}

cout << "100 : " << output[0] << endl;

cout << "90~99 : " << output[1] << endl;

cout << "80~89 : " << output[2] << endl;

cout << "70~79 : " << output[3] << endl;

cout << "60~69 : " << output[4] << endl;

cout << "<60 : " << output[5] << endl;

cout << "average : " << sun/6 << endl;

system("pause");

return 0;

}

=============3.可輸入要幾位學生(動態配置)========

#include <iostream>

#include <string>


using namespace std;

int main(){

int n,sc,sun = 0;

string output[6];

cout << "how many students?" << endl;

cin >> n;

int *score = new int[n];

for(int i=0;i<n;i++){

cout << "plz input " << i+1 << " student's score" << endl;

cin >> sc;

sun += sc;

//sc >= 60 ? score[i] = sc : score[i] = 0;

score[i]=(sc>=60?sc:0);

sc == 100 ? (output[0] += "*"): sc >=90 ? output[1] += "*" : sc >=80 ? output[2] += "*" : sc >=70 ? output[3] += "*":sc >=60 ? output[4] += "*" : output[5] += "*" ;

}

cout << "100 : " << output[0] << endl;

cout << "90~99 : " << output[1] << endl;

cout << "80~89 : " << output[2] << endl;

cout << "70~79 : " << output[3] << endl;

cout << "60~69 : " << output[4] << endl;

cout << "<60 : " << output[5] << endl;

cout << "average : " << sun/n << endl;

system("pause");

return 0;

}


==============================

4.輸入一整數畫出n*n的矩形中間空

#include <iostream>

using namespace std;

int main(){

int n;

cout << "input num" << endl;

cin >> n;

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){

i==1 || i==n ? (cout << "*") : j==1 || j==n ? (cout << "*") : (cout << " ");

}

cout << endl;

}

system("pause");

return 0;

}

==============================
5.摩方陣

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main(){

int n,ns,x=0,y=0;

cout << "input num" << endl;

cin >> n;

cin >>x >> y;

ns = n*n;

int **s = new int*[n];



for(int i=0;i<n;i++) s[i] = new int[n];

for(int d=0;d<n;d++){

for(int f=0;f<n;f++){

s[d][f] = ns+1;

}

}





for(int j=1;j<=(ns); j++){

x=x%n;

y=y%n;

if(s[x][y]<ns){

x=x-2;

y=y-1;

}

if(x <= -1){

x=x+n;

}

if(y <= -1){

y=y+n;

}



s[x][y] = j;

x++;y++;

}

cout <<endl;

for(int a=0;a<n;a++){

int sum=0;

for(int b=0;b<n;b++){

sum += s[a][b];

cout << setw(4) << s[a][b] ;

}

cout << "=" << sum <<endl;

}

system("pause");

return 0;

}



==============================
6.gcd 找出最大公因數

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main(){

int n1,n2,min=0,gcd=0;

cout << "plz input two numbers" <<endl;

cin >> n1 >> n2;

min = n1<n2?n1:n2;

for(int i=1;i<=min;i++){

if(n1%i == 0 && n2%i == 0) gcd = i;

}

cout << "gcd = " << gcd << endl;



system("pause");

return 0;

}



1 則留言: