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

给定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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值