微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这段代码在做什么检查两个字符串的字谜 代码高尔夫简而言之

如何解决这段代码在做什么检查两个字符串的字谜 代码高尔夫简而言之

代码检查一个字谜是否有两个字符串。你能解释一下它是如何工作的吗?它在做什么。

#include <stdio.h>

int t[256],i;
int main(int c)
{
    for(;c+3;)
        (i=getchar())>10?t[i]+=c:(c-=2);

    for(i=257;--i&&!t[i-1];);
    puts(i?"false":"true");    
}

这是我在查看源代码时所理解的: 它维护一个数组 t[],并将值存储在 index = 字符的 ascii 值处。存储的值对于第一个字符串的字符为 1,对于第二个字符串的字符为 -1。如果两个字符串中都有一个字符,则存储在其中的值为零。 1 + (-1)。第一个 for 循环是输入。

你能解释一下这个循环正在检查什么,它是如何设置变量 i 的值的: for(i=257;--i&&!t[i-1];);

代码来自:https://developerinsider.co/find-anagram-string-programming-puzzles/

解决方法

只是为了好玩,让我们分解一下:

int t[256],i;     // Relying on the fact that global vars are initialized

int main(int c)   // Not a legal signature for main
{
    for(;c+3;)    // What is c's initial value? Assuming it is 1
        (i=getchar())>10?t[i]+=c:(c-=2);  // Read a char from stdin
                                          // If it is not a newline add c (1 or -1) to t[i]
                                          // - that is: increment or decrement letter count
                                          // If it is the 1st newline,c = c - 2 = -1;
                                          // So for the first word,increment letter count
                                          // For 2nd word,decrement letter count
                                          // When a 2nd newline is found c = c - 2 = -3
                                          // This ends the loop since -3 + 3 = 0

    for(i=257;--i&&!t[i-1];);             // Starting at the end of the array,check
                                          // each char code. They should all be 0
                                          // if not,it wasn't an anagram
    puts(i?"false":"true");               // If i did not reach 0 in previous loop,// not an anagram
}

我现在需要喝一杯。

,

代码高尔夫简而言之

你打高尔夫球吗?不,我是说this kind of golf

#include <stdio.h> // for getchar lib,not even mandatory to compile...

int t[256],i; // t is where you store the user input
int main(int c) // reusing the register of argc,equal to 1 // NB: if using any argument your program will fail but ok.
{
    for(;c+3;) // it loops until c reach -3,currently at 1 
        (i=getchar())>10?t[i]+=c:(c-=2); 

// i=getchar() // get user input
// (i=getchar())>10? // if user input > 10 means as long as the user 
// does not press enter,as '\n' is equal to 10.
// t[i]+=c:(c-=2); // if user input is valid (>10) then the array t at index of your character will either be incremented (if first input) 
// or decremented (if second input,as the first '\n' will decrement t[i] since c == -1) as c will be respectively equal to +1 and -1.
// then it will exit the loop on the second input (c = -3)

    for(i=257;--i&&!t[i-1];); 
// start from the end,i is predecrement and we access index i-1 to automatically 
// exit the loop when i reach 1,no need to check with --i>=0 (--i    >=0    is 3 chars instead of
// 2 char for t[i    -1    ]). If t[i-1] is equal 0 it means that the // character was 
// effectively cancelled out by the second string so t[i-1] == 0 
// and !t[i-1] are equivalent
    puts(i?"false":"true");  
// at the end --i will decrement i from 1 to 0 and exit without 
// reaching !t[0-1],if i is not 0 then it means the break occured 
// before going through all the characters.   
}

注意:如果不是为了打高尔夫球,当然不要创建这样的程序。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。