洗牌问题 洗的我一脸懵逼 找规律哈 随便选一个数作为参考系 按照规律变换 每变换一次计数器++ 回到原来位置即可。
以第一个数为例1的位置变化为1 2 4…
大于n的数每次对应的下标为(当前坐标-x)*2-1
模拟一下就出来了
乘法不想写 左移运算相当于*2 右移运算相当于/2 浮点数不支持左右算术移位计算
可以把数字转成二进制看一下就可以了
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
int x;
while(cin>>x){
int pos=1;
int cnt=0;
while(true){
cnt++;
if(pos<=x){
pos=(pos<<1);
}
else{
pos=((pos-x)<<1)-1;
}
if(pos==1){
cout<<cnt<<endl;
break;
}
}
}
}
做完这个题我就是湖人黑粉了 没懂为什么出物理题(我物理要是好就不来学什么计算机了QAQ)
下面附上某网络大咖的推理过程
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
float h, l, v, g = 9.8;
while(cin>>h>>l>>v,h||l||v)
{
h += 0.5 * v * v / g - 0.5 * g * l * l / (v * v); //把上面的公式化简一下就是这个
printf("%.2f\n", h);
}
return 0;
}
这个计算机并不友好 多次样例记得初值清0 不得不说我的函数写的还是很清新脱俗的~~~
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
typedef long long ll;
int m1,m2;
int r1,r2,r3;
void A(){
r1=m1;
}
void B(){
r2=m2;
}
void C(){
m1=r3;
}
void D(){
m2=r3;
}
void E(){
r3=r1+r2;
}
void F(){
r3=r1-r2;
}
int main(){
while(scanf("%d%d",&m1,&m2)!=EOF){
// cout<<r1<<r2<<endl;
r1=r2=r3=0; //不清0就wa
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='A') A();
else if(s[i]=='B') B();
else if(s[i]=='C') C();
else if(s[i]=='D') D();
else if(s[i]=='E') E();
else if(s[i]=='F') F();
}
cout<<m1<<","<<m2<<endl;
}
}
年轻人 要学会暴力 没事不要想什么最短路 最小生成树 都是浮云的
从0点搜 就硬搜 计算机不爆炸就往死里搜
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int a[N][N];
bool st[N];
int n;
int cnt;
int ans=0x3f3f3f3f;
void dfs(int current,int step,int sum){
if(step==cnt){
ans=min(ans,sum);
return;
}
for(int i=1;i<n;i++){
if(st[i]){
st[i]=false; //一条路线
dfs(i,step+1,sum+a[current][i]);
st[i]=true; //回溯
}
}
}
int main(){
while(cin>>n,n){
memset(st,false,sizeof st);
cnt=0;
ans=0x3f3f3f3f;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int k;
cin>>k;
for(int i=0;i<k;i++){
int x;
cin>>x;
if(!st[x]){ //重复点无意义
cnt++;
st[x]=true;
}
}
dfs(0,0,0);
cout<<ans<<endl;
}
return 0;
}
上一个题教会我们要暴力 我们在这个题上应用一下
边读入边用hash表存储这个字符串的出现次数 最后直接查询
hash表会爆暴吗? 管他呢 暴就暴呗 反正我没遇到暴的 只要内存够大 nothing is impossible~~
#include<iostream>
#include<cstring>
#include<unordered_map>
using namespace std;
unordered_map<string,int> h;
char c;
int main()
{
string temp="";
while(1)
{
c=getchar();
if(c=='\n')
{
c=getchar();
temp="";
}
if(c=='\n') break;
temp+=c;
h[temp]++;
}
string str="";
while(cin>>str){
cout<<h[str]<<endl;
}
return 0;
}
第三题不会 太复杂 大家想了解的可以百度 方案数问题一般都是dp
dp没一个是善良的 QAQ