第一:例题
1.
# include <iostream>
using namespace std;
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
a[i]=i*2+2;
for(i=0;i<10;i++)
{
cout<<a[i]<<'\t';
if((i+1)%5==0)
cout<<endl;
}
return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
int i,math[40],n;
float aver = 0.0;
int unpassedcount = 0;
int highscorecount =0;
cout << "请输入学生人数: ";
cin >> n;
cout << "请输入成绩: ";
for(i=0;i<n;i++)
{
cin >> math[i];
if(math[i]>100||math[i]<0)
{
cout <<"请输入0到100:";
i--;
continue;
}
aver+=math[i];
}
aver/=n;
for(i=0;i<n;i++)
{
if(math[i]<60)unpassedcount++;
if(math[i]>=90)highscorecount++;
}
cout << "平均分为:"<<aver<<endl;
cout <<"90分以上人数为:"<<highscorecount<<endl;
cout << "不及格人数为:"<< unpassedcount<<endl;
return 0;
}
3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10],i,big;
cout <<"please input 10 numbers:\n";
for(i=0;i<10;i++)
cin>>a[i];
cout << setw(4)<<a[i];
cout <<endl;
big = a[0];
for(i=1;i<10;i++)
if(a[i]>big)
big=a[i];
cout <<"the big number is: "<<big<<endl;
return 0;
}
4.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10];
int i,j,t;
cout << "please input 10 number: \n";
for (i=0;i<10;i++)
cin >> a[i];
cout << "the number are: ";
for (i=0;i<10;i++)
cout <<setw(4)<<a[i];
cout << endl;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(a[j]>a[j+1])
{
t=a[j];a[j]=a[j+1];a[j+1]=t;
}
cout << "the sorted numbers are : ";
for (i=0;i<10;i++)
cout <<setw(4)<<a[i];
cout <<endl;
return 0;
}
4.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
int f[40]={1,1};
for(i=2;i<40;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<40;i++)
{
if(i%4==0)
cout <<endl;
cout << setw(12)<<f[i];
}
cout <<endl;
return 0;
}
5.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
int j;
int a[5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i%2==0)
a[i][j]=i*5+j+1;
else
a[i][4-j]=i*5+j+1;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout << setw(4)<<a[i][j];
cout << endl;
}
return 0;
}
6.
#include <iostream>
using namespace std;
int main()
{
char str[50];
cout<<"Please input strings: ";
cinget(str.50);
cout << "The string is: ";
cout << str << endl;
return 0;
}
7.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
cout<<"Please input strings: ";
cin.get(str,100);
cout << "字符串"<<str<<"的反向字符串为:";
for(int i=strlen(str)-1;i>=0;i--)
cout << str[i];
cout << endl;
return 0;
}
8.
#include <iostream>
using namespace std;
int main()
{
char s[]="This is C programming test.";
int i=0,pLen=0,maxpLen = 0, pSeat = 0;
while(s[i]!='\0')
{
while(s[i]!=' '&&s[i]!='\0')
{
pLen++;
i++;
}
if(pLen>maxpLen)
{
pSeat=i-pLen;
maxpLen=pLen;
}
while(s[i]==' ')
i++;
pLen=0;
}
cout <<"最长的单词 :";
for(i=0;i<maxpLen;i++)
cout << s[pSeat+i];
cout <<endl;
return 0;
}
9.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[50];
cout <<"Please input a string: ";
cin.get(str,50);
cout << "The length of string "<<str<<" is "<<strlen(str)<<endl;
return 0;
}
10.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[10];
cout <<"请输入字符串,知道输入hello后程序结束:"<<endl;
do{
cin >> str;
}while(strcmp(str,"hello")!=0);
return 0;
}
11.
#include <iostream>
using namespace std;
int main()
{
char str[50];
int len =0;
cout << "请输入一个字符串:";
cin.get(str,50);
while (str[len]!='\0')
{
len++;
}
cout <<"字符串 "<<str<<" 的长度为:"<<len <<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char str[50];
int len =0;
cout << "请输入一个字符串:";
cin.get(str,50);
while (str[len]!='\0')
{
len++;
}
cout <<"字符串 "<<str<<" 的长度为:"<<len <<endl;
return 0;
}
第二:习题
1.
#include <iostream>
using namespace std;
int main()
{
int arr[5],temp,i,j;
cout <<"Please enter five number: "<<endl;
for(i=0;i<5;i++)
cin>>arr[i];
for(j=0;j<5;j++)
for(i=4;i>=0;i--)
{
if(arr[i]<arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
for(i=0;i<5;i++)
{
cout <<arr[i];
}
cout << endl;
return 0;
}//冒泡排序法即可,也可对其优化,减少循环次数,比如已经放到正确位置的数字就可以不再比较以及移动。
2.
#include <iostream>
using namespace std;
int main()
{
int arr[2][3],i,j,m,n;
int temp=0;
cout << "请输入6个任意整数: "<<endl;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cin >> arr[i][j];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
if(arr[i][j]>temp)
{
temp=arr[i][j];
m=i;
n=j;
}
}
cout <<"该数组最大的数是: "<<arr[m][n]<<" 下标为: "<<m<<","<<n;
cout << endl;
return 0;
}
3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j=0;
int f[40]={1,1};
for(i=2;i<40;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<20;i++)
{
if(f[i]>100)
j++;
}
cout <<"前20个数中有 "<<j<<"个三位数,第16项为:"<<f[15];
cout <<endl;
return 0;
}
4.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch[50];
cout << "请输入一行文字:"<<endl;
cin.get(ch,50);
int i;
int up=0,low=0,blank=0,other=0,num=0;
for(i=0;ch[i]!='\0';i++)
{
if(isupper(ch[i]))
up++;
else if(islower(ch[i]))
low++;
else if (ch[i]==' ')
blank++;
else if(isdigit(ch[i]))
num++;
else
other++;
}
cout << "文字中有 "<<up<<" 个大写字母, "<<low<<"个小写字母,"<<blank<<"个空格,"
<<num<<"个数字以及 "<<other<<"个其他字符。"<<endl;
return 0;
}
5.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ch[50];
int i,num=0;
cout << "请输入一行字符串:"<<endl;
cin.get(ch,50);
cout <<"反向后的字符串为:";
for(i=strlen(ch)-1;i>=0;i--)
{
cout <<ch[i];
}
cout <<endl;
cout << "该字符串长度为:"<<strlen(ch)<<endl;
}
6.
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
char ch1[50],ch2[50];
int i,j=0;
cout << "请输入一行字符串:"<<endl;
cin.get(ch1,50);
strcpy(ch2,ch1);
for(i=0;ch2[i]!='\0';i++)
{
if(!isdigit(ch2[i]))
ch1[j++]=ch2[i];
}
ch1[j]='\0';
cout<<"改动后的字符串为:";
for(i=0;ch1[i]!='\0';i++)
cout <<ch1[i];
cout<<",长度为:"<<j<<endl;
return 0;
}
第三:矩阵
#include <iostream>
using namespace std;
int main()
{
int arr1[4][5],arr2[5][3],arr3[4][3];
int i,j,k;
cout<<"请输入4*5矩阵的数:"<<endl<<endl<<endl;
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
cout<<"请输入第#"<<i+1<<"行"<<"第#"<<j+1<<"个数:"<<endl;
cin>>arr1[i][j];
}
}
cout<<"请输入5*3矩阵的数:"<<endl<<endl<<endl;
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
cout<<"请输入第#"<<i+1<<"行"<<"第#"<<j+1<<"个数:"<<endl;
cin>>arr2[i][j];
}
}
cout<<"4*5的矩阵为:"<<endl;
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
cout<<arr1[i][j]<<" ";
cout<<endl;
}
cout<<"5*3的矩阵为:"<<endl;
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
cout<<arr2[i][j]<<" ";
cout<<endl;
}
for(i=0;i<4;i++)
for(k=0;k<3;k++)
{
j=0;
arr3[i][k]=arr1[i][j]*arr2[j][k]+arr1[i][j+1]*arr2[j+1][k]+arr1[i][j+2]*arr2[j+2][k];
}
cout<<"结果是:"<<endl;
for(i=0;i<4;i++){
for(j=0;j<3;j++)
cout <<arr3[i][j]<<" ";
cout <<endl;
}
return 0;
}
第四:
#include <iostream>
#include <cmath>
using namespace std;
#define S 100
void odd(int N,int index);
void uodd_1(int N);
void uodd_2(int N);
int arr[S][S];
int ox,oy;
int i,j;
int main ()
{
int N;
cout<<"请输入一个大于3的整数N: ";
cin >> N;
if(N%2==1)
{
ox=oy=0;
odd(N,0);
}
else
{
if(N%4==0)
uodd_1(N);
else uodd_2(N);
}
cout << "魔方阵:"<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
cout <<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
/* 奇数阶幻方
最经典的填法是罗伯特法(楼梯法),填写方法是这样:
把1(或最小的数)放在第一行正中;按以下规律排列剩下的n×n-1个数:
(1)每一个数放在前一个数的右上一格;
(2)如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列;
(3)如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行;
(4)如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在前一个数的下一行同一列的格内;
(5)如果这个数所要放的格已经有数填入,处理方法同(4)。
这种写法总是先向“右上”的方向,象是在爬楼梯。
三阶幻方:
8 1 6
3 5 7
4 9 2 */
void odd(int N,int index)
{
int x,y,temp;
y=N/2;
x=0;
for(temp=index+1;temp<=index+pow(N,2);temp++)
{
arr[ox+x][oy+y]=temp;
if(temp%N==0)x++;
else x--,y++;
x=(x%N+N)%N;
y=(y%N+N)%N;
}
}
/* 双偶阶幻方
n为偶数,且能被4整除 (n=4,8,12,16,20……;n=4k,k=1,2,3,4,5……)
互补:如果两个数字的和,等于幻方最大数和最小数的和,即 n*n+1,称为互补。*/
/* 四阶幻方
将数字从左到右、从上到下按顺序填写:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
将对角线上的数字,换成与它互补的数字。
这里,n×n+1 = 4×4+1 = 17;
把1换成17-1 = 16;
把6换成17-6 = 11;
把11换成17-11 = 6;
把16换成17-16 = 1;
……
换完后就是一个四阶幻方。
16* 2 3 13*
5 11* 10* 8
9 7* 6* 12
4* 14 15 1* */
void uodd_1(int N)
{
int temp=1,add;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
arr[i][j]=temp;
temp++;
}
add=N*N+1;
j=0;
for(i=0;i<N;i++)
{
arr[i][j]=add-arr[i][j];
j++;
}
j=N-1;
for(i=0;i<N;i++)
{
arr[i][j]=add-arr[i][j];
j--;
}
}
/* 单偶阶幻方
n为偶数,且不能被4整除 (n=6,10,14,18,22……;n=4k+2,k=1,2,3,4,5……)
以n=10为例。这时,k=2
(1) 把方阵分为A,B,C,D四个象限,这样每一个象限肯定是奇数阶。
用楼梯法,依次在A象限,D象限,B象限,C象限按奇数阶幻方的填法填数。
6阶幻方第一步:
8 1 6 | 26 19 24
3 5 7 | 21 23 25
4 9 2 | 22 27 20
-------------------------
35 28 33 | 17 10 15
30 32 34 | 12 14 16
31 36 29 | 13 18 11
(2) 在A象限的中间行、中间格开始,按自左向右的方向,标出k格。
A象限的其它行则标出最左边的k格。
将这些格,和C象限相对位置上的数,互换位置。
6阶幻方第二步:
35* 1 6 | 26 19 24
3 32* 7 | 21 23 25
31* 9 2 | 22 27 20
-------------------------
8* 28 33 | 17 10 15
30 5* 34 | 12 14 16
4* 36 29 | 13 18 11
(3) 在B象限任一行的中间格,自右向左,标出k-1列。
(注:6阶幻方由于k-1=0,所以不用再作B、D象限的数据交换)
将B象限标出的这些数,和D象限相对位置上的数进行交换,就形成幻方。
6阶幻方:
35 1 6 | 26 19* 24
3 32 7 | 21 23* 25
31 9 2 | 22 27* 20
-------------------------
8 28 33 | 17 10* 15
30 5 34 | 12 14* 16
4 36 29 | 13 18* 11 */
void uodd_2(int N)
{
int m,n,temp;
ox=oy=0;
odd(N/2,pow(N/2,2)*0);
ox=oy=N/2;
odd(N/2,pow(N/2,2)*1);
ox=0;
oy=N/2;
odd(N/2,pow(N/2,2)*2);
ox=N/2;
oy=0;
odd(N/2,pow(N/2,2)*3);
m=(N-2)/4;
for(i=0;i<N/2;i++)
{
for(j=0;j<m;j++)
{
n=(i==N/4)?N/4+j:j;
temp=arr[i][n];
arr[i][n]=arr[i+N/2][n];
arr[i+N/2][n]=temp;
}
for(j=0;j<m-1;j++)
{
n=N/2+N/4+j;
temp=arr[i][n];
arr[i][n]=arr[i+n/2][n];
arr[i+N/2][n]=temp;
}
}
}