C++程序設計譚浩強 第五章(數組)習題答案(部分有改進)

5.1篩選法求100以內素數

#include <iostream>
#include <iomanip>    //set系列控制符
using namespace std;
#include <math.h>
int main()
 {
	int i,j,n,a[101];
	for (i=1;i<=100;i++)
		a[i]=i;
  a[1]=0;

  for (i=2;i<sqrt(100);i++)  //非素數置零
    for (j=i+1;j<=100;j++)
       {if(a[i]!=0 && a[j]!=0)
	      if (a[j]%a[i]==0)
	       a[j]=0;  }

    cout<<endl;

    for (i=1,n=0;i<=100;i++)   //非零值輸出
     {if (a[i]!=0)
       {cout<<setw(5)<<a[i]<<" ";
        n++;}
      if(n==10)         //每十個數換行
        {cout<<endl;
   	     n=0;}
     }
	cout<<endl;
	return 0;
   } 

5.2選擇法對10個整數排序

#include <iostream>
using namespace std;
//#include <math.h>
int main()
  {int i,j,min,temp,a[11];
   cout<<"enter data:"<<endl;
   for (i=1;i<=10;i++)
   {cout<<"a["<<i<<"]=";
    cin>>a[i];                   //輸入10個數 
   }

   cout<<endl<<"The original numbers:"<<endl;
   for (i=1;i<=10;i++)
     cout<<a[i]<<" ";           // 輸出這10個數 
   cout<<endl;

/*對10個數從小到大排序*/
   for (i=1;i<=9;i++)            
     {
		min=i;
		for (j=i+1;j<=10;j++)
			if (a[min]>a[j])  min=j;
//將a[i]~a[10]中最小者與a[i] 對換
		temp=a[i]; 
		a[i]=a[min];
		a[min]=temp;

     }

// 輸出已排好序的10個數
   cout<<endl<<"The sorted numbers:"<<endl;
   for (i=1;i<=10;i++)           
     cout<<a[i]<<" ";
   cout<<endl;
   return 0;  
}  

5.3求一個3*3矩陣對角線元素之和

#include <iostream>
using namespace std;
int main()
 {int a[3][3],sum=0;
  int i,j;
  cout<<"enter data:"<<endl;;
   for (i=0;i<3;i++)
     for (j=0;j<3;j++)
       cin>>a[i][j];
   for (i=0;i<3;i++)
     sum=sum+a[i][i];
   cout<<"sum="<<sum<<endl;
   return 0;
  }

5.4有一個已排好的數組,今輸入一個數,按原來的排序插入數組中。

#include <iostream>
using namespace std;
int main()
 {
   int a[11]={1,4,6,9,13,16,19,28,40,100};
   int num,i,j;
   cout<<"array a:"<<endl;
   for (i=0;i<10;i++)
     cout<<a[i]<<" ";
   cout<<endl;

   cout<<"insert data:";
   cin>>num;
   if (num>a[9])
     a[10]=num;
   else
    {for (i=0;i<10;i++)
		{if (a[i]>num)
			{
				for (j=9;j>=i;j--)
				a[j+1]=a[j];
				a[i]=num;
				break;
			}
		}
     }
  cout<<"Now, array a:"<<endl;
  for (i=0;i<11;i++)
    cout<<a[i]<<" ";
  cout<<endl;
  return 0;
 }

5.5將一個數組中的值按逆序重新存放

#include <iostream>
using namespace std;
int main()
{ const int n=5;
  int a[n],i,temp;
  cout<<"enter array a:"<<endl; 
  for (i=0;i<n;i++)
    cin>>a[i];
  cout<<"array  :"<<endl;
  for (i=0;i<n;i++)
   cout<<a[i]<<" ";
  for (i=0;i<n/2;i++)            //循環的作用是將對稱的元素的值互換
    { temp=a[i];
      a[i]=a[n-i-1];
      a[n-i-1]=temp;
     }
  cout<<endl<<"Now  array :"<<endl;
  for (i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;
  return 0;
}   

5.6楊輝三角

#include <iostream>
#include <iomanip>
using namespace std;
int main()
 {const int n=11;
  int i,j,a[n][n];
  for (i=1;i<n;i++)
    {a[i][i]=1;
     a[i][1]=1;
    }
  for (i=3;i<n;i++)
    for (j=2;j<=i-1;j++)
       a[i][j]=a[i-1][j-1]+a[i-1][j];

  for (i=1;i<n;i++)
    {for (j=1;j<=i;j++)
       cout<<setw(5)<<a[i][j]<<" ";
     cout<<endl;
    }
  cout<<endl;
  return 0;
}

 

 

5.7找出一個二維數組中的鞍點,即元素該行最大,該列最小(可能沒有)

#include <iostream>
//#include <iomanip>
using namespace std;
int main()
{
	const int n=4,m=5;  //假設數組四行五列
	int i,j,a[n][m],max,maxj;
	bool flag;
	for (i=0;i<n;i++)
		for (j=0;j<m;j++)
			cin>>a[i][j];

	for (i=0;i<n;i++)
	{
		//找出第i行最大的數
		max=a[i][0];maxj=0;
		for (j=0;j<m;j++)
			if(a[i][j]>max)
			{max=a[i][j];maxj=j;}
		//假設是鞍點
		flag=true;
		//行最大數與列元素比,max不是最小,則不是鞍點
		for (int k=0;k<n;k++)
			if (max>a[k][maxj])
			{flag=false;continue;}

		if(flag)
		{
			cout<<"a["<<i<<"]["<<j<<"]="<<max<<endl;
			break;
		}
	}
	//如果flag爲假則鞍點不存在
	if(!flag)
		cout<<"It does not exist!"<<endl;
	return 0;
}

5.8有15個數按由大到小的順序存放在一個數組中,輸入一個數,折半查找法找出該數是第幾個元素。

     如果數不存在則打印無此數

#include <iostream>
using namespace std;
int main()
{ const int n=15;
  int i,number,top,bott,mid,loca,a[n];
  bool flag=true,sign;
  char c;

//輸入15個數按順序 由大到小
  cout<<"enter data:"<<endl;
  cin>>a[0];
  i=1;
  while(i<n)
   {cin>>a[i];
    if (a[i]<=a[i-1])
      i++;
    else
      cout<<"enter this data again:";
   }
  cout<<endl;

//輸出數
  for (i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;

//給這個程序一個可執行的循環  無限次執行
  while(flag)
    {
	 cout<<"input number to look for:";
     cin>>number;
     sign=false;
     top=0;            //top是查找區間的起始位置
     bott=n-1;         //bott是查找區間的最末位置

     if ((number<a[n-1])||(number>a[0]))  //要查的數不在查找區間內
       loca=-1;        // 不存在
	 else
	 {
		while ((!sign) && (top<=bott))
		{mid=(bott+top)/2;
			if (number==a[mid])
			{
				loca=mid;
				cout<<"Find "<<number<<", its position is "<<loca+1<<endl;
				sign=true;
			 }
			else if (number<a[mid])
			 top=mid+1;
			else
			  bott=mid-1;
       }
	 }
//沒找到
     if(!sign||loca==-1)
       cout<<number<<" has not found."<<endl;

	 //循環
     cout<<"continu or not(Y/N)?";
     cin>>c;
     if (c=='N'||c=='n')
		 flag=false;
    }

   return 0;
 } 

5.9有一篇文章,共有三行文字,每行80字符。統計英文大寫字母,小寫字母,數字,空格,其他的個數

#include <iostream>
using namespace std;
int main()
{int i,j,upper,lower,digit,space,other;
 char text[3][80];
 upper=lower=digit=space=other=0;
 for (i=0;i<3;i++)
   {cout<<"please input line "<<i+1<<endl;
    gets(text[i]);
    for (j=0;j<80 && text[i][j]!='\0';j++)
      {if (text[i][j]>='A'&& text[i][j]<='Z')
         upper++;
       else if (text[i][j]>='a' && text[i][j]<='z')
         lower++;
       else if (text[i][j]>='0' && text[i][j]<='9')
         digit++;
       else if (text[i][j]==' ')
         space++;
       else
         other++;
     }
   }
   cout<<"upper case:"<<upper<<endl;
   cout<<"lower case:"<<lower<<endl;
   cout<<"digit     :"<<digit<<endl;
   cout<<"space     :"<<space<<endl;
   cout<<"other     :"<<other<<endl;
   return 0;
} 

5.10打印以下圖案字符數組方法,string方法

#include <iostream>
using namespace std;
int main()
{ char a[5]={'*','*','*','*','*'};
  int i,j,k;
  char space=' ';
  for (i=0;i<5;i++)              // 輸出5行 
   { cout<<endl;                 // 輸出每行前先換行 
     cout<<"    ";               // 每行前面留4個空格 
     for (j=1;j<=i;j++)
       cout<<space;              // 每行再留一個空格 
     for (k=0;k<5;k++)
       cout<<a[k];               // 每行輸出5個*號
	}   
  cout<<endl;
   return 0;
} 
#include <iostream>
#include <string>
using namespace std;
int main()
{ string stars="*****";
  int i,j;
  for (i=0;i<5;i++)              // 輸出5行 
   { cout<<"    ";               // 每行前面留4個空格
     for (j=1;j<=i;j++)
       cout<<" ";               // 每行再插入i個空格    
     cout<<stars<<endl;         // 輸出5個*號
  }   
  return 0;
} 

5.11電文 。第一個字母變成第二十六個字母。

編程將密碼譯回原文

#include <iostream>
using namespace std;
int main()
 {int j,n;
  char ch[80],tran[80];
  cout<<"input cipher code:";
  gets(ch);
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (ch[j]!='\0')
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      tran[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
      tran[j]=219-ch[j];
    else
      tran[j]=ch[j];
    j++;
  }
  n=j;
  cout<<"original text:";
  for (j=0;j<n;j++)
    putchar(tran[j]);
  cout<<endl;
  return 0;
 }
#include <iostream>
#include <string>
using namespace std;
int main()
 {int j;
  string ch="I will visit China next week.",tran;
  tran=ch;
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (j<=ch.size())
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      tran[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
	  tran[j]=219-ch[j];
    else
	  tran[j]=ch[j];
    j++;
  }
  cout<<"original text:";
  cout<<tran<<endl;
  return 0;
 }
#include <iostream>
#include <string>
using namespace std;
int main()
 {int j;
  string ch="                              ";
  char *p=&ch[0];            //定義字符指針並使之指向ch的首字符
  cout<<"input cipher code:";
  gets(p);                  //從鍵盤讀入一行字符
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (j<=ch.size())
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      ch[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
	  ch[j]=219-ch[j];
    j++;
  }
  cout<<"original text:";
  cout<<ch<<endl;
  return 0;
 }

5.12將兩個字符串連接起來,取代第一個字符串

5.12.1 用字符數組,不用strcat

#include <iostream>
#include <string>
using namespace std;
int main()
{char s1[80],s2[40];
  int i=0,j=0;
  cout<<"input string1:";
  cin>>s1;
  cout<<"input string2:";
  cin>>s2;
  while (s1[i]!='\0')
    i++;
  while(s2[j]!='\0')
    s1[i++]=s2[j++];
  s1[i]='\0';
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

5.12.2用strcat函數

#include <iostream>
using namespace std;
int main()
{char s1[80],s2[40];
  cout<<"input string1:";
  cin>>s1;
  cout<<"input string2:";
  cin>>s2;
  strcat(s1,s2);
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

5.12.3用string方法定義字符串變量

#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="week",s2="end";
  cout<<"s1="<<s1<<endl;
  cout<<"s2="<<s2<<endl;
  s1=s1+s2;
  cout<<"The new string is:"<<s1<<endl;
  return 0;
}

5.13輸入n個字符串,按字母由小到大排列輸出

#include <iostream>
#include <string>
using namespace std;
int main()
{ const int n=5;
  int i,j;
  string str[n],temp;
  cout<<"please input strings:"<<endl;
  for(i=0;i<n;i++)
	  cin>>str[i];
  for(i=0;i<n-1;i++)
    for(j=0;j<n-i-1;j++)
      if(str[j]>str[j+1])
	  {temp=str[j];str[j]=str[j+1];str[j+1]=temp;}
  cout<<endl<<"sorted strings:"<<endl;
  for(i=0;i<n;i++)
	  cout<<str[i]<<endl;
  return 0;
}

5.14輸入n個字符串,把其中以A打頭的字符串取出

#include<iostream>
#include<string>
using namespace std;

int main()
{
	const int n=5;   //設5個字符串
	string str[100];
	int i;
	cout<<"please input code"<<endl;

	for(i=0;i<n;i++)
		cin>>str[i];  //給str賦值
	for(i=0;i<n;i++)
		if(str[i][0]=='A') //一維字符串數組 可以這樣調用其中的字符
			cout<<"結果:"<<str[i]<<endl;
	return 0;
}

5.15輸入一個字符串,把其中字符按逆序輸出。

5.15.1字符數組

#include <iostream>
using namespace std;
int main()
{ const int n=10;  //設一共10個字符
  int i;
  char a[n],temp;
  cout<<"please input a string:";
  for(i=0;i<n;i++)
	  cin>>a[i];
  for(i=0;i<n/2;i++)
  {temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}
  for(i=0;i<n;i++)
	  cout<<a[i];
  cout<<endl;
  return 0;
}

5.15.2用string

#include <iostream>
#include <string>
using namespace std;
int main()
{ 
	string a;
	int i,n;
	char temp;
	cout<<"please input a string:";
	cin>>a;
	n=a.size();
	for(i=0;i<n/2;i++)
	{
		temp=a[i];
		a[i]=a[n-i-1];
		a[n-i-1]=temp;
	}
	cout<<a<<endl;
	return 0;
}

5.16輸入10個學生的姓名、學號和成績,將其中不及格者的姓名、學號和成績輸出

#include<iostream>
#include<string>
using namespace std;

const int n=10;
string name[n];
int num[n],score[n];

int main()
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<"input name,number and score of student "<<i+1<<" : " <<endl;
		cin>>name[i]>>num[i]>>score[i];
	}

	cout<<endl<<"The list of failed:"<<endl;
	for(i=0;i<n;i++)
		if(score[i]<60)
			cout<<name[i]<<"  "<<num[i]<<"  "<<score[i]<<endl;
	return 0;
}

阿這        錯的地方我改過了

這答案應該都是對的

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章