C++

氣泡排序與搜尋

<找出眾數> - 沒有寫完, 剩下的請同學練習補上

#include <iostream>

#include <cstdlib>

#include <time.h>


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {

int a[50],sum[50][2],i,j,temp,max0,max1;

srand((unsigned)time(NULL));

for(i=0;i<50;i++)

{

a[i]=rand()%50+1;


}

//排序

for(i=0;i<50;i++)

{

for(j=0;j<50;j++)

{

if(a[j]>a[j+1])

{

temp=a[j+1];

a[j+1]=a[j];

a[j]=temp;

}

}

}

//排序結果

for(i=0;i<50;i++)

{

std::cout << a[i] << " , ";

}

std::cout << "\n";

//找眾數

// initialization

for(i=0;i<50;i++)

{

for(j=0;j<=1;j++)

{

sum[i][j]=0;

}

}

for(i=0;i<50;i++)

{

for(j=0;j<50;j++)

{

if(sum[j][0]==0)

{

sum[j][0]=a[i];

sum[j][1]+=1 ;

break;

}

else if(sum[j][0]==a[i])

{

sum[j][1]+=1 ;

break;

}

}

}

max0=0;

max1=0;

for(i=0;i<50;i++)

{

if(sum[i][1]>max1)

{

max0=i;

max1=sum[i][1];

}

}

//眾數

std::cout << "the most amount is " << sum[max0][0] << " , the amount is ." << max1 ;

return 0;

}

輸入一串字串, 不超過10個字。判斷第一個字母是否為大寫英文字,若是輸出"YES",若不是輸出"NO"。

#include <iostream>

#include <string>


using namespace std;


int main()

{

char str[10];

while(1)

{

cout << "input string: ";

cin >> str;

if(str[0]>=65 && str[0]<=90)

{

cout << "YES !" << endl ;

}

else

{

cout << "NO !" << endl ;

}

}

}

kbhit()函數

#include <iostream>

#include <conio.h>

using namespace std;

int main()

{

/* Display message until key is pressed */

while (!kbhit())

{

cout << "Hit me! ";

}

cout << "/nkey struck was " << getch() << endl;

return 0;

}