45、寫一函數,在一數組裏查找某個值
//寫一函數,在一數組裏查找某個值
#include<iostream>
#include<stdlib.h>
using namespace std;
int Search(int a[], int n, int key)
{
int i;
for(i=0; i<n; i++)
{
if(a[i]==key)
{
//cout << "a[" << i << "]=" << a[i] << endl;
return i;
}
//else return -1;
}
return -1;//key不在a[]中,返回-1,表示該函數失敗
}
int main()
{
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int key;
cin >> key;
cout << Search(a,10,key);
system("pause");
return 0;
}
46、編一程序,求兩個矩陣的乘積
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int m,n,a,b,i,j,k;
int **a1;
int **b1;
int **c1;
cout<<"輸入第一個矩陣的行數與列數:";
cin>>m>>n;
cout <<"輸入第二個矩陣的行數與列數:";
cin>>a>>b;
if(n != a)
{
cout<<"維數不同無法相乘"<<endl;
exit(-1);
}
//動態內存分配
a1 = new int *[m];
for (i=0; i<m; i++)
if ((a1[i] = new int [n])==NULL)
exit (0);
b1 = new int * [a];
for (i = 0;i<a;i++)
if ((b1[i] = new int [b])==NULL)
exit (0);
c1 = new int * [m];
for (i=0; i<m; i++)
if ((c1[i] = new int [b])==NULL)
exit (0);
//創建矩陣a1
cout<<"輸入第一個矩陣的元素:";
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>a1[i][j];
}
//創建矩陣b1
cout<<"輸入第二個矩陣的元素:";
for (i=0; i<a; i++)
{
for (j=0; j<b; j++)
cin>>b1[i][j];
}
//將c1矩陣所有元素賦值爲0
for (i=0; i<m; i++)
for (j=0; j<b; j++)
c1[i][j] = 0;
//計算c1
for (i=0; i<m; i++)
for(j=0; j<b; j++)
for (k=0; k<n; k++)
c1[i][j] += a1[i][k]*b1[k][j];
//輸出c1
cout<<"輸出結果爲"<<endl;
for (i=0; i<m; i++)
{
k = 0;
for (j=0; j<b; j++)
{
cout<<c1[i][j];
k = k+1;
if (k==b)
cout<<endl;
else cout<<"\t";
}
}
//釋放內存空間
for (i=0; i<m; i++)
delete[]a1[i];
delete []a1;
for (i=0; i<a; i++)
delete[]b1[i];
delete []b1;
for (i = 0;i<m;i++)
delete[]c1[i];
delete []c1;
system("pause");
return 0;
}
參考代碼:C++編程求兩個矩陣的乘積
47、計算某日是某年的第幾天
//計算某日是某年的第幾天
#include<iostream>
#include<stdlib.h>
using namespace std;
/*判斷是否爲閏年*/
bool isLeapYear(int n)
{
if(n%4 == 0) return true;
return false;
}
/*計算天數*/
int count_days(int year, int mon, int date)
{
int s=0;
switch(mon-1)
{
//case 12: s+=31;
case 11: s+= 30;
case 10: s+= 31;
case 9: s += 30;
case 8: s += 31;
case 7: s += 31;
case 6: s += 30;
case 5: s += 31;
case 4: s += 30;
case 3: s += 31;
case 2: s += 28;
case 1: s += 31;
//case 0: s += 0;
default:;
}
if(isLeapYear(year)) s++;
return s = s + date;
}
int main()
{
int year, mon, date;
cout << "請輸入年月日:";
cin >> year >> mon >> date;
cout << year << "年" << mon << "月" << date << "日是" << year << "的第";
cout <<count_days(year, mon, date) << "天" << endl;
system("pause");
return 0;
}
48、編寫一個幫助小學生學習加法的程序,隨機產生2個數,讓學生輸入答案
//編寫一個幫助小學生學習加法的程序,隨機產生2個數,讓學生輸入答案。
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
/* 獲得隨機數1-100 */
int get_randomNO()
{
//int a;
//a = rand()%6 + 1;
//cout << rand() << endl;
return rand()%100 + 1;
}
int main()
{
int a, b, c;
//unsigned seed;
//seed = time(0);
//srand(seed);
srand(time(0));
for(int i=0; i<5; i++)//練習次數
{
a = get_randomNO();
b = get_randomNO();
cout << a << " + " << b << " = ";
cin >> c;
if(c == a+b)
cout << "回答正確" << endl;
else
cout << "回答錯誤,正確答案是" << a+b << endl;
}
system("pause");
return 0;
}
49、從52個數裏選13個數
//從52個數裏選13個數
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int a[53]={0}, b[13]={0}, t, i, j;
for(int i=1; i<=52; i++)
a[i] = i;
srand(time(0));
/*獲得13個1-52之間不重複的數*/
for(int j=1; j<=13; j++)
{
a[j] = rand()%52 + 1;
for(int k=0; k<j; k++)
if(a[j] == a[k])//如果與前面的數重複,執行j-1,重搖
{
j--;
break;
}
}
/*排序*/
for(i=1; i<=13; i++)
for(j=1; j<=13-i; j++)
{
if(a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
for(i=1; i<=13; i++)
cout << a[i] << " ";
system("pause");
return 0;
}
題目來源:50道C/C++編程練習題及答案
總結
1、題45中return -1;作爲key不在數組中的返回標誌;參考:C++函數返回值介紹(含return 0 與 return 1 與 return -1介紹
2、C++二維數組指針