• fzuoop期中练习


    5-1 科学计数法的值

    科学计数法是一种数学专用术语。将一个数表示成 a×10的n次幂的形式,其中1≤|a|<10,n为整数,这种记数方法叫科学计数法。例如920000可以表示为9.2*10^5。现在需要对输入的字符串进行分离,自动识别该科学计数法中的a和幂次,计算其表征的具体数值并输出该值。例如,对于输入的复数字符串“9.210^5”,输出 The actual value for 9.210^5 is 920000

    注意:

    • 1、每组测试数据仅包括一个用于科学计数法的字符串。
    • 2、输入字符串保证合法。
    • 3、字符串长度不超过1000
    • 4、幂次不超过200

    输入示例:
    9.2*10^5

    输出示例:
    The actual value for 9.2*10^5 is 920000

    #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 不能用循环是一件多么悲伤的事

    下面是一个算到10的加法表:
    0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 0 + 3 = 3 0 + 4 = 4 0 + 5 = 5 0 + 6 = 6 0 + 7 = 7 0 + 8 = 8 0 + 9 = 9 0 +10 = 10
    1 + 0 = 1 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 1 + 4 = 5 1 + 5 = 6 1 + 6 = 7 1 + 7 = 8 1 + 8 = 9 1 + 9 = 10
    2 + 0 = 2 2 + 1 = 3 2 + 2 = 4 2 + 3 = 5 2 + 4 = 6 2 + 5 = 7 2 + 6 = 8 2 + 7 = 9 2 + 8 = 10
    3 + 0 = 3 3 + 1 = 4 3 + 2 = 5 3 + 3 = 6 3 + 4 = 7 3 + 5 = 8 3 + 6 = 9 3 + 7 = 10
    4 + 0 = 4 4 + 1 = 5 4 + 2 = 6 4 + 3 = 7 4 + 4 = 8 4 + 5 = 9 4 + 6 = 10
    5 + 0 = 5 5 + 1 = 6 5 + 2 = 7 5 + 3 = 8 5 + 4 = 9 5 + 5 = 10
    6 + 0 = 6 6 + 1 = 7 6 + 2 = 8 6 + 3 = 9 6 + 4 = 10
    7 + 0 = 7 7 + 1 = 8 7 + 2 = 9 7 + 3 = 10
    8 + 0 = 8 8 + 1 = 9 8 + 2 = 10
    9 + 0 = 9 9 + 1 = 10
    10+ 0 = 10
    本题目要求读入1个整数,输出加法表,每一行都算到结果为输入的整数为止。不允许使用循环,不允许使用循环,不允许使用循环。重要的事情说三遍(括弧笑)

    输入格式:
    在一行中给出一个正整数N(0≤N≤99)。

    输出格式:
    按照示例的格式输出左上三角N+M的表,行列都从0开始。
    加号左边数字占2位、左对齐;
    加号右边数字占2位、右对齐;
    结果数字占2位,左对齐。
    等号两边各一个空格。
    两个式子之间加一个空格(行末的空格不用去掉)

    输入样例:
    5

    输出样例:
    0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 0 + 3 = 3 0 + 4 = 4 0 + 5 = 5
    1 + 0 = 1 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 1 + 4 = 5
    2 + 0 = 2 2 + 1 = 3 2 + 2 = 4 2 + 3 = 5
    3 + 0 = 3 3 + 1 = 4 3 + 2 = 5
    4 + 0 = 4 4 + 1 = 5
    5 + 0 = 5

    这道题虽然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;
    }
    
  • 相关阅读:
    闭包问题小总结
    git常用命令总结
    数组对象方法的使用
    JS截取URL参数
    CSS清除浮动&内容居中&文字溢出
    网址大全
    any more
    下载网页视频
    下载无损音乐
    "前端" 宝藏网站
  • 原文地址:https://www.cnblogs.com/ZhaoxiCheung/p/5585581.html
Copyright © 2020-2023  润新知