HzuHtx


私信TA

用户名:hetangxin123

访问量:41208

签 名:

私はいつまでもレムが好きです。

等  级
排  名 29
经  验 14163
参赛次数 10
文章发表 76
年  龄 0
在职情况 学生
学  校 贺州学院
专  业 软件工程

  自我简介:

写不动,根本写不动

看了第一个大佬的,感觉是我想复杂了XB3`%QP4U{[%JX]@(_PRW9J.jpg,不过还是可以值得借鉴一下的


#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "sstream"
#include "cmath"
using namespace std;
string a[1001][1001];
int main()
{
	int n;
	while (cin >> n)
	{
		if (n == 1)
			printf("1/1\n");
		else
		{
			int m = sqrt(n);
			m = m + 2;
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < m; j++)
				{
					stringstream ii, jj;
					string s1, s2;
					ii << i + 1;
					ii >> s1;
					jj << j + 1;
					jj >> s2;
					a[i][j] = s1;
					a[i][j] += '/';
					a[i][j] += s2;
				}
			}
			int temp = 1;
			int x = 0, y = 0;
		loop:if (x == 0)
		{
			y = y + 1;
			while (y >= 0)
			{
				if (temp == n - 1)
				cout << a[x][y] << endl;
				temp++;
				if (temp == n)
				{
					goto loop1;
				}
				x++;
				y--;
			}
			y++;
			x--;
		}
			 if (y == 0)
			 {
				 x = x + 1;
				 while (x >= 0)
				 {
					 if(temp==n-1)
					 cout << a[x][y] << endl;
					 temp++;
					 if (temp == n)
					 {
						 goto loop1;
					 }
					 x--;
					 y++;
				 }
				 x++;
				 y--;
			 }
		 loop1:if (temp < n)
			 goto loop;
		}
	}
	return 0;
}


附上第二次写的正确的答案

#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
	int n;
	while (cin >> n)
	{
		int i = 0;
		int s = 0;
		while (s < n)
		{
			i++;
			s = s + i;
		}
		if (i % 2 == 1)
		{
			s = s - i;
			int a = i;
			int b = 1;
			for (int i = 1; i < n - s; i++)
			{
				a--;
				b++;
			}
			cout << a << "/" << b << endl;
		}
		else
		{
			s = s - i;
			int a = 1;
			int b = i;
			for (int j = 1; j < n - s; j++)
			{
				a++;
				b--;
			}
			cout << a << "/" << b << endl;
		}
	}
	return 0;
}


附上第三种方法=.=  ,可以优化下,懒得优化了


#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
    int a, b;
    if (n == 1)
        cout << "1/1" << endl;
    if (n == 2)
        cout << "1/2" << endl;
    else
    {
        a = 2; b = 1;
        int flag = 1;
        int flag1 = 1, flag2 = 1;
        for (int i = 3; i < n; i++)
        {
            if (b == 1 && flag == 1)
            {
                a = a + 1;
                flag = 0;
                flag1 = 0;
                flag2 = 1;
                continue;
            }
            if (a == 1 && flag == 1)
            {
                b = b + 1;
                flag = 0;
                flag2 = 0;
                flag1 = 1;
                continue;
            }
            if (!flag1)
            {
                a = a - 1;
                b = b + 1;
                flag = 1;
                continue;
            }
            if (!flag2)
            {
                a = a + 1;
                b = b - 1;
                flag = 1;
                continue;
            }
        }
        cout << a << "/" << b << endl;
    }
    }
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区