矩阵求和
- #include<iostream.h>
- void main()
- {
- int i,j,a[3][3],s1,s2;
- cout<<"请输入一个矩阵:";
- s1=s2=0;
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- cin>>a[i][j];
- }
- for(i=0;i<3;i++)
- {
- s1=s1+a[i][i];
- s2=s2+a[i][2-i];
- }
- cout<<"两条对角线的和为:"<<s1+s2<<endl;
- }
#include<iostream.h>
void main()
{
int i,j,a[3][3],s1,s2;
cout<<"请输入一个矩阵:";
s1=s2=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for(i=0;i<3;i++)
{
s1=s1+a[i][i];
s2=s2+a[i][2-i];
}
cout<<"两条对角线的和为:"<<s1+s2<<endl;
}
字符串插入
- # include <iostream>
- using namespace std;
- int main ()
- {
- char a[10]={"1623"},b[3]="ab";
- char max;
- int index=0,i=0,length_a=0,length_b=0,k=0;
- max=a[0];
- while (a[i]!='\0')
- {
- if(a[i]>max)
- {
- max=a[i];
- index=i;
- }
- i++;
- }
- while (a[length_a]!='\0')
- length_a++;
- while(b[length_b]!='\0')
- length_b++;
- while(length_a>index)
- {
- a[length_a+length_b]=a[length_a];
- length_a--;
- }
- index++;
- while (b[k]!='\0')
- {
- a[index]=b[k];
- k++;
- index++;
- }
- cout<<a;
- return 0;
- }