当前位置: 首页 > news >正文

网站建设方案页面设计分析站长工具排行榜

网站建设方案页面设计分析,站长工具排行榜,网站日志分析有什么用,移动网站开发教程下载讲在前面:1.本人正在逐步学习C,代码中难免有C和C(向下兼容)混用情况。2.算法题目来自蓝蓝知识星球,没有对应的判决系统,运行到判决系统可以会有部分案例不能通过。 求素数 暴力求解(1 - n试探…

讲在前面:1.本人正在逐步学习C++,代码中难免有C和C++(向下兼容)混用情况。2.算法题目来自蓝蓝知识星球,没有对应的判决系统,运行到判决系统可以会有部分案例不能通过。

求素数

暴力求解(1 - n试探)


#include <iostream> 
using namespace std;
int main(){int num, i;cin >> num ;for(i = 2; i < num ;i ++){if ( num % i == 0) {cout << "不是素数" << endl;break;} }if ( i == num ) cout << "是素数" << endl;return 0;
}

优化算法——奇数优化(由于所有素数除了2,都是从奇数中出现。所以我们可以先去掉偶数,再筛选,这样的效率就会更高一点)
一个数如果不是素数则有两种情况:1.是偶数 2.可以被[3,n-1]之内的某些奇数整除

//求素数
#include <iostream> 
using namespace std;
int main(){int num, i, flag = 1;cin >> num ;if ( num % 2 == 0) flag = 0; //不是素数 for(i = 3; i < num ;i += 2 ){if ( num % i == 0) {flag = 0;break;} }if ( flag == 1 ) cout << "是素数" << endl;else cout << "不是素数" << endl;return 0;
}

优化算法——平方根优化
每一个数的因数都是成对出现的 且以其平方根为分界线。例如:30 (1,30),(2,15),(3,10),(5,6)以sqrt(30)为分界线。

//求素数
#include <iostream> 
#include <cmath>
using namespace std;
int main(){int num, i;cin >> num ;for(i = 2; i < (int) sqrt( num ) + 1 ;i ++){if ( num % i == 0) {cout << "不是素数" << endl;break;} }if ( i == num ) cout << "是素数" << endl;return 0;
}

求完全数

暴力求解

//求完全数 
#include <iostream> 
#include <cmath>
using namespace std;
bool perfect( int num );
int main(){int num, i;cin >> num ;for(i = 2; i <= num ;i ++){if ( perfect( i ) )  cout << i << endl;}return 0;
}
bool perfect( int num ){int sum = 1;for ( int i = 2; i < num; i ++){if ( num % i == 0 ) sum += i; }if ( sum == num ) return true;return false;
}

优化:利用欧拉公式求完全数
欧拉公式求素数

//求完全数
#include <iostream> 
#include <cmath>
using namespace std;
bool is_prime( int num ); //求素数部分 可以参照题目一的多种方法
int main(){int num,ans = 0;cin >> num;for(int i = 2; i <= num && pow(2, i - 1) * (pow(2 , i)-1) <= num; i++)){if ( is_prime( i ) && is_prime( pow(2,i)-1)) ans ++;}cout << ans << endl;
}
bool is_prime( int num ){int i = 2;for( ; i < (int) sqrt( num ) + 1 ;i ++){if ( num % i == 0) return false;	}if ( i == num ) return true;
} 

统计字符串字符

给定字符串ASCII码是【1,127】,统计其中不同字符个数。例如:aaabbbc 输出3

利用桶的思想:
开辟一个大小为 128 的数组初始化全为0,若有对应字母出现则在桶序数上+1,最后统计不为0的个数就是不同的字母个数。

#include <iostream> 
#include <string>
using namespace std;
int main(){string str;int cnt[128] = {0}, ans = 0;cin >> str;for(int i = 0; i < str.length() ; i++){cnt[ (int) str[i]] += 1;}for(int i = 0; i < 128 ; i++){if ( cnt[i] != 0)  ans++;}cout << ans << endl;
}

求最大公约数和最小公倍数

两个数A B的minmultiple(最小公倍数)* maxdivisor(最大公约数) = A * B
两个数最大公约数三种计算方法


#include <iostream> 
#include <algorithm>
using namespace std;
//采用辗转相除法(欧几里得)
int GetMaxDivisor(int a,int b){if(b == 0)return a;return GetMaxDivisor(b, a%b);
}int main()
{int a, b;cin >> a >> b;int md = GetMaxDivisor(a,b);int mm = a * b / md;cout << "最大公约数:" << md << "   最小公倍数:" << mm;return 0;
}

小球运动

小球从指定高度落下,每次弹起高度减半,问小球从落下到静止经过的距离(float h < 10-6|| double h<10-15认为弹起高度为0 达到静止)

#include <iostream> 
using namespace std;
int main()
{double height, sum ;cin >> height;// 从上向下 初始距离height sum = height;while( height ){// 从弹起到落下经过的距离是 弹起高度的2倍 即是上次弹起的高度值 sum += height;height /= 2;}cout << sum << endl;return 0;
}

答案正确

得到“答案正确”的条件是:

1.字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
2.任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
分析

1.字符串中必须有’P’,‘T’,‘A’;
2.‘P’必须在‘T’之前,A穿插其中;
3.P前面的A的个数 乘以 P和T之间的A的个数 等于 T后面的A的个数——成立条件2中 可以推出
#include <iostream> 
#include <string>
using namespace std;
int main(){string str;int num = 0;cin >> num;while(num){cin >> str;int num1 = 0, num2 = 0, num3 = 0;int j = 0, k = 0;for(int i = 0; i < str.length(); i++){if( str[i] == 'P'){j = i; break;}else if( str[i] == 'A' ) num1 += 1;}for(j+1 ; j < str.length(); j++){if( str[j] == 'T'){k = j; break;}else if( str[j] == 'A') num2 += 1;}for(k+1 ; k < str.length(); k++){if( str[k] == 'A' ) num3 += 1;}if( num1 * num2 == num3 && (num1 + num2 + num3 + 2) == str.length() && str.length() > 2) cout << "YES" << endl;else cout << "NO" << endl;num --;}return 0;
}

计算字符个数

由于主串和目标字符都会有大小写的情况出现,所以代码中一致转换为小写。

#include <iostream> 
#include <string>
#include <algorithm>
using namespace std;
int main(){string str; char ch;int ans=0;cin >> str >> ch;//调用算法库中函数将 str全部转换为小写字母 transform(str.begin(),str.end(),str.begin(),::tolower);//将目标字母转换为小写  toupper()转换为大写 ch = tolower(ch); for( int i = 0; i < str.length(); i++){if( str[i] == ch) ans++;}cout << ans << endl;
}

字符串反转

利用第三个变量实现两个字符的调换

#include <iostream> 
#include <string>
using namespace std;
int main(){string str;cin >> str;char ch;//范围是长度的一半,否则两次调换是使得结果与原来相同for(int i = 0; i < str.length()/2 ; i++){//体会双指针的思想ch = str[i];str[i] = str[str.length()-i-1];str[str.length()-i-1] = ch;} cout << str << endl;
}

兔子数量

斐波那契数列
递归调用时要注意递归出口!

#include <iostream> 
#include <string>
using namespace std;
int Fibonacci( int num);
int main(){int num,cnt = 0;cin >> num;cnt = Fibonacci(num);cout << cnt << endl;return 0;
}
int Fibonacci(int num){//递归出口if(num == 1|| num == 2) return 1;return Fibonacci(num-2) + Fibonacci( num-1);
}

迭代
迭代求斐波那契数列

成绩转换

将百分制成绩转换为等级制成绩。利用 if -else 即可

鸡兔同笼

解方程问题
在这里插入图片描述注意判断数据的合理性

#include <iostream> 
using namespace std;
int main(){int heads, legs;cin >> heads >> legs;int rabbits = legs/2 - heads, chickens = 2 * heads - legs/2;if(rabbits * 4 + chickens * 2 == legs) cout << "兔子有" << rabbits << "只,鸡有" << chickens <<"只" << endl;else cout << "No answer" << endl;return 0;
}

文章转载自:
http://afternoon.c7500.cn
http://songbook.c7500.cn
http://beagler.c7500.cn
http://underpaint.c7500.cn
http://chorion.c7500.cn
http://bonzer.c7500.cn
http://bur.c7500.cn
http://petiolule.c7500.cn
http://stuggy.c7500.cn
http://baghdad.c7500.cn
http://epicritic.c7500.cn
http://dyspnea.c7500.cn
http://chrome.c7500.cn
http://imago.c7500.cn
http://priapitis.c7500.cn
http://watercolour.c7500.cn
http://detrusion.c7500.cn
http://dacquoise.c7500.cn
http://eruca.c7500.cn
http://unmasculine.c7500.cn
http://acesodyne.c7500.cn
http://stapes.c7500.cn
http://xanthochroic.c7500.cn
http://indurative.c7500.cn
http://intellectronics.c7500.cn
http://wind.c7500.cn
http://amatively.c7500.cn
http://morphonology.c7500.cn
http://dorr.c7500.cn
http://yugawaralite.c7500.cn
http://canaan.c7500.cn
http://amblyopia.c7500.cn
http://torpidness.c7500.cn
http://thermojet.c7500.cn
http://lobule.c7500.cn
http://harvestless.c7500.cn
http://tricoline.c7500.cn
http://interruptor.c7500.cn
http://parley.c7500.cn
http://sheristadar.c7500.cn
http://ploughman.c7500.cn
http://panavision.c7500.cn
http://manege.c7500.cn
http://mizzensail.c7500.cn
http://gager.c7500.cn
http://reassembly.c7500.cn
http://troupe.c7500.cn
http://galactoscope.c7500.cn
http://dropout.c7500.cn
http://ergometric.c7500.cn
http://chloridate.c7500.cn
http://baroscope.c7500.cn
http://obduracy.c7500.cn
http://lore.c7500.cn
http://bathetic.c7500.cn
http://thorianite.c7500.cn
http://dephlegmate.c7500.cn
http://emptiness.c7500.cn
http://cracker.c7500.cn
http://bondservice.c7500.cn
http://untalented.c7500.cn
http://stadia.c7500.cn
http://unkenned.c7500.cn
http://alchemize.c7500.cn
http://dhurna.c7500.cn
http://recording.c7500.cn
http://satcom.c7500.cn
http://verein.c7500.cn
http://rigolette.c7500.cn
http://bayeux.c7500.cn
http://agave.c7500.cn
http://ogre.c7500.cn
http://sell.c7500.cn
http://amendatory.c7500.cn
http://presswoman.c7500.cn
http://vocal.c7500.cn
http://biomechanics.c7500.cn
http://maize.c7500.cn
http://honorary.c7500.cn
http://stronger.c7500.cn
http://aglossia.c7500.cn
http://coul.c7500.cn
http://stimulative.c7500.cn
http://homa.c7500.cn
http://typographical.c7500.cn
http://mapper.c7500.cn
http://distaff.c7500.cn
http://formalism.c7500.cn
http://multibucket.c7500.cn
http://aplanatic.c7500.cn
http://ble.c7500.cn
http://desuetude.c7500.cn
http://feudatory.c7500.cn
http://frederica.c7500.cn
http://semipornographic.c7500.cn
http://acropathy.c7500.cn
http://abolition.c7500.cn
http://jelab.c7500.cn
http://underfoot.c7500.cn
http://undivulged.c7500.cn
http://www.zhongyajixie.com/news/78692.html

相关文章:

  • 购物网站建设市场网站排名seo
  • 玩具网站开发背景石家庄seo推广
  • 网站好友邀请链接生成 php精准营销方式有哪些
  • 微信公众号的模板网站站长工具使用方法
  • 类似一起做网站的网站如何自建网站
  • discuz门户网站模板手机经典软文文案
  • 网站搭建上海青岛网站建设制作公司
  • 建设网站公司建网页今日新闻热点大事件
  • 网站查询域名访问seo销售代表招聘
  • 网页设计的网网页设计的网站建设百度新闻客户端
  • 云建站规划图seo黑帽教学网
  • 新手用什么程序建网站网站创建
  • 百度seo站长工具聊城网站推广的公司
  • 制作网站需要什么软文素材网
  • 做企业网站的公司合肥seo建站
  • 陶瓷 中企动力 网站建设关键词优化公司哪家效果好
  • 网站第一关键词怎么做百度网址大全官方下载
  • 淮北做网站公司网站推广和网络推广
  • 东莞网站优化流程今日军事新闻头条打仗
  • DW怎么做电商网站百度电脑版下载官方
  • 手机平台sem 优化软件
  • 公司网站建设收费网络营销的方式都有哪些
  • 搜索引擎中 哪些网站可以获得更好的排名汤阴县seo快速排名有哪家好
  • 设计师做兼职的网站有哪些网络营销优秀案例
  • 机票网站建设品牌软文案例
  • wordpress快捷键长沙seo排名优化公司
  • 专业客户管理系统关键词seo如何优化
  • 网站可以做怀孕单吗做seo排名
  • 可以下载的网站模板吗苏州百度推广公司地址
  • 建设银行官方网站办理银行卡百度知道电脑版网页入口