通过:
1、反序输出
问题描述:输入任意4个字符(如:abcd), 并按反序输出(如:dcba)
2、最大最小值
问题描述:输入N个(N<=10000)数字,求出这N个数字中的最大值和最小值。每个数字的绝对值不大于1000000。
代码:
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int m[n];
for(int i=0;i<n;i++)
m[i]=0;
int min=0,max=0;
for(int i=0;i<n;i++){
cin>>m[i];
if(m[i]<=min)
min=m[i];
else if(m[i]>=max)
max=m[i];
}
cout<<max<<" "<<min<<endl;
}
return 0;
}
3、abc
问题描述:设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。
超时错误解:
r1=a*100+b*10+c;
r2=b*100+c*10+c;
for(a=1;a<10;a++){
for(b=1;b<10;b++){
for(c=0;c<10;c++){
if(r1+r2==532)
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
}
提交解:
#include<iostream>
using namespace std;
int main(){
int a,b,c,r1,r2;
for(a=1;a<10;a++){
for(b=1;b<10;b++){
for(c=0;c<10;c++){
r1=a*100+b*10+c;
r2=b*100+c*10+c;
if(r1+r2==532)
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
}
return 0;
}
复杂度更少解:
来源:牛客网
#include <stdio.h>
int main(){
int i,bcc,a,b,c;
for(i=0;i<=532;++i){
bcc=532-i;
//若十位与个位相同,判断和是否532
if(bcc/10%10 == bcc%10){
a=i/100;
c=bcc%10;
b=bcc/100;
if(a*100+b*10+c + bcc==532)
printf("%d %d %d\n",a,b,c);
}
}
return 0;
}
4、特殊算法,逐位相乘
问题描述:写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
在循环中加入计算的写法,代码量更少
来源:牛客网
#include<stdio.h>
int main (int argc, char *argv[]){
int a,b,res,ta,tb;
for(;~scanf("%d %d",&a,&b);){
for(res=0,ta=a;ta>0;ta/=10)
for(tb=b;tb>0;tb/=10)
res += (ta%10)*(tb%10);
printf("%d\n",res);
}
}
复杂度更小,利用字符串的解法:
来源:牛客网
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
string str1, str2;
while(cin >> str1 >> str2){
int sum = 0;
for(int i = 0; i < str1.length(); ++i){
for(int j = 0; j < str2.length(); ++j){
sum += (str1[i]-'0')*(str2[j]-'0');
}
}
cout << sum << endl;
}
return 0;
}
2018.6.23
1、小白鼠根据重量排序输出颜色
问题描述:N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。帽子的颜色用“red”,“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。
使用结构体,一个整型数组和一个字符串数组的写法会超时
(1)、对小鼠颜色定义,使用String或者char 【】;
(2)、冒泡排序
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
(3)、但是可以使用c++中自带的sort()函数,默认是升序排序。
此题是由大到小输出,降序排序,故自定义排序规则,然后调用函数,部分代码如下:
#include <iostream>
using namespace std;
int n;
cin>>n;
using namespace std;
typedef struct mou{//定义数组元素组成
int weight;
char color[n];
}mouse[n];
bool cmp(mou a, mou b)//定义排序规则 降序
{
return a.weight>b.weight;
}
int main(){
for(int i=0;i<n;i++){
cin>>mouse[i].weight>>mouse[i].color;
}
sortmouse,mouse+n,cmp);\\给定排序区间
for(int i=0;i<n;i++){
cout<<mouse[i].color<<endl;
}
return 0;
}
2、Skew数
问题描述:在 skew binary 表示中,第 k 位的值 x[k] 表示 x[k]×(2^(k+1)-1)。每个位上的可能数字是 0 或 1,最后面一个非零位可以是 2,例如,10120(skew) = 1×(2^5-1) + 0×(2^4-1) + 1×(2^3-1) + 2×(2^2-1) + 0×(2^1-1) = 31 + 0 + 7 + 6 + 0 = 44。
(1)、C++中n次方的表示方法为:直接调用库函数中的pow(x , y)。添加头文件#include <math.h>
(2)不能用int存储输入的数,会超出int类型的范围。
(3)使用字符串string类型来完成输入,直接调用字符串a.length(),b=a.[1]-'0'对字符串中的每一位数字来取出
代码如下:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
string a;
int c;
while (cin >> a){
int b = a.size();
c = b;
int sum = 0;
for (int i = 1; i<=c; i++){
int g= (a[i - 1] - '0');
sum += g*(pow(2, b) - 1);
b--;}
cout << sum << endl;
}
return 0;
}
自己编程AC以后看看别人怎么写的,会发现更多代码量更少复杂度更低的解决办法。