C,D,I 的C++实现
2016-06-23 22:30
357 查看
前言 具体分析请见http://blog.csdn.net/hnust_v/article/details/51746013
//C #include <bits/stdc++.h> using namespace std; int A[50500]={}; int main() { //freopen("E:\\test.txt","r",stdin); //freopen("E:\\tsst.txt","w",stdout); int n; while(cin>>n&&n) { queue<int> Que; for(int i=1;i<=n;i++) Que.push(i); int Count=0; while(Que.size()!=1) { ++Count; if(!(Count%2)) Que.pop(); else {int a=Que.front();Que.pop();Que.push(a);} } printf("%d\n",Que.front()); } return 0; }
//D #include <bits/stdc++.h> using namespace std; int Cal(int n) { if(n==1) return 1; if(!(n%2)) return Cal(n/2)*2-1; else return Cal((n-1)/2)*2+1; } int main(void) { int n; while(cin>>n) cout<<Cal(n)<<endl; }
//I #include <cstdio> using namespace std; typedef long long LL; LL Cal(LL n, LL m, LL k) { LL ans = 1; if(m == 1) return n; for(LL i = 1; i <= n;) { if((ans+m) < i) { LL x = (i-1-ans)%(m-1)? (i-1-ans)/(m-1): (i-1-ans)/(m-1)-1; if(i+x > n) { ans += (n+1-i)*m; break;} else { ans += x*m;i += x;} } else { ans = (ans+m)%i?(ans+m)%i:i; i++;} } return ans; } int main() { // freopen("f:\\test.txt","r",stdin); long long n,m; while(~scanf("%lld %lld",&n,&m)) printf("%lld\n",Cal(n,m,1)); }
相关文章推荐
- c语言学习笔记35之calloc
- C++类实践——时间类
- 2024C语言合法标识符
- C++中friend的使用
- C++中friend的使用
- C++中map注意事项
- C++学习心得
- 堆排序
- c++学习心得
- 项目 - 处理C++源代码的程序
- 最大堆、最小堆C++实现
- 项目 - 处理C++源代码的程序
- 同步IO和异步IO
- 项目 - 处理C++源代码的程序
- C++概述
- c语言中如何判断一个数组是递增数组
- C语言使用非递归和递归函数分别实现阶乘,斐波那契,最大公约数
- C语言使用非递归和递归函数分别实现阶乘,斐波那契,最大公约数
- 238. Product of Array Except Self
- c++学习心得