| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

【求组】新人写的小代码有点问题

#include <stdio.h>

int sum(int n,int a);

int main()

{

int n,a;

scanf("%d%d",&n, &a);

printf("s=%d",sum(n,a));

}

int sum(int n,int a)

{

for(i=1,t=1; i<=n; i++) {

s+=item;

t=t*10;

}

return s;

}

S=1594578524

```#include <stdio.h>

int sum(int n,int a);

int main()

{

int n,a;

scanf("%d%d",&n, &a);

printf("s=%d",sum(n,a));

return 0;
}

int sum(int n,int a)

{

for(i=1,t=0; i<=n; i++) {

t = t*10+a;

s += t ;

}

return s;
}```

//online parser: https://www.bccn.net/run/

```#include <stdio.h>

int sum(int n, int a)
{
if (1 == n) return a;

#define PLUS10(_a) (_a << 1) + (_a << 3)
return a * n + sum(n - 1, PLUS10(a));
}

int main(int argc, char *argv[])
{
int n = 5, a = 3;

printf("s = %d", sum(n, a));

return 0;
}```

/online parser: https://www.bccn.net/run/

```#include <stdio.h>

int sum(int n, int a)
{
#define BUG_ON
if (0 == n) return 0;
if (1 == n) return a;

#define PLUS10(_a) (_a << 1) + (_a << 3)
return a * n + sum(n - 1, PLUS10(a));
}

int main(int argc, char *argv[])
{
int n, a, i;
unsigned char *p = (unsigned char *)main;

//test procedure
for(i = 0; i < 125; i++, p++) {
#define B2HT(_b) ((_b >> 4) & 0xf)
#define B2LT(_b) ((_b) & 0x0f)
n = B2HT(*p) % 10;
a = B2LT(*p) % 10;
printf("src = 0x%02x, n = %d, a = %d  ", *p, n, a);
printf("[%02d]:sum = %d\n", i + 1, sum(n, a));
}

return 0;
}```

src = 0x55, n = 5, a = 5  [01]:sum = 61725
src = 0x48, n = 4, a = 8  [02]:sum = 9872
src = 0x89, n = 8, a = 9  [03]:sum = 111111102
src = 0xe5, n = 4, a = 5  [04]:sum = 6170
src = 0x48, n = 4, a = 8  [05]:sum = 9872
src = 0x83, n = 8, a = 3  [06]:sum = 37037034
src = 0xec, n = 4, a = 2  [07]:sum = 2468
src = 0x30, n = 3, a = 0  [08]:sum = 0
src = 0x89, n = 8, a = 9  [09]:sum = 111111102
src = 0x7d, n = 7, a = 3  [10]:sum = 3703701
src = 0xdc, n = 3, a = 2  [11]:sum = 246
src = 0x48, n = 4, a = 8  [12]:sum = 9872
src = 0x89, n = 8, a = 9  [13]:sum = 111111102
src = 0x75, n = 7, a = 5  [14]:sum = 6172835
src = 0xd0, n = 3, a = 0  [15]:sum = 0
src = 0x48, n = 4, a = 8  [16]:sum = 9872
src = 0xc7, n = 2, a = 7  [17]:sum = 84
src = 0x45, n = 4, a = 5  [18]:sum = 6170
src = 0xf8, n = 5, a = 8  [19]:sum = 98760
src = 0x7c, n = 7, a = 2  [20]:sum = 2469134
src = 0x05, n = 0, a = 5  [21]:sum = 0
src = 0x40, n = 4, a = 0  [22]:sum = 0
src = 0x00, n = 0, a = 0  [23]:sum = 0
src = 0xc7, n = 2, a = 7  [24]:sum = 84
src = 0x45, n = 4, a = 5  [25]:sum = 6170

```
#include <windows.h>

int main()
{
int n = 5, a = 3, t = 0, s = 0;
while (n-- > 0) s += (t = t * 10 + a);

char buf[100] = { 0 };
wsprintfA(buf, "s=%d", s);
MessageBoxA(0, buf, buf, MB_OK);
}

```

Windows 并不提供 stdio.h 头文件, 为什么呢? 因为它的速度实在太慢了, 例如字符串和整数之间的转换, 自己手动实现的都比标准库实现的速度快几十甚至几百倍, Windows有自己IO操作函数, 例如lstrlenA(), lstrcatA(), wsprintfA(),CreateFile(), 等等, 有些已经过期了, 能自己实现的尽量自己实现

[此贴子已经被作者于2021-8-30 15:55编辑过]

```
#include <windows.h>

int main()
{
while (MessageBoxA(NULL, "这是正文字符串, 继续显示吗?", "这是标题字符串", MB_YESNO) == IDYES);
return 0;
}

```

```#include <stdio.h>

int sum(int n, int a);

int main() {
int n, a;
scanf("%d%d", &n, &a);
printf("\b\b= %d\n", sum(n, a));
}

int sum(int n, int a) {
int s = 0;
int tmp = a;
for (int i = 1; i <= n; i++) {
s += tmp;
printf("%d + ", tmp);
tmp = tmp * 10 + a;
}
return s;
}
```

[此贴子已经被作者于2021-9-3 21:25编辑过]

• 7
• 1/1页
• 1