3 条题解

  • 2
    @ 2024-2-12 19:11:13
    #include <iostream> 
    using namespace std; 
    int n_num(int n){ 
        int a = 1; 
        for (int i = 10;; i *= 10){ 
            if (n / i != 0)  
                a++;  
            else  
                break;  
        } 
        return a; 
    } 
    void num_one(int n){ 
        switch (n){ 
            case 1: cout << "one"; break; 
            case 2: cout << "two"; break; 
            case 3: cout << "three"; break; 
            case 4: cout << "four"; break; 
            case 5: cout << "five"; break; 
            case 6: cout << "six"; break; 
            case 7: cout << "seven"; break; 
            case 8: cout << "eight"; break; 
            case 9: cout << "nine"; break; 
        } 
    } 
    void num_two_one(int n){ 
        switch (n){ 
            case 10: cout << "ten"; break; 
            case 11: cout << "eleven"; break; 
            case 12: cout << "twelve"; break; 
            case 13: cout << "thirteen"; break; 
            case 14: cout << "fourteen"; break; 
            case 15: cout << "fifteen"; break; 
            case 16: cout << "sixteen"; break; 
            case 17: cout << "seventeen"; break; 
            case 18: cout << "eighteen"; break; 
            case 19: cout << "nineteen"; 
        } 
    } 
    void num_two_other(int n){ 
        switch (n / 10){ 
            case 2: cout << "twenty"; break; 
            case 3: cout << "thirty"; break; 
            case 4: cout << "forty"; break; 
            case 5: cout << "fifty"; break; 
            case 6: cout << "sixty"; break; 
            case 7: cout << "seventy"; break; 
            case 8: cout << "eighty"; break; 
            case 9: cout << "ninety"; break; 
        } 
        if (n % 10 != 0){ 
            cout << "-"; 
            num_one(n % 10); 
        } 
    } 
    void num_three(int n){ 
        num_one(n / 100); 
        if (n % 100 == 0)
            cout << " hundred";  
        else{ 
            cout << " hundred and "; 
            if (n % 100 / 10 == 1)
                num_two_one(n % 100);  
            else  
                num_two_other(n % 100);  
        } 
    } 
    void num_four_to_six(int n){ 
        if (n_num(n / 1000) == 1){ 
            num_one(n / 1000); 
            cout << " thousand ";
            num_three(n % 1000); 
        } 
        if (n_num(n / 1000) == 2){ 
            if (n / 10000 == 1){ 
                num_two_one(n / 1000); 
                cout << " thousand "; 
                num_three(n % 1000); 
            } 
            else{ 
                num_two_other(n / 1000); 
                cout << " thousand "; 
                num_three(n % 1000); 
            } 
        } 
        if (n_num(n / 1000) == 3){ 
            num_three(n / 1000); 
            cout << " thousand "; 
            num_three(n % 1000); 
        } 
    } 
    void num_seven_to_nine(int n){ 
        if (n_num(n / 1000000) == 1){ 
            num_one(n / 1000000); 
            cout << " million "; 
            num_four_to_six(n % 1000000); 
        } 
        if (n_num(n / 1000000) == 2){ 
            if (n / 10000000 == 1){ 
                num_two_one(n / 1000000); 
                cout << " million "; 
                num_four_to_six(n % 1000000); 
            } 
            else{ 
                num_two_other(n / 1000000); 
                cout << " million "; 
                num_four_to_six(n % 1000000);
            } 
        } 
        if (n_num(n / 1000000) == 3){ 
            num_three(n / 1000000); 
            cout << " million "; 
            num_four_to_six(n % 1000000); 
        } 
    } 
    void num_ten(int n){ 
        num_one(n / 1000000000); 
        cout << " billion "; 
        num_seven_to_nine(n % 100000000); 
    } 
    int main(){  
        int n; 
        cin >> n; 
        if (n_num(n) == 0){ 
            cout << "zero"; 
            return 0; 
        } 
        if (n_num(n) == 1) 
            num_one(n);  
        if (n_num(n) == 2){ 
            if (n / 10 == 1)
                num_two_one(n);  
            else  
                num_two_other(n);  
        } 
        if (n_num(n) == 3) 
            num_three(n);  
        if (n_num(n) >= 4 && n_num(n) <= 6)
            num_four_to_six(n);  
        if (n_num(n) >= 7 && n_num(n) <= 9)  
            num_seven_to_nine(n);  
        if (n_num(n) == 10) 
            num_ten(n);  
        return 0; 
    }
    
    • 2
      @ 2023-7-8 9:56:02
      #include <iostream>
      using namespace std;
      int n_num(int n)//判断位数的函数
      {
      	int a = 1;
      	for (int i = 10;; i *= 10)
      	{
      		if (n / i != 0)
      		{
      			a++;
      		}
      		else
      		{
      			break;//如果n/i==0的话说明已经到达最高位了,就可以停下来了
      		}
      	}
      	return a;
      }
      void num_one(int n)//一位数
      {
      	switch (n)
      	{
      	case 1:
      		cout << "one";
      		break;
      	case 2:
      		cout << "two";
      		break;
      	case 3:
      		cout << "three";
      		break;
      	case 4:
      		cout << "four";
      		break;
      	case 5:
      		cout << "five";
      		break;
      	case 6:
      		cout << "six";
      		break;
      	case 7:
      		cout << "seven";
      		break;
      	case 8:
      		cout << "eight";
      		break;
      	case 9:
      		cout << "nine";
      		break;
      	}
      }
      void num_two_one(int n)
      {
      	switch (n)
      	{
      	case 10:
      		cout << "ten";
      		break;
      	case 11:
      		cout << "eleven";
      		break;
      	case 12:
      		cout << "twelve";
      		break;
      	case 13:
      		cout << "thirteen";
      		break;
      	case 14:
      		cout << "fourteen";
      		break;
      	case 15:
      		cout << "fifteen";
      		break;
      	case 16:
      		cout << "sixteen";
      		break;
      	case 17:
      		cout << "seventeen";
      		break;
      	case 18:
      		cout << "eighteen";
      		break;
      	case 19:
      		cout << "nineteen";
      	}
      }
      void num_two_other(int n)
      {
      	switch (n / 10)
      	{
      	case 2:
      		cout << "twenty";
      		break;
      	case 3:
      		cout << "thirty";
      		break;
      	case 4:
      		cout << "forty";
      		break;
      	case 5:
      		cout << "fifty";
      		break;
      	case 6:
      		cout << "sixty";
      		break;
      	case 7:
      		cout << "seventy";
      		break;
      	case 8:
      		cout << "eighty";
      		break;
      	case 9:
      		cout << "ninety";
      		break;
      	}
      	if (n % 10 != 0)
      	{
      		cout << "-";
      		num_one(n % 10);
      	}
      }
      void num_three(int n)
      {
      	num_one(n / 100);
      	if (n % 100 == 0)
      	{
      		cout << " hundred";
      	}
      	else
      	{
      		cout << " hundred and ";
      		if (n % 100 / 10 == 1)
      		{
      			num_two_one(n % 100);
      		}
      		else
      		{
      			num_two_other(n % 100);
      		}
      	}
      }
      void num_four_to_six(int n)
      {
      	if (n_num(n / 1000) == 1)//4位数,比如1179
      	{
      		num_one(n / 1000);
      		cout << " thousand ";
      		num_three(n % 1000);
      	}
      	if (n_num(n / 1000) == 2)//5位数,比如11451
      	{
      		if (n / 10000 == 1)
      		{
      			num_two_one(n / 1000);
      			cout << " thousand ";
      			num_three(n % 1000);
      		}
      		else
      		{
      			num_two_other(n / 1000);
      			cout << " thousand ";
      			num_three(n % 1000);
      		}
      	}
      	if (n_num(n / 1000) == 3)//6位数,比如114514(好臭的数字)
      	{
      		num_three(n / 1000);
      		cout << " thousand ";
      		num_three(n % 1000);
      	}
      }
      void num_seven_to_nine(int n)
      {
      	if (n_num(n / 1000000) == 1)//7位数
      	{
      		num_one(n / 1000000);
      		cout << " million ";
      		num_four_to_six(n % 1000000);
      	}
      	if (n_num(n / 1000000) == 2)//8位数
      	{
      		if (n / 10000000 == 1)
      		{
      			num_two_one(n / 1000000);
      			cout << " million ";
      			num_four_to_six(n % 1000000);
      		}
      		else
      		{
      			num_two_other(n / 1000000);
      			cout << " million ";
      			num_four_to_six(n % 1000000);
      		}
      	}
      	if (n_num(n / 1000000) == 3)//9位数
      	{
      		num_three(n / 1000000);
      		cout << " million ";
      		num_four_to_six(n % 1000000);
      	}
      }
      void num_ten(int n)//10位数
      {
      	num_one(n / 1000000000);
      	cout << " billion ";
      	num_seven_to_nine(n % 100000000);
      }
      int main()
      {
      	//题目给的范围刚好是int型,不如直接用int就不要用string了
      	int n;
      	cin >> n;
      	if (n_num(n) == 0)//0位数,0比较特别,如果放在n_num函数里面0输出的a是0所以不妨单独讨论
      	{
      		cout << "zero";
      		return 0;
      	}
      	if (n_num(n) == 1)//1位数
      	{
      		num_one(n);
      	}
      	if (n_num(n) == 2)//2位数
      	{
      		if (n / 10 == 1)//2位数中1开头的也很特殊,所以也单拎出来
      		{
      			num_two_one(n);
      		}
      		else
      		{
      			num_two_other(n);
      		}
      	}
      	if (n_num(n) == 3)
      	{
      		num_three(n);
      	}
      	if (n_num(n) >= 4 && n_num(n) <= 6)
      	{
      		num_four_to_six(n);
      	}
      	if (n_num(n) >= 7 && n_num(n) <= 9)
      	{
      		num_seven_to_nine(n);
      	}
      	if (n_num(n) == 10)
      	{
      		num_ten(n);
      	}
      	return 0;
      }
      
      • 2
        @ 2023-7-8 9:55:22

        #include <iostream> using namespace std; int n_num(int n)//判断位数的函数 { int a = 1; for (int i = 10;; i *= 10) { if (n / i != 0) { a++; } else { break;//如果n/i==0的话说明已经到达最高位了,就可以停下来了 } } return a; } void num_one(int n)//一位数 { switch (n) { case 1: cout << "one"; break; case 2: cout << "two"; break; case 3: cout << "three"; break; case 4: cout << "four"; break; case 5: cout << "five"; break; case 6: cout << "six"; break; case 7: cout << "seven"; break; case 8: cout << "eight"; break; case 9: cout << "nine"; break; } } void num_two_one(int n) { switch (n) { case 10: cout << "ten"; break; case 11: cout << "eleven"; break; case 12: cout << "twelve"; break; case 13: cout << "thirteen"; break; case 14: cout << "fourteen"; break; case 15: cout << "fifteen"; break; case 16: cout << "sixteen"; break; case 17: cout << "seventeen"; break; case 18: cout << "eighteen"; break; case 19: cout << "nineteen"; } } void num_two_other(int n) { switch (n / 10) { case 2: cout << "twenty"; break; case 3: cout << "thirty"; break; case 4: cout << "forty"; break; case 5: cout << "fifty"; break; case 6: cout << "sixty"; break; case 7: cout << "seventy"; break; case 8: cout << "eighty"; break; case 9: cout << "ninety"; break; } if (n % 10 != 0) { cout << "-"; num_one(n % 10); } } void num_three(int n) { num_one(n / 100); if (n % 100 == 0) { cout << " hundred"; } else { cout << " hundred and "; if (n % 100 / 10 == 1) { num_two_one(n % 100); } else { num_two_other(n % 100); } } } void num_four_to_six(int n) { if (n_num(n / 1000) == 1)//4位数,比如1179 { num_one(n / 1000); cout << " thousand "; num_three(n % 1000); } if (n_num(n / 1000) == 2)//5位数,比如11451 { if (n / 10000 == 1) { num_two_one(n / 1000); cout << " thousand "; num_three(n % 1000); } else { num_two_other(n / 1000); cout << " thousand "; num_three(n % 1000); } } if (n_num(n / 1000) == 3)//6位数,比如114514(好臭的数字) { num_three(n / 1000); cout << " thousand "; num_three(n % 1000); } } void num_seven_to_nine(int n) { if (n_num(n / 1000000) == 1)//7位数 { num_one(n / 1000000); cout << " million "; num_four_to_six(n % 1000000); } if (n_num(n / 1000000) == 2)//8位数 { if (n / 10000000 == 1) { num_two_one(n / 1000000); cout << " million "; num_four_to_six(n % 1000000); } else { num_two_other(n / 1000000); cout << " million "; num_four_to_six(n % 1000000); } } if (n_num(n / 1000000) == 3)//9位数 { num_three(n / 1000000); cout << " million "; num_four_to_six(n % 1000000); } } void num_ten(int n)//10位数 { num_one(n / 1000000000); cout << " billion "; num_seven_to_nine(n % 100000000); } int main() { //题目给的范围刚好是int型,不如直接用int就不要用string了 int n; cin >> n; if (n_num(n) == 0)//0位数,0比较特别,如果放在n_num函数里面0输出的a是0所以不妨单独讨论 { cout << "zero"; return 0; } if (n_num(n) == 1)//1位数 { num_one(n); } if (n_num(n) == 2)//2位数 { if (n / 10 == 1)//2位数中1开头的也很特殊,所以也单拎出来 { num_two_one(n); } else { num_two_other(n); } } if (n_num(n) == 3) { num_three(n); } if (n_num(n) >= 4 && n_num(n) <= 6) { num_four_to_six(n); } if (n_num(n) >= 7 && n_num(n) <= 9) { num_seven_to_nine(n); } if (n_num(n) == 10) { num_ten(n); } return 0; }

        • 1

        信息

        ID
        126
        时间
        1000ms
        内存
        128MiB
        难度
        1
        标签
        递交数
        62
        已通过
        46
        上传者