每隔n次定制符号

cout-ing symbol after every n times?

本文关键字:符号 每隔      更新时间:2023-10-16

我想将符号"."与结果组合起来,并在每8个(字符或数字)后添加一个我试着使用for循环,但我做不到,这是我的代码,它是一种求和,从较大的数字中减去较小的数字,然后在第一个过程中得到较大数字的符号"a",然后从较大数字中减去更小的数字,直到结束。我想要的是在"A"之前和每8个过程之后定制".":

#include<iostream>
using namespace std;
int main()
{
    int A=26;
    int B=7;
    cout<<endl;
    while(A!=B)
    {
        if(A>B)
        {
            A=A-B;
            if(A==B){cout<<"AB";}
            else cout<<"A";
        }
        else
        {
            B=B-A;
            if(A==B){cout<<"BA";}
            else cout<<"B";
        }
    }
    cout<<endl;
    getchar();
    return 0;
}

保留一个int count变量。在每个过程中递增++count,当count变为8时,打印您想要的任何内容。

类似这样的东西:

int main()
{
    int A=26;
    int B=7;
    char bigger='.'; //I suppose this is what you want to print periodically!
    int count = 0;
    cout << bigger;
    while(A!=B)
    {
        if(A>B)
        {
            A=A-B;
            if(A==B) { cout << "AB"; count += 2;}
            else { cout << "A"; ++count; }
        }
        else
        {
            B=B-A;
            if(A==B) { cout << "BA"; count += 2; }
            else { cout << "B"; ++count; }
        }
        if(count == 8)
        {
            cout << bigger;
            count = 0; //reset it back to 0
        }
    }
    cout<<endl;
    getchar();
    return 0;
}