阅读理解题,各种字典序,字符串和字符数组库函数

来源:互联网 发布:友情链接源码 编辑:程序博客网 时间:2024/05/20 16:39

给定N个int24的正整数,求出所有能组成直角三角形的组合,并按组合后形成的字符串输出。

sprintf(字符数组,“输出模式”,对应的内容);

字符数组转换成字符串

#include <iostream>#include <string>using namespace std;int main(){char a[10]="aasdbbbba";string s(&a[0],&a[strlen(a)]);cout<<s<<endl;system("pause");}


字符串转为字符数组

#include <iostream>#include <string>using namespace std;int main(){string s="aaavvva";char a[10];strncpy(a,s.c_str(),s.length());for(int i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;system("pause");} 

字符数组转换成数字遇到非数字终止

#include<iostream>using namespace std;int main(){ char a[7]="213123"; int x; x=atoi(a);   cout<<x<<endl; getchar(); return 0;}

#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<string>using namespace std;int main(){    long long t,n,a[55],i,j,k,cnt;    string str[1000];    char s[200];     scanf("%lld",&t);    while (t--)    {          cnt=0;          scanf("%lld",&n);          for (i=0;i<n;i++)              scanf("%lld",&a[i]);          sort(a,a+n);          for (i=0;i<n-2;i++)          {              for (j=i+1;j<n-1;j++)              {                  long long tmp=a[i]*a[i]+a[j]*a[j];                  for (k=j+1;k<n;k++)                  {                      if (tmp<a[k]*a[k]) break;                      if (tmp==a[k]*a[k])                      {                         sprintf(s," {%lld %lld %lld}",a[i],a[k],a[j]);                         str[cnt++]=s;                      }                  }              }          }          sort(str,str+cnt);          if (cnt==0) printf("No Pythogorean triples found in the sequence.\n");          else          {              printf("Found Pythogorean triples: ");              for (i=0;i<cnt;i++)                  printf(" %s",str[i].c_str());              printf("\n");          }          sort(str,str+cnt);    }    return 0;} 


原创粉丝点击