5-1 科学计数法的值
#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
string str;
while(cin >> str)
{
string ans;
bool flag,IsSign,IsMinus;
int i;
int len = str.size();
string::size_type loc = str.find("^");
if (str[loc + 1] != '-')
{
IsSign = false;
}
else
{
IsSign = true;
}
if (!IsSign)
{
int intcnt = 0;
for (i = 0; str[i] != '*' && str[i] != '.'; i++)
{
ans += str[i];
intcnt++;
}
if (str[i] == '.')
{
i++;
}
int cnt = 0;
for (; str[i] != '*'; i++)
{
ans += str[i];
cnt++;
}
int record;
string tmp;
stringstream ss;
if (loc == string::npos)
{
record = 1;
}
else
{
for (i = loc+1; i < len; i++)
{
tmp += str[i];
}
ss << tmp;
ss >> record;
}
if (record < cnt)
{
flag = false;
}
else
{
flag = true;
}
int len1 = ans.size();
if (flag)
{
cout << "The actual value for " << str << " is " << ans;
for (i = record - cnt; i > 0; i--)
{
cout << "0";
}
cout << endl;
}
else
{
cout << "The actual value for " << str << " is ";
for (i = 0; i < intcnt + record; i++)
{
cout << ans[i];
}
cout << ".";
for (i = intcnt + record; i < len1; i++)
{
cout << ans[i];
}
cout << endl;
}
}
else
{
if (str[0] == '-')
{
str.erase(0,1);
IsMinus = true;
}
else
{
IsMinus = false;
}
len = str.size();
int intcnt = 0;
for (i = 0; str[i] != '*' && str[i] != '.'; i++)
{
ans += str[i];
intcnt++;
}
if (str[i] == '.')
{
i++;
}
for (; str[i] != '*'; i++)
{
ans += str[i];
}
int record;
string tmp;
stringstream ss;
if (loc == string::npos)
{
record = 1;
}
else
{
if (IsMinus)
{
for (i = loc + 1; i < len; i++)
{
tmp += str[i];
}
}
else
{
for (i = loc + 2;i < len;i++)
{
tmp += str[i];
}
}
ss << tmp;
ss >> record;
}
cout << "The actual value for " << str << " is ";
if (IsMinus)
{
cout << "-";
}
cout << "0.";
for (i = 0; i < record - intcnt; i++)
{
cout << "0";
}
cout << ans << endl;
}
}
return 0;
}
5-3 不能用循环是一件多么悲伤的事
这道题虽然AC了,总感觉递归的姿势不正确= =
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;
int N;
void func(int num,int cnt)
{
if (num <= N)
{
if (num + cnt <= N)
{
printf("%-2d+%2d = %-2d ",num,cnt,num+cnt);
//cout << setiosflags(ios::left) << setw(2) << num << " + " << setiosflags(ios::right) << setw(2) << cnt << " = " << setiosflags(ios::left) << setw(2) << num+cnt << " ";
func(num,cnt+1);
}
cout << endl;
}
if (num > N)
exit(0);
func(num+1,0);
}
int main()
{
scanf("%d",&N);
func(0,0);
return 0;
}