1.1 编写Fun()函数,将整数分解为13x+11y的形式,输出格式为: 126,13*8,11*2
void fun(int x)
{
int num1=13;
int num2=11;
int oper1=x/num1;
int oper2=x%num1;
//cout<<oper1<<" "<<oper2<<endl;
while(oper2%num2!=0)
{
oper1--;
oper2+=num1;
//cout<<oper1<<" "<<oper2<<endl;
}
int oper3=oper2/num2;
cout<<x<<","<<num1<<"*"<<oper1<<","<<num2<<"*"<<oper3<<endl;
}
int main()
{
int x;
cout<<"please enter a number:"<<endl;
cin>>x;
fun(x);
return 0;
}
1.2 将一个字符数组中ASCII码值大于等于‘d’的字符提取到字符串开头,结尾加\0.
void getChar(char s[],int length)
{
string s2;
for(int i=0;i<length;i++)
{
if(s[i]>='d')
s2+=s[i];
}
s2+='\0';
cout<<s2<<endl;
}
int main()
{
char p[]="abcdefg";
int length=sizeof(p);
cout<<length<<endl;
getChar(p,length);
return 0;
}
1.3 查找字符串中ASCII码最大的元素,并把它换到开头,在此之前的元素后移。
void maxASCII(string s)
{
int length=s.length();
char max_char=s[0];
int max=0;
for(int i=1;i<length;i++)
{
if(s[i]>max_char)
{
max_char=s[i];
max=i;
}
}
for(int j=max;j>0;j--)
{
s[j]=s[j-1];
}
s[0]=max_char;
cout<<s<<endl;
}
int main()
{
string s;
cin>>s;
maxASCII(s);
return 0;
}