代码改变世界

算法学习之路(1)

2016-12-02 15:01  roffen  阅读(203)  评论(0)    收藏  举报

#include<iostream>
#include<iomanip>
#include "string.h"
#include<cstdio>
#include<cmath>
#include<time.h>
using namespace std;
/*int main()
{
int a, b;
while (cin >> a)
{
int sum = 0;
if (a == 0)
continue;
else
{
for (int i = 1;i <= a;i++)
{
cin >> b;
sum += b;
}
}
cout << sum << endl;
}
}*/
/*int main()
{
int a = 0;
cin >> a;
for (int i = 0;i < a;i++)
{
int b;
while (cin >> b)
{
int sum = 0,e;
if (b == 0)
continue;
else
{
for (int d = 0;d < b;d++)
{
cin >> e;
sum += e;
}
cout << sum << endl;
}
}
}
}*/
/*int main()
{
int a;
int arry[1000];
cin >> a;
for (int i=0;i < a;i++)
{
int b, sum = 0;
cin >> b;
for (int j = 0;j < b;j++)
{
int c;
cin >> c;
sum += c;
}
arry[i] = sum;
}
for (int i = 0;i < a - 1;i++)
{
cout<<arry[i]<<endl<<endl;//这个gg题目,md要把最后一个单独输出
}
cout << arry[a-1] << endl;
system("pause");
}*/
//这上面的都是第一节的。。。
//这是第二节,关于text
/*int main() //这题。。。写了一天,泪目了我操。找了很久关于字符的输入,对着百度的c代码改,wa了好几次,妈蛋最后发现是范围看错了,改成1000还是过不了,还有一个没改
{
int a;
char a1[1005], a2[1005];
cin >> a;
getchar();//除去回车
for (int i = 0;i < a;i++)
{
cin.getline(a2, 1000);
int b = strlen(a2);
for (int i = 0, j = 0, k = 0;i < b;i++)
{
if (a2[i] != ' ')
{
a1[j++] = a2[i];
}
else
{
for (k = j-1 ;k >= 0;k--)
{
cout << a1[k];
}
j = 0;
if (k < 0)
cout << " ";
}
if(i==(b-1))
{
for (k = j - 1;k >= 0;k--)
{
cout<< a1[k];
}
}
}
cout << endl;
}
return 0;
}*/
/*int main()
{
float ave=0,sum=0;
for (int i = 0;i < 12;i++)//pe了好几次,最后发现是最后输出完要加个回车。。。。
{
float a;
cin >> a;
sum += a;
}
ave = sum / 12;
cout << '$' <<fixed<<setprecision(2)<< ave<<endl;
// system("pause");
}*/
//关于输出n^n的个位
/*int main()//这个算法超时了
{
int a;
cin >> a;
for (int i = 0;i < a;i++)
{
int b;
cin >> b;
if (b >= 1 && b <= 1000000000)
{
int c = b,d=1;
b %= 10;
for (int i = 0;i < c;i++)
{
d *= b;
d %= 10;
}
if (i < a - 1)
cout << d << endl;
else
cout << d;
}
}
//system("pause");
}*/
//尝试用暴力的方法穷举出解得情况
/*int main()//依然gg
{
int a;
cin >> a;
for (int i = 0;i < a;i++)
{
int b, c;
cin >> b;
c = b % 10;//分别求出个位和幂位
if (b >= 1 && b <= 1000000000)
{
if (c == 1 || c == 5 || c == 6 || c == 0)
{
cout << c<<endl;
}
if (c == 4)
{
if (b % 2 == 1)
cout << 4 << endl;
else
cout << 6 << endl;
}
if (c == 9)
{
if (b % 2 == 1)
cout << 9 << endl;
else
cout << 1 << endl;
}
if (c == 2)
{
if (b % 4 == 1)
cout << 2 << endl;
if (b % 4 == 2)
cout << 4 << endl;
if (b % 4 == 3)
cout << 8 << endl;
if (b % 4 == 0)
cout << 6 << endl;
}
if (c == 3)
{
if (b % 4 == 1)
cout << 3 << endl;
if (b % 4 == 2)
cout << 9 << endl;
if (b % 4 == 3)
cout << 7 << endl;
if (b % 4 == 0)
cout << 1 << endl;
}
if (c == 7)
{
if (b % 4 == 1)
cout << 7 << endl;
if (b % 4 == 2)
cout << 9 << endl;
if (b % 4 == 3)
cout << 3 << endl;
if (b % 4 == 0)
cout << 1 << endl;
}
if (c == 8)
{
if (b % 4 == 1)
cout << 8 << endl;
if (b % 4 == 2)
cout << 4 << endl;
if (b % 4 == 3)
cout << 2 << endl;
if (b % 4 == 0)
cout << 6 << endl;
}
}
}
// system("pause");
}*/
//穷举完又上网找了下,发现可以把对四取模的操作应用在第一种的循环上
/*int main()
{
int a;
cin >> a;
while(a--)
{
int b;
int c, d;
cin >> b;
c = b % 10;
d = b % 4;
if (b <= 1000000000 && b >= 1)//ac了,答案不需要这条语句。。。
{
while (d == 0)
{
d = 4;
}
int j = 1;
for (int i = 0;i < d;i++)
{
j *= c;
j %= 10;
}
cout << j << endl;
}
}
// system("pause");
}*/
//关于url转码把特殊符号转为百分号编码
/*int main()
{
char a[1000];
while (gets_s(a))
{
if (a[0] == '#')
break;
for (int i = 0;i <strlen(a);i++)
{
if (a[i] == ' ')
cout << "%20";
else if (a[i] == '!')
cout << "%21";
else if (a[i] == '$')
cout << "%24";
else if (a[i] == '%')
cout << "%25";
else if (a[i] == '(')
cout << "%28";
else if (a[i] == ')')
cout << "%29";
else if (a[i] == '*')
cout << "%2a";
else
cout << a[i];
}
cout << endl;
}
//system("pause");
return 0;
}*/
//关于运算
/*int main()
{
int n;
cin >> n;
while (n--)
{
char a;
int b, c;//原本是double,wa的原因是在除法时没有判断是否整除就直接控制位数,看了百度才增加判断%,所以这里要改
cin >> a;//小技巧在把整形转成浮点
cin >> b >> c;
if (b > 0 && c < 10000)
{
if (a == '+')
cout << b + c << endl;
if (a == '-')
cout << b - c << endl;
if (a == '*')
cout << b*c << endl;
if (a == '/')
if (b%c != 0)
{
cout << fixed << setprecision(2) << b / (c*1.0) << endl;
}
else
cout << b / c << endl;
}
}
// system("pause");
}*/
//把数字进行进制转换
/*int main()
{
int a;
while (cin >> a)//又是看别人的代码= =还
{
int sum = 0;
while (a--)
{
int b, c,k=0;
char d, e;
cin >> b >> d >> c >> e;
while (b)
{
sum += b%10*pow(c, k++);
b /= 10;//在这里搞错没加等号,死循环了
}
}
cout << sum << endl;
}
}
*/
//取二进制下的最小位数

//挑-第一题
/*int max(int a, int b);
int main()
{
int n;
int a[100];
srand(time(0));
cin >> n;
int ans=0;
for (int i = 0;i < n;i++)
{
a[i] = rand()%6;
}
for (int i = 0;i < n;i++)
{
for (int j = i + 1;j < n;j++)
{
for (int k = j + 1;k < n;k++)
{
int len = a[i] + a[j] + a[k];
int ma = max(a[i], max(a[j], a[k]));
int re = len - ma;
if (re > ma)
{
ans = max(ans, len);
}
}
}
}
cout << ans;
system("pause");
}
int max(int a, int b)
{
if (a >= b)
return a;
if (a < b)
return b;
}*/
//抽签问题
/*int main()
{
int n = 3, m = 10;
int a[] = { 1,3,5 };
bool f = -1;
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
for (int k = 0;k < n;k++)
{
for (int x = 0;x < n;x++)
{
if (a[i] + a[k] + a[j] + a[x] == m)
{
f = 0;
}
}
}
}
}
if (f == 0)cout << "yse";
if (f == -1)cout << "no";
system("pause");
}*/
//用折半查找优化算法
/*int main()
{
int n = 3, m = 10;
int a[] = { 1,3,5 };
bool search(int x)
{
int i = 0, r = n;
while (r - i >= 1)
{
int l = (i + r) / 2;
if (a[l] == m)return true;
else if (a[l] < m)i = l + 1;
else
r = l;
}
return false;
}
bool f = false;
sort(a)

for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
for (int k = 0;k < n;k++)
{
if (search(m - a[i] - a[j] - a[k]))
{
f = true;
}
}
}
}
if (f)cout << "yes";
else cout << "no";
}*/
//nineteen
/*int min(int a, int b);
int main()
{
char a[110];
gets_s(a);
int len = strlen(a);
int ni=0, nn=0, ne=0,nt=0;
for (int i = 0;i < len;i++)
{
if (a[i] == 'i')ni++;
else if (a[i] == 'n')nn++;
else if (a[i] == 'e')ne++;
else if (a[i] == 't')nt++;
}
int ans = min(3 * (nn - 1), min(2 * ne, min(6 * ni,6*nt)));
if (ans == 3 * (nn - 1))cout << (nn - 1) / 2;
else if (ans == 2 * ne)cout << ne / 3;
else if (ans == 6 * ni)cout << ni;
else if (ans == 6 * nt)cout << nt;
// system("pause");
}
int min(int a, int b)
{
if (a >= b)
return b;
else
return a;
}*/
//矩阵变化
/*int main()
{
double matrix[170][170];
int n;
cin >> n;
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cin>>matrix[i][j];
}
}
double matrix1[170][170] = { 0 }, matrix2[170][170] = {0};
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
if (i == j)
{
matrix1[i][j] = matrix[i][j];
}
else
{
matrix1[i][j] =( matrix[i][j] + matrix[j][i]) / 2;
matrix2[i][j] = (matrix[i][j] - matrix[j][i]) / 2;
}
}
}
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << fixed << setprecision(8) << matrix1[i][j] << " ";
}
cout << endl;
}
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << fixed << setprecision(8) << matrix2[i][j] << " ";
}
cout << endl;
}
//system("pause");
}*/
//摆火柴棒
int main()
{
char a[320];
gets_s(a);
int len = strlen(a)-2;
int x,y,z;
for (int i = 0;i < strlen(a);i++)
{
if (a[i] == '+')x = i;
else if (a[i] == '=')y = i - x - 1;
}
z = len - x - y;
if (len % 2 == 1)
cout << "Impossible";
else
{
if ( x + y == z)
{
cout << a;
}
else if (x + y + 1 == z - 1)
{
for (int i = 0;i < x+1;i++)
{
cout << "|";
}
cout << "+";
for (int i = 0;i < y;i++)
{
cout << "|";
}
cout << "=";
for (int i = 0;i < z - 1;i++)
{
cout << "|";
}
}
else if (x + y - 1 == z + 1)
{
for (int i = 0;i < x - 1;i++)
{
cout << "|";
}
cout << "+";
for (int i = 0;i < y;i++)
{
cout << "|";
}
cout << "=";
for (int i = 0;i < z + 1;i++)
{
cout << "|";
}
}
else
{
cout << "Impossible";
}
}
system("pause");
}